2016-08-26 20 views
1

インターフェイスを実装するカスタムクラス(イングランド/ウェールズのビジネスバンクの休日に関する)を実装しようとしています。Laravel 5 - インターフェイスをバインドできません

マイフォルダ構造は次のとおりです。

App\Library\WorkDay.php 
App\Library\Holiday\HolidayInterface.php 
App\Library\Holiday\HolidayEnglandWales.php 
App\Providers\HolidayInterfaceProvider.php 
Tests\WorkDayTest.php 

HolidayInterface:

<?php 

namespace App\Library\Holiday; 

/** 
* Holiday Interface 
* 
* @author andy.roberts 
*/ 

interface HolidayInterface { 

/** 
* Retrieve a list of holidays for supplied year 
* 
* @param int $year Holiday year 
* @param int $subsitute Subsitute holiday on weekend 
*/ 
public function getHoliday($year); 

/** 
* Add a single holiday event 
* 
* @param string $name 
* @param int $timestamp 
*/ 
public function addHoliday($name, $timestamp); 

/** 
* Remove single holiday event 
* 
* @param int $timestamp 
*/ 
public function removeHoliday($timestamp); 
} 

HolidayEnglandWales:

<?php 

namespace App\Library\Holiday; 

use App\Library\Holiday\HolidayInterface; 


/** 
* Public and bank holidays in England and Wales 
* 
* @author andy.roberts 
*/ 

class HolidayEnglandWales implements HolidayInterface { 
    .... 
} 

HolidayInterfaceProvider:

<?php 

namespace App\Providers; 

use Illuminate\Support\ServiceProvider; 

use App\Library\Holiday\HolidayEnglandWales; 

class HolidayInterfaceProvider extends ServiceProvider 
{ 

    /** 
    * Bootstrap the application services. 
    * 
    * @return void 
    */ 
    public function boot() 
    { 
     // 
    } 

    /** 
    * Register the application services. 
    * 
    * @return void 
    */ 
    public function register() 
    { 
     $this->app->bind('App\Library\Holiday\HolidayInterface', function(){ 

      return new HolidayEnglandWales(); 

     }); 
    } 


} 

平日:

<?php 


namespace App\Library; 

/** 
* Calculate the number of working days between two dates 
* 
* This is achieved by a simple algorithm which calculates 
* the number of complete weeks and remaining days within 
* any given period. 
* 
* Each complete week is multiplied by the number of 
* working days, and the remaining days enumerated. 
* 
* Public holidays are included in the calculation. 
* 
* @author andy.roberts 
*/ 

class WorkDay { 

    const DAY = 86400; 
    const WEEK = 604800; 
    const MONDAY = 1; 
    const TUESDAY = 2; 
    const WEDNESDAY = 3; 
    const THURSDAY = 4; 
    const FRIDAY = 5; 
    const SATURDAY = 6; 
    const SUNDAY = 7; 

    ... 

    public function __construct(HolidayInterface $holiday, $params = array()) { 

     $this->_nonWorkingDay = array(self::SATURDAY, self::SUNDAY); 

     if(isset($params['includeEndDay'])) { 
      $this->_includeEndDay = ($params['includeEndDay'] == true) ? true : false; 
     } 

     $this->_holiday = $holiday; 
    } 

} 

WorkDayTest:

<?php 

use Illuminate\Foundation\Testing\WithoutMiddleware; 
use Illuminate\Foundation\Testing\DatabaseMigrations; 
use Illuminate\Foundation\Testing\DatabaseTransactions; 
use App\Models\User; 
use Carbon\Carbon; 
use Symfony\Component\Console\Output\ConsoleOutput; 

use App\Library\WorkDay; 
use App\Library\Holiday\HolidayEnglandWales; 


/** 
* Working Days Test Case 
*/ 
class WorkDayTest extends TestCase 
{ 

    /** 
    * Prepares the environment before running a test. 
    */ 
    /* 
    protected function setUp() { 
     parent::setUp(); 
     date_default_timezone_set('Europe/London'); 
    } 
    */ 


    /** 
    * Working days in a single month 
    * 
    * January 2010 
    * 
    * 31 days 
    * 21 working days 
    * 1 holiday (New Years Day) 
    */ 
    public function testWorkingDayInSingleMonth() { 
     $workDay = new WorkDay(new HolidayEnglandWales()); 
     $this->assertEquals($workDay->count('2010-01-01', '2010-01-31'), 20); 
    } 
} 

WorkDayTestでは、この行が実行される:

$workDay = new WorkDay(new HolidayEnglandWales()); 

このエラーが生成される:

enter image description here

クラスがインターフェイスにバインドされていないようで、問題の内容がわかりません。私は無駄に作曲家の更新を試みました。 App \ Config、btwのプロバイダ配列にプロバイダを追加しました。

ご協力いただければ幸いです。レジスタ()関数の中でAppServiceProvider

答えて

0

私はあなたがあなたのクラスとインターフェイスを追加すべきだと思います。

例:

$this->app->bind(
    \Rar\Services\HelperService::class, 
    \Rar\Services\Impl\HelperServiceImpl::class 
); 

ここHelperServiceは、クラス あるとHelperServiceImplは、私はそれを行っているインタフェース

+0

です。私は問題を解決したと思う。 WorkDay.phpで、私は働いていると思われるpublic function __construct(\ App \ Library \ Holiday \ HolidayInterface $ holiday、$ params = array())にコンストラクタを変更しました。それを見守ります。 –

関連する問題