public function getFilter()
 {
     $filterComposite = new FilterComposite();
     $filterComposite->addFilter("get", new GetFilter());
     $excludes = new FilterComposite();
     $excludes->addFilter("servicemanager", new MethodMatchFilter("getServiceManager"), FilterComposite::CONDITION_AND);
     $excludes->addFilter("eventmanager", new MethodMatchFilter("getEventManager"), FilterComposite::CONDITION_AND);
     $filterComposite->addFilter("excludes", $excludes, FilterComposite::CONDITION_AND);
     return $filterComposite;
 }
 /**
  * Create service with name
  *
  * @param ServiceLocatorInterface $serviceLocator
  * @param $name
  * @param $requestedName
  * @return mixed
  */
 public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
 {
     $config = $serviceLocator->get('Config');
     $config = $config[self::FACTORY_NAMESPACE][$requestedName];
     $filterCollection = new FilterComposite();
     $conditionMap = array('and' => FilterComposite::CONDITION_AND, 'or' => FilterComposite::CONDITION_OR);
     foreach ($config as $property => $filters) {
         $propertyFilter = new PropertyFilterComposite($property);
         foreach ($filters as $filterKey => $filterConfig) {
             $condition = isset($filterConfig['condition']) ? $conditionMap[$filterConfig['condition']] : FilterComposite::CONDITION_OR;
             $filterService = $filterConfig['filter'];
             if (!$serviceLocator->has($filterService)) {
                 throw new ServiceNotCreatedException(sprintf('Invalid filter %s for field %s: service does not exist', $filterService, $name));
             }
             $filterService = $serviceLocator->get($filterService);
             if (!$filterService instanceof FilterInterface) {
                 throw new InvalidCallbackException(sprintf('Filter service %s must implement FilterInterface'), get_class($filterService));
             }
             $propertyFilter->addFilter($filterKey, $filterService, $condition);
         }
         $filterCollection->addFilter($property, $propertyFilter, FilterComposite::CONDITION_AND);
     }
     return $filterCollection;
 }
Ejemplo n.º 3
0
 /**
  * Add a new filter to take care of what needs to be hydrated.
  * To exclude e.g. the method getServiceLocator:
  *
  * <code>
  * $composite->addFilter("servicelocator",
  *     function($property) {
  *         list($class, $method) = explode('::', $property);
  *         if ($method === 'getServiceLocator') {
  *             return false;
  *         }
  *         return true;
  *     }, FilterComposite::CONDITION_AND
  * );
  * </code>
  *
  * @param string $name Index in the composite
  * @param callable|Filter\FilterInterface $filter
  * @param int $condition
  * @return Filter\FilterComposite
  */
 public function addFilter($name, $filter, $condition = FilterComposite::CONDITION_OR)
 {
     return $this->filterComposite->addFilter($name, $filter, $condition);
 }
    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"));
    }