/** {@inheritDoc} */ public function isComparable(AbstractSnapshot $snapshot) { if (!parent::isComparable($snapshot)) { return false; } return $snapshot->oid === $this->oid; }
/** * Construct the snapshot * * The following options are taken into account : * - snapshotClass : snapshot class to use for each elements. If none are * given, the normalize() method will transform this into * either an ArraySnapshot or an ObjectSnapshot, depending * on the situation. * * @param mixed $data Either an array or a traversable, data to take a snapshot of * @param mixed $primary Property path compatible value to use to get the primary key of each elements * @param array $options Array of options * * @throws InvalidArgumentException the $data is not an array or a Traversable * @throws InvalidArgumentException the $data is not an at least 2 dimensional array * @throws InvalidArgumentException the snapshotClass in the options is not loadable * @throws InvalidArgumentException the snapshotClass in the options is not a valid snapshot class * @throws InvalidArgumentException one of the elements of the collection does not have a $primary key */ public function __construct($data, $primary, array $options = []) { $this->data = []; $this->raw = $data; $primary = new PropertyPath($primary); $snapshot = null; $accessor = PropertyAccess::createPropertyAccessorBuilder()->enableExceptionOnInvalidIndex()->getPropertyAccessor(); if (isset($options['snapshotClass'])) { if (!class_exists($options['snapshotClass'])) { throw new InvalidArgumentException(sprintf('The snapshot class "%s" does not seem to be loadable', $options['snapshotClass'])); } $refl = new ReflectionClass($options['snapshotClass']); if (!$refl->isInstantiable() || !$refl->isSubclassOf('Totem\\AbstractSnapshot')) { throw new InvalidArgumentException('A Snapshot Class should be instantiable and extends abstract class Totem\\AbstractSnapshot'); } $snapshot = $options['snapshotClass']; } if (!is_array($data) && !$data instanceof Traversable) { throw new InvalidArgumentException(sprintf('An array or a Traversable was expected to take a snapshot of a collection, "%s" given', is_object($data) ? get_class($data) : gettype($data))); } foreach ($data as $key => $value) { if (!is_int($key)) { throw new InvalidArgumentException('The given array / Traversable is not a collection as it contains non numeric keys'); } if (!$accessor->isReadable($value, $primary)) { throw new InvalidArgumentException(sprintf('The key "%s" is not defined or readable in one of the elements of the collection', $primary)); } $primaryKey = $accessor->getValue($value, $primary); $this->link[$primaryKey] = $key; $this->data[$primaryKey] = $this->snapshot($value, $snapshot); } parent::normalize(); }
public function __construct(array $data) { $this->raw = $data; $this->data = $data; parent::normalize(); }
public function isComparable(AbstractSnapshot $snapshot) { $refl = new \ReflectionObject($snapshot); return $refl->isAnonymous() || parent::isComparable($snapshot); }
/** {@inheritDoc} */ public function compute(AbstractSnapshot $old, AbstractSnapshot $new) { if (null !== $this->changes) { return; } $this->changes = []; foreach (array_unique(array_merge(array_keys($old->getComparableData()), array_keys($new->getComparableData()))) as $key) { $result = $this->computeEntry($old, $new, $key); if (null !== $result) { $this->changes[$key] = $result; } } /* * Collection tricky case : each changes should not be represented by * its primary key, but by a numeric key. Because it is a collection, duh. */ if ($old instanceof CollectionSnapshot && $new instanceof CollectionSnapshot) { $this->changes = array_values($this->changes); } }