예제 #1
0
파일: Getter.php 프로젝트: TuxBoy/Demo-saf
 /**
  * Generic getter for a collection of objects
  *
  * @param $stored     Component[] Actual value of the property (will be returned if not null)
  * @param $class_name string Class for each collection's object
  * @param $object     object Parent object
  * @param $property   string|Reflection_Property Parent property (or property name). Recommended
  *        but can be omitted if foreign class is a Component
  * @return object[]
  */
 public static function &getCollection(&$stored, $class_name, $object, $property = null)
 {
     if (!(self::$ignore || isset($stored))) {
         if (Dao::getObjectIdentifier($object)) {
             $search_element = Search_Object::create($class_name);
             $is_component = isA($search_element, Component::class);
             if (isset($property)) {
                 if (!$property instanceof Reflection_Property) {
                     $property = new Reflection_Property(get_class($object), $property);
                 }
                 $property_name = $property->getAnnotation('foreign')->value;
                 $dao = ($dao = $property->getAnnotation('dao')->value) ? Dao::get($dao) : Dao::current();
             } else {
                 $dao = Dao::current();
                 $property_name = null;
             }
             if ($is_component) {
                 /** @var $search_element Component */
                 $search_element->setComposite($object, $property_name);
                 $link_properties_names = (new Link_Class($class_name))->getLinkPropertiesNames();
                 $options = $link_properties_names ? [Dao::sort(), Dao::key($link_properties_names)] : [Dao::sort()];
                 $stored = $dao->search($search_element, null, $options);
             } elseif (!empty($property_name)) {
                 $property = new Reflection_Property(get_class($search_element), $property_name);
                 $accessible = $property->isPublic();
                 if (!$accessible) {
                     $property->setAccessible(true);
                 }
                 $property->setValue($search_element, $object);
                 if (!$accessible) {
                     $property->setAccessible(false);
                 }
                 $stored = $dao->search($search_element, null, [Dao::sort()]);
             } else {
                 trigger_error('getCollection() must be called for a component foreign type' . ' or with a parent property name', E_USER_ERROR);
             }
         }
         if (!isset($stored)) {
             $stored = [];
         }
     }
     return $stored;
 }