コード例 #1
0
ファイル: ModelBuilder.php プロジェクト: ncud/sagalaya
 public static function create($object, $data = array())
 {
     static::init($object);
     foreach (static::$properties as $property) {
         if (ModelAnnotation::match($property, array('ManyToMany', 'OneToMany'))) {
             $property->setAccessible(true);
             $property->setValue($object, new ArrayCollection());
         }
     }
     foreach ($data as $field => $value) {
         try {
             $comment = ModelAnnotation::get($object, $field);
             if (preg_match('|ManyToMany|', $comment) || preg_match('|OneToMany|', $comment)) {
                 static::add($object, $field, $value);
             } else {
                 if (preg_match('|ManyToOne|', $comment) || preg_match('|OneToOne|', $comment)) {
                     static::set($object, $field, $value);
                 } else {
                     $object->{$field} = $value;
                 }
             }
         } catch (\ReflectionException $e) {
             continue;
         }
     }
 }
コード例 #2
0
ファイル: ModelValidator.php プロジェクト: ncud/sagalaya
 public static function convertToArray($object)
 {
     $result = array();
     $reflector = new \ReflectionClass($object);
     $properties = $reflector->getProperties(\ReflectionProperty::IS_PROTECTED);
     foreach ($properties as $property) {
         $property->setAccessible(true);
         $value = $property->getValue($object);
         $field = $property->getName();
         if ($value == null) {
             $result[$field] = null;
         } else {
             if (ModelAnnotation::match($property, array('ManyToMany', 'OneToMany'))) {
                 $result[$field] = $value->count();
             } elseif (ModelAnnotation::match($property, array('ManyToOne', 'OneToOne'))) {
                 $result[$field] = $value->id;
             } else {
                 $result[$field] = $value;
             }
         }
     }
     return $result;
 }