/** * Build tabs containing class properties * * @param $class Reflection_Class * @return Tab[] Tabs will contain Reflection_Property[] as content */ public static function build(Reflection_Class $class) { /** @var $group_annotations Group_Annotation[] */ $group_annotations = $class->getAnnotations('group'); $properties = $class->getProperties([T_EXTENDS, T_USE]); return self::buildProperties($properties, $group_annotations); }
/** * Call onDuplicate methods defined by @duplicate class annotation * * @param $object object * @param $class Reflection_Class */ private function onDuplicate($object, Reflection_Class $class) { foreach ($class->getAnnotations('duplicate') as $on_duplicate) { $callback = explode('::', $on_duplicate->value); if ($callback[1] === true || is_numeric($callback[1])) { $callback[1] = 'onDuplicate'; } if (isA($object, $callback[0])) { call_user_func([$object, $callback[1]]); } else { call_user_func([$callback[0], $callback[1]], $object); } } }
/** * Build tabs containing object properties * * This fills in properties 'display' and 'value' special properties, usefull ie for * Html_Template_Functions * * @param $object object * @param $filter_properties string[] * @return Tab[] tabs will contain Reflection_Property_Value[] as content */ public static function buildObject($object, $filter_properties = null) { $class = new Reflection_Class(get_class($object)); /** @var $group_annotations Group_Annotation[] */ $group_annotations = $class->getAnnotations('group'); self::mergeGroups($group_annotations); $properties = $class->accessProperties(); foreach ($properties as $property_name => $property) { if (!isset($filter_properties) || in_array($property_name, $filter_properties)) { $property = new Reflection_Property_Value($property->class, $property->name, $object, false, true); $property->final_class = $class->name; $properties[$property_name] = $property; } else { unset($properties[$property_name]); } } return parent::buildProperties($properties, $group_annotations); }
/** * Initializes the object if not already set * - if a data link identifier is set, read the object from the data link and remove it from * $array * - if the object is a link class and the link class identifier properties values are set, * read the object from the data link * * @param $array array the source array * @param $object object the object to complete (if set) or to build (if null) * This object is always set at the end of execution of initObject() * @return array if read from a link object, this is the search properties that identify it */ private function initObject(&$array, &$object) { if (!isset($object)) { if (isset($array['id']) && $array['id']) { $object = Dao::read($array['id'], $this->class->name); } else { foreach ($this->class->getAnnotations('before_build_array') as $before) { call_user_func_array([$this->class->name, $before->value], [&$array]); } $link_search = $this->initLinkObject($array, $object); if (!isset($object)) { $object = $this->class->newInstance(); } } if (isset($array['id'])) { unset($array['id']); } } return isset($link_search) ? $link_search : null; }
/** * @param $object object * @param $only_properties string[] property names if we want to check those properties only * @return boolean */ public function validate($object, $only_properties = []) { $this->valid = true; $only_properties = array_flip($only_properties); $class = new Reflection_Class($object); // properties value validation foreach ($class->accessProperties() as $property) { if (!$only_properties || isset($only_properties[$property->name])) { $property_validator = new Property_Validator($property); $validated_property = $property_validator->validate($object); if (is_null($validated_property)) { return $this->valid = null; } else { $this->report = array_merge($this->report, $property_validator->report); $this->valid = $this->valid && $validated_property; } } } // object validation foreach ($class->getAnnotations() as $annotation) { if ($annotation instanceof Template\Object_Validator) { $validated_annotation = $annotation->validate($object); if (is_null($validated_annotation)) { return $this->valid = null; } else { if (!$validated_annotation) { $this->report[] = $annotation; } $this->valid = $this->valid && $validated_annotation; } } } return $this->valid; }