Ejemplo n.º 1
0
 /**
  * build the resource from an model object using his getters.
  *
  *
  * @param ResourceInterface $model
  * @param $object
  *
  * @return ResourceInterface
  */
 public static function buildFromObject(ModelInterface $model, $object, $dateFormat = 'Y-m-d')
 {
     $reflection = new \ReflectionClass($model);
     $properties = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED);
     $reflectionModel = new \ReflectionClass($object);
     foreach ($properties as $property) {
         $setter = 'set' . ucfirst($property->getName());
         $getter = 'get' . ucfirst($property->getName());
         try {
             $method = $reflectionModel->getMethod($getter);
             $value = $object->{$getter}();
             $attribute = $model->{$getter}();
             if ($value instanceof \DateTime) {
                 // format special pour les dates ...
                 $model->{$setter}($value->format($dateFormat));
             } else {
                 if (is_object($value) && $model->{$getter}() instanceof ModelInterface) {
                     self::buildFromObject($attribute, $value, $dateFormat);
                 } else {
                     if (is_array($value)) {
                         // : can only map array of scalar => @todo map array of object
                         $collection = new Collection();
                         $collection->setItems($value);
                         $model->{$setter}($collection);
                     } else {
                         $model->{$setter}($value);
                     }
                 }
             }
         } catch (\ReflectionException $e) {
             // nothing to do!
         }
     }
     return $model;
 }
Ejemplo n.º 2
0
 public function testCollection()
 {
     $collection = new Collection();
     $this->assertEquals(0, $collection->count());
     $this->assertEquals(array(), $collection->toArray());
     $a = new Pet();
     $collection->push($a);
     $this->assertEquals(1, $collection->count());
     $this->assertEquals(array(array('id' => 123, 'name' => 'myName')), $collection->toArray());
     $this->assertInstanceOf('\\ArrayIterator', $collection->getIterator());
 }