Bravo3\Orm\Services\ListManager::remove PHP Method

remove() public static method

Remove an item from a list
public static remove ( object $entity, string $property, object $value, array $fns, string $getter = null, string $setter = null )
$entity object
$property string Property name to remove from, not needed if providing both a getter and setter
$value object Value to remove
$fns array Array of getter functions which must have equal value to consider a match
$getter string Custom getter name
$setter string Custom setter name
    public static function remove($entity, $property, $value, array $fns, $getter = null, $setter = null)
    {
        list($getter, $setter) = self::resolveFunctionNames($property, $getter, $setter);
        $arr = $entity->{$getter}();
        if (!$arr) {
            return;
        }
        $local_values = [];
        foreach ($fns as $fn_index => $fn) {
            $local_values[$fn_index] = $value->{$fn}();
        }
        foreach ($arr as $item_index => $item) {
            foreach ($fns as $fn_index => $fn) {
                if ($local_values[$fn_index] !== $item->{$fn}()) {
                    continue 2;
                }
            }
            unset($arr[$item_index]);
            $entity->{$setter}(array_values($arr));
        }
    }

Usage Example

Example #1
0
 /**
  * Remove an asset from the category
  *
  * @param Asset $asset
  * @return $this
  */
 public function removeAsset(Asset $asset)
 {
     ListManager::remove($this, 'assets', $asset, ['getId']);
     return $this;
 }
All Usage Examples Of Bravo3\Orm\Services\ListManager::remove