/**
  * 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();
 }
Beispiel #2
0
 /**
  * Build this snapshot.
  *
  * @param object $object Object to fix at the current moment
  *
  * @throws InvalidArgumentException If this is not an object
  */
 public function __construct($object)
 {
     if (!is_object($object)) {
         throw new InvalidArgumentException(sprintf('Object expected, had "%s"', gettype($object)));
     }
     $this->raw = $object;
     $this->oid = spl_object_hash($object);
     $export = (array) $object;
     $class = get_class($object);
     foreach ($export as $property => $value) {
         $property = str_replace(["*", "{$class}"], '', $property);
         // not accessible properties
         $this->data[$property] = $value;
     }
     parent::normalize();
 }
Beispiel #3
0
 /**
  * Build this snapshot.
  *
  * @param object $object Object to fix at the current moment
  *
  * @throws InvalidArgumentException If this is not an object
  */
 public function __construct($object)
 {
     if (!is_object($object)) {
         throw new InvalidArgumentException(sprintf('Object expected, had "%s"', gettype($object)));
     }
     $this->raw = $object;
     $this->oid = spl_object_hash($object);
     $refl = new ReflectionObject($object);
     if (method_exists('\\ReflectionObject', 'isCloneable') && $refl->isCloneable()) {
         $this->raw = clone $object;
     }
     /** @var \ReflectionProperty $reflProperty */
     foreach ($refl->getProperties() as $reflProperty) {
         $reflProperty->setAccessible(true);
         $value = $reflProperty->getValue($object);
         $this->data[$reflProperty->getName()] = $value;
     }
     parent::normalize();
 }
Beispiel #4
0
 public function __construct(array $data)
 {
     $this->raw = $data;
     $this->data = $data;
     parent::normalize();
 }