public function __construct(array $records)
 {
     $this->addValidator(function ($record) {
         if (!$record instanceof UploadRecord) {
             throw new \InvalidArgumentException('Record must be an instance of UploadRecord');
         }
     });
     $this->setKey(function (UploadRecord $record) {
         return $record->getPageID();
     });
     parent::__construct($records);
 }
Beispiel #2
0
 public function testSerialization()
 {
     $collection = new Collection();
     $collection->setKey(function ($item) {
         return $item['id'];
     });
     $collection->setSort(function ($a, $b) {
         return $a > $b ? -1 : 1;
     }, $collection::SORT_KEY);
     $collection->addValidator(function ($item) {
         return array_key_exists('id', $item);
     });
     $this->assertInstanceOf('Serializable', $collection);
     $serialized = serialize($collection);
     $unserialized = unserialize($serialized);
     $item1 = ['id' => 1, 'hello' => 'word'];
     $item2 = ['id' => 2, 'hello' => 'word'];
     $unserialized->add($item1);
     $unserialized->add($item2);
     $this->assertSame($item1, $unserialized->get(1));
     $this->assertSame([2 => $item2, 1 => $item1], $unserialized->all());
     try {
         $unserialized->add(['hey' => 'i dont have an id!']);
     } catch (\InvalidArgumentException $e) {
         return true;
     }
     $this->fail('Validation did not fail after being serialized.');
 }
Beispiel #3
0
 /**
  * Instanciates an objects for each row of the resultset and sets the
  * properties of it based on the keys/values.
  *
  * @see bind
  *
  * @param  string $className          The fully qualified name of a class you wish to
  *                                    instantiate and bind data to.
  * @param  array        $args         Array of arguments to pass to the constructor
  * @param  bool         $force        True to bind properties even if they don't exist
  * @param  Collection   $cache        Cache to attempt to get objects from
  * @param  string|null  $force        The key to match the row to the
  * @throws \InvalidArgumentException  Throws exception if class name is not string or if not found
  *
  * @return array                      The updated object(s) with data bound to them.
  */
 public function bindTo($className, array $args = array(), $force = false, Collection $cache = null, $cacheKey = null)
 {
     // Only strings can be passed in
     if (!is_string($className)) {
         throw new \InvalidArgumentException('Only a fully qualified classname can be passed to Result::bindTo()');
     }
     // Existing, valid class name?
     if (!class_exists($className)) {
         throw new \InvalidArgumentException(sprintf('`%s` class not found', $className));
     }
     $this->_setDefaultKeys($cacheKey);
     $this->reset();
     $result = array();
     while ($row = $this->_result->fetchObject()) {
         if ($cache !== null) {
             if ($cache->exists($row->{$cacheKey})) {
                 $result[] = $cache->get($row->{$cacheKey});
                 continue;
             }
         }
         if ($args) {
             $obj = new \ReflectionClass($className);
             $obj = $obj->newInstanceArgs($args);
         } else {
             $obj = new $className();
         }
         $this->_bindPropertiesToObject($obj, $row, $force);
         $result[] = $obj;
         if ($cache !== null) {
             $cache->add($obj);
         }
     }
     return $result;
 }
 /**
  * @param UserTypeInterface $type
  */
 public function __construct(UserTypeInterface $type)
 {
     $this->_type = $type;
     parent::__construct();
 }
 /**
  * @param array $generators
  * @param $default
  */
 public function __construct(array $generators, $default)
 {
     parent::__construct($generators);
     $this->setDefault($default);
 }