Symfony\Component\PropertyAccess\PropertyAccessor::writeCollection PHP Method

writeCollection() private method

Adjusts a collection-valued property by calling add*() and remove*() methods.
private writeCollection ( array $zval, string $property, array | Traversable $collection, string $addMethod, string $removeMethod )
$zval array The array containing the object to write to
$property string The property to write
$collection array | Traversable The collection to write
$addMethod string The add*() method
$removeMethod string The remove*() method
    private function writeCollection($zval, $property, $collection, $addMethod, $removeMethod)
    {
        // At this point the add and remove methods have been found
        $previousValue = $this->readProperty($zval, $property);
        $previousValue = $previousValue[self::VALUE];

        if ($previousValue instanceof \Traversable) {
            $previousValue = iterator_to_array($previousValue);
        }
        if ($previousValue && is_array($previousValue)) {
            if (is_object($collection)) {
                $collection = iterator_to_array($collection);
            }
            foreach ($previousValue as $key => $item) {
                if (!in_array($item, $collection, true)) {
                    unset($previousValue[$key]);
                    $zval[self::VALUE]->{$removeMethod}($item);
                }
            }
        } else {
            $previousValue = false;
        }

        foreach ($collection as $item) {
            if (!$previousValue || !in_array($item, $previousValue, true)) {
                $zval[self::VALUE]->{$addMethod}($item);
            }
        }
    }