RulerZ::applyFilterSpec()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RulerZ;
6
7
use RulerZ\Compiler\Compiler;
8
use RulerZ\Compiler\CompilationTarget;
9
use RulerZ\Context\ExecutionContext;
10
use RulerZ\Exception\TargetUnsupportedException;
11
use RulerZ\Spec\Specification;
12
13
class RulerZ
14
{
15
    /**
16
     * @var Compiler
17
     */
18
    private $compiler;
19
20
    /**
21
     * @var array<CompilationTarget>
22
     */
23
    private $compilationTargets = [];
24
25
    /**
26
     * @param array $compilationTargets A list of compilation targets, each one handles a specific target type (an array, a DoctrineQueryBuilder, ...)
27
     */
28
    public function __construct(Compiler $compiler, array $compilationTargets = [])
29
    {
30
        $this->compiler = $compiler;
31
32
        foreach ($compilationTargets as $targetCompiler) {
33
            $this->registerCompilationTarget($targetCompiler);
34
        }
35
    }
36
37
    /**
38
     * Registers a new target compiler.
39
     *
40
     * @param CompilationTarget $compilationTarget The target compiler to register.
41
     */
42
    public function registerCompilationTarget(CompilationTarget $compilationTarget): void
43
    {
44
        $this->compilationTargets[] = $compilationTarget;
45
    }
46
47
    /**
48
     * Apply the filters on the target using the given rule and parameters.
49
     * The target compiler to use is determined at runtime using the registered ones.
50
     *
51
     * @param mixed  $target           The target to filter.
52
     * @param string $rule             The rule to apply.
53
     * @param array  $parameters       The parameters used in the rule.
54
     * @param array  $executionContext The execution context.
55
     *
56
     * @return mixed
57
     *
58
     * @throws TargetUnsupportedException
59
     */
60 View Code Duplication
    public function applyFilter($target, string $rule, array $parameters = [], array $executionContext = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
    {
62
        $targetCompiler = $this->findTargetCompiler($target, CompilationTarget::MODE_APPLY_FILTER);
63
        $compilationContext = $targetCompiler->createCompilationContext($target);
64
        $executor = $this->compiler->compile($rule, $targetCompiler, $compilationContext);
65
66
        return $executor->applyFilter($target, $parameters, $targetCompiler->getOperators()->getOperators(), new ExecutionContext($executionContext));
67
    }
68
69
    /**
70
     * Filters a target using the given rule and parameters.
71
     * The target compiler to use is determined at runtime using the registered ones.
72
     *
73
     * @param mixed  $target           The target to filter.
74
     * @param string $rule             The rule to apply.
75
     * @param array  $parameters       The parameters used in the rule.
76
     * @param array  $executionContext The execution context.
77
     *
78
     * @return \Traversable The filtered target.
79
     *
80
     * @throws TargetUnsupportedException
81
     */
82 View Code Duplication
    public function filter($target, string $rule, array $parameters = [], array $executionContext = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        $targetCompiler = $this->findTargetCompiler($target, CompilationTarget::MODE_FILTER);
85
        $compilationContext = $targetCompiler->createCompilationContext($target);
86
        $executor = $this->compiler->compile($rule, $targetCompiler, $compilationContext);
87
88
        return $executor->filter($target, $parameters, $targetCompiler->getOperators()->getOperators(), new ExecutionContext($executionContext));
89
    }
90
91
    /**
92
     * Filters a target using the given specification.
93
     * The targetCompiler to use is determined at runtime using the registered ones.
94
     *
95
     * @param mixed         $target           The target to filter.
96
     * @param Specification $spec             The specification to apply.
97
     * @param array         $executionContext The execution context.
98
     *
99
     * @return mixed The filtered target.
100
     *
101
     * @throws TargetUnsupportedException
102
     */
103
    public function filterSpec($target, Specification $spec, array $executionContext = [])
104
    {
105
        return $this->filter($target, $spec->getRule(), $spec->getParameters(), $executionContext);
106
    }
107
108
    /**
109
     * Apply the filters on a target using the given specification.
110
     * The targetCompiler to use is determined at runtime using the registered ones.
111
     *
112
     * @param mixed         $target           The target to filter.
113
     * @param Specification $spec             The specification to apply.
114
     * @param array         $executionContext The execution context.
115
     *
116
     * @return mixed
117
     *
118
     * @throws TargetUnsupportedException
119
     */
120
    public function applyFilterSpec($target, Specification $spec, array $executionContext = [])
121
    {
122
        return $this->applyFilter($target, $spec->getRule(), $spec->getParameters(), $executionContext);
123
    }
124
125
    /**
126
     * Tells if a target satisfies the given rule and parameters.
127
     * The target compiler to use is determined at runtime using the registered ones.
128
     *
129
     * @param mixed  $target           The target.
130
     * @param string $rule             The rule to test.
131
     * @param array  $parameters       The parameters used in the rule.
132
     * @param array  $executionContext The execution context.
133
     *
134
     * @throws TargetUnsupportedException
135
     */
136 View Code Duplication
    public function satisfies($target, string $rule, array $parameters = [], array $executionContext = []): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
    {
138
        $targetCompiler = $this->findTargetCompiler($target, CompilationTarget::MODE_SATISFIES);
139
        $compilationContext = $targetCompiler->createCompilationContext($target);
140
        $executor = $this->compiler->compile($rule, $targetCompiler, $compilationContext);
141
142
        return $executor->satisfies($target, $parameters, $targetCompiler->getOperators()->getOperators(), new ExecutionContext($executionContext));
143
    }
144
145
    /**
146
     * Tells if a target satisfies the given specification.
147
     * The target compiler to use is determined at runtime using the registered ones.
148
     *
149
     * @param mixed         $target           The target.
150
     * @param Specification $spec             The specification to use.
151
     * @param array         $executionContext The execution context.
152
     *
153
     * @throws TargetUnsupportedException
154
     */
155
    public function satisfiesSpec($target, Specification $spec, array $executionContext = []): bool
156
    {
157
        return $this->satisfies($target, $spec->getRule(), $spec->getParameters(), $executionContext);
158
    }
159
160
    /**
161
     * Finds a target compiler supporting the given target.
162
     *
163
     * @param mixed  $target The target to filter.
164
     * @param string $mode   The execution mode (MODE_FILTER or MODE_SATISFIES).
165
     *
166
     * @throws TargetUnsupportedException
167
     */
168
    private function findTargetCompiler($target, $mode): CompilationTarget
169
    {
170
        /** @var CompilationTarget $targetCompiler */
171
        foreach ($this->compilationTargets as $targetCompiler) {
172
            if ($targetCompiler->supports($target, $mode)) {
173
                return $targetCompiler;
174
            }
175
        }
176
177
        throw new TargetUnsupportedException('The given target is not supported.');
178
    }
179
}
180