Example #1
0
 /**
  * Return current ObjecModel\Collection instance handled by current instance
  * instant instanciation is performed if $_value is null or $force is true
  * 
  *  @param boolean $force
  *  @return t41\ObjectModel\Collection
  */
 public function getValue($force = false)
 {
     if (is_null($this->_value) || $force === true) {
         /* set a new Collection based on instanceof parameter value */
         $this->_value = new ObjectModel\Collection($this->getParameter('instanceof'));
         $this->_value->setBoundaryBatch(-1);
         $this->_value->setParent($this->_parent);
         /* inject the condition that allows to find collection members */
         if ($this->getParameter('keyprop')) {
             $this->_value->having($this->getParameter('keyprop'))->equals($this->_parent);
         }
         /* inject any other defined condition */
         if ($this->getParameter('morekeyprop')) {
             foreach ($this->getParameter('morekeyprop') as $value) {
                 if (strstr(trim($value), ' ') !== false) {
                     // if value contains spaces, it's a pattern
                     $parts = explode(' ', $value);
                     if (count($parts) == 3) {
                         if ($parts[2] == 'novalue') {
                             $parts[2] = Condition::NO_VALUE;
                         }
                         if ($parts[2] == ObjectUri::IDENTIFIER) {
                             $parts[2] = $this->_parent->getUri();
                         }
                         if (substr($parts[2], 0, 1) == '%' && substr($parts[2], -1) == '%') {
                             $prop = substr($parts[2], 1, strlen($parts[2]) - 2);
                             if (($prop = $this->_parent->getProperty($prop)) !== false) {
                                 $parts[2] = $prop->getValue();
                             }
                         }
                         if (strstr($parts[2], ',') !== false) {
                             $parts[2] = explode(',', $parts[2]);
                         }
                         $this->_value->having($parts[0])->{$parts}[1]($parts[2]);
                     } else {
                         if ($parts[1] == 'novalue') {
                             $parts[1] = Condition::NO_VALUE;
                         }
                         if (substr($parts[1], 0, 1) == '%' && substr($parts[1], -1) == '%') {
                             $prop = substr($parts[1], 1, strlen($parts[1]) - 2);
                             if (($prop = $this->_parent->getProperty($prop)) !== false) {
                                 $parts[1] = $prop->getValue();
                             }
                         }
                         if (strstr($parts[1], ',') !== false) {
                             $parts[1] = explode(',', $parts[1]);
                         }
                         $this->_value->having($parts[0])->equals($parts[1]);
                     }
                 } else {
                     // default case, we expect the member to hold a property
                     // with the same name and value as the current object
                     if (($property = $this->_parent->getProperty($value)) === false) {
                         throw new Exception(sprintf("keyprop value '%s' doesn't match any property of class '%s'", $value, $this->_parent->getClass()));
                     }
                     $this->_value->having($value)->equals($property->getValue());
                 }
             }
         }
         // set sorting
         if ($this->getParameter('sorting')) {
             foreach ($this->getParameter('sorting') as $key => $val) {
                 if ($this->_value->getDataObject()->getRecursiveProperty($key) !== false) {
                     $this->_value->setSorting(array($key, $val));
                 } else {
                     Core::log(sprintf("Can't sort %s property with unknown property %s", $this->_id, $key), \Zend_Log::WARN);
                 }
             }
         }
         // DON'T POPULATE THERE, IT IS DONE IMPLICITELY IN Collection::getMembers()
         //$this->_value->debug();
     }
     return parent::getValue();
 }