Symfony2 Doctrine2 Proper Entity Design for filter and rules

So, I want to do the following, and I'm just looking for advice on how to create it.

  • I have Filterone that can be run on certain types of entities
  • Filterhave Ruleto determine their behavior
  • Filter process entities using their rules and return logical
  • As a concrete example, I can filter out, which means that some regular expression in the given field is rejected, and I can define rules for it. I can also have different rules that use simple prefix matching in the same filter.

So, I came up with the following

interface FilterInterface
{
    /**
     * Load the rules for this filter
     */
    function loadRules();

    /**
     * Filter an entity in an optional context
     * @param mixed $entity Should return true for $this->supportsEntity($entity)
     * @param array $context Other information
     * 
     * @return boolean True if the $entity passes this filter
     */
    function filter($entity, $context = array());

    /**
     * Check if filter supports the entity type
     * @param mixed $entity
     * 
     * @return boolean true if this Filter can be run for that entity type
     */
    function supportsEntity($entity);
}

Then I plan to do:

class ImageFilter implements FilterInterface

Which will load the rules from the database and implement the filter.

So, initially I, although I had Entity, sort of

FilterRule:

  • scope ( , , )
  • type ( , , , )
  • (, )

ImageFilter FilterRule, . ImageFilter . .

public function filter($entity)
{
    foreach ($this->rules as $rule) {
        switch($rule->getType()) {
            case RULE_TYPE_REGEX:
                $this->doRegex();
            break;
            case RULE_TYPE_PREFIX:
                $this->doPrefix();
            break;
        }
    }
}

, switch? , - :

public function filter($entity)
{
    foreach ($this->rules as $rule) {
        $rule->process($entity);
    }
}

, Filter , , " " . , POPO, , ( 100% , ).

Inheritance Mapping, Rule , ( ). , , , Rule - , "" -, ...

, , :

  • Filter , Rule . Rule "" .
  • Rule - . Rule "", Filter Filter , Rule
  • Rule , , switch statement
  • 2., 3. Rule "" ( , , )
  • 1 4. , Filter Rule ( , "" factory)

, , , . , , . ( , , , )

+3
1

, , , : Rules, Filters RuleProcessing. , .

Rules .

Filters , .

Rule Processing , $entity $rule . , - :

public function filter($entity)
{
    foreach ($this->rules as $rule) {
        RuleProcessing->processRule($entity, $rule);
    }
}

( , .)

0

All Articles