FOF30\Model\DataModel\Collection::find PHP 메소드

find() 공개 메소드

Find a model in the collection by key.
public find ( mixed $key, mixed $default = null ) : DataModel
$key mixed
$default mixed
리턴 FOF30\Model\DataModel
    public function find($key, $default = null)
    {
        if ($key instanceof DataModel) {
            $key = $key->getId();
        }
        return array_first($this->items, function ($itemKey, $model) use($key) {
            /** @var DataModel $model */
            return $model->getId() == $key;
        }, $default);
    }

Usage Example

예제 #1
0
 /**
  * @group           DataModel
  * @group           CollectionFind
  * @covers          FOF30\Model\DataModel\Collection::find
  * @dataProvider    CollectionDataprovider::getTestFind
  */
 public function testFind($test, $check)
 {
     $msg = 'Collection::find %s - Case: ' . $check['case'];
     $items = $this->buildCollection();
     $collection = new Collection($items);
     $key = $test['key'] == 'object' ? $items[2] : $test['key'];
     $result = $collection->find($key, $test['default']);
     if ($check['type'] == 'object') {
         $this->assertInstanceOf('FOF30\\Model\\DataModel', $result, sprintf($msg, 'Should return an instance of DataModel'));
         $this->assertEquals($check['result'], $result->getId(), sprintf($msg, 'Failed to return the correct item'));
     } else {
         $this->assertSame($check['result'], $result, sprintf($msg, 'Failed to return the correct item'));
     }
 }