Beispiel #1
0
 /**
  * Checks item must be an array or object when a key is set.
  *
  * @expectedException \InvalidArgumentException
  * @expectedExceptionMessage Item must be array or object
  */
 public function testAddingItemMustBeArrayOrOjectWhenHasKey()
 {
     $collection = new Collection();
     $collection->setKey('hello');
     $collection->add("hi");
 }
Beispiel #2
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;
 }