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; } } }
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; }