FOF30\Model\DataModel\Collection::removeById PHP Method

removeById() public method

Remove an item in the collection by key
public removeById ( mixed $key ) : void
$key mixed
return void
    public function removeById($key)
    {
        if ($key instanceof DataModel) {
            $key = $key->getId();
        }
        $index = array_search($key, $this->modelKeys());
        if ($index !== false) {
            unset($this->items[$index]);
        }
    }

Usage Example

示例#1
0
 /**
  * @group           DataModel
  * @group           CollectionRemoveById
  * @covers          FOF30\Model\DataModel\Collection::removeById
  * @dataProvider    CollectionDataprovider::getTestRemoveById
  */
 public function testRemoveById($test, $check)
 {
     $msg = 'Collection::removeById %s - Case: ' . $check['case'];
     $items = $this->buildCollection();
     $collection = new Collection($items);
     $key = $test['key'] == 'object' ? $items[2] : $test['key'];
     $collection->removeById($key);
     $this->assertArrayNotHasKey($check['key'], $collection, sprintf($msg, 'Failed to remove the item'));
 }