/**
  * Executed composite filters if property matches
  *
  * @param string $property The name of the property
  * @return bool
  */
 public function filter($property)
 {
     if ($property == $this->property) {
         return parent::filter($property);
     }
     return true;
 }
Beispiel #2
0
 /**
  * (non-PHPdoc)
  * @see \Zend\Stdlib\Hydrator\ClassMethods::extract()
  */
 public function extract($object)
 {
     if (!is_object($object)) {
         throw new Exception\BadMethodCallException(sprintf('%s expects the provided $object to be a PHP object)', __METHOD__));
     }
     $objectClass = get_class($object);
     // reset the hydrator's hydrator's cache for this object, as the filter may be per-instance
     if ($object instanceof FilterProviderInterface) {
         $this->extractionMethodsCache[$objectClass] = null;
     }
     // pass 1 - finding out which properties can be extracted, with which methods (populate hydration cache)
     if (!isset($this->extractionMethodsCache[$objectClass])) {
         $this->extractionMethodsCache[$objectClass] = array();
         $filter = $this->filterComposite;
         $methods = get_class_methods($object);
         if ($object instanceof FilterProviderInterface) {
             $filter = new FilterComposite(array($object->getFilter()), array(new MethodMatchFilter('getFilter')));
         }
         foreach ($methods as $method) {
             $methodFqn = $objectClass . '::' . $method;
             if (!($filter->filter($methodFqn) && $this->callableMethodFilter->filter($methodFqn))) {
                 continue;
             }
             $attribute = $method;
             if (strpos($method, 'get') === 0) {
                 $attribute = substr($method, 3);
                 if (!property_exists($object, $attribute)) {
                     $attribute = lcfirst($attribute);
                 }
             }
             $this->extractionMethodsCache[$objectClass][$method] = $attribute;
         }
     }
     #echo '<pre>';print_r($this->extractionMethodsCache[$objectClass]);echo '</pre>';exit;
     $values = array();
     // pass 2 - actually extract data
     foreach ($this->extractionMethodsCache[$objectClass] as $methodName => $attributeName) {
         $realAttributeName = $this->extractName($attributeName, $object);
         if (true === isset($this->fieldsetMap[$realAttributeName])) {
             $values[$this->fieldsetMap[$realAttributeName]][$realAttributeName] = $this->extractValue($realAttributeName, $object->{$methodName}(), $object);
         } else {
             $values[$realAttributeName] = $this->extractValue($realAttributeName, $object->{$methodName}(), $object);
         }
     }
     return $values;
 }
    public function testWithComplexCompositeAdded()
    {
        $filter1 = new FilterComposite();
        $filter1->addFilter("foobarbaz", function($property) {
                return true;
            });
        $filter1->addFilter("foobar", function($property) {
                return false;
            });
        $filter2 = new FilterComposite();
        $filter2->addFilter("bar", function($property) {
                return true;
            }, FilterComposite::CONDITION_AND);
        $filter2->addFilter("barblubb", function($property) {
                return true;
            }, FilterComposite::CONDITION_AND);
        $this->assertTrue($filter1->filter("foo"));
        $this->assertTrue($filter2->filter("foo"));
        $filter1->addFilter("bar", $filter2);
        $this->assertTrue($filter1->filter("blubb"));

        $filter1->addFilter("blubb", function($property) { return false; }, FilterComposite::CONDITION_AND);
        $this->assertFalse($filter1->filter("test"));
    }