Completed
Pull Request — master (#46)
by
unknown
02:24
created

RulerZ::normalizeParameters()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace RulerZ;
4
5
use RulerZ\Compiler\Compiler;
6
use RulerZ\Compiler\Target\CompilationTarget;
7
use RulerZ\Context\ExecutionContext;
8
use RulerZ\Exception\TargetUnsupportedException;
9
use RulerZ\Spec\Specification;
10
11
class RulerZ
12
{
13
    /**
14
     * @var array<CompilationTarget> $compilationTargets
15
     */
16
    private $compilationTargets = [];
17
18
    /**
19
     * Constructor.
20
     *
21
     * @param Compiler $compiler           The compiler.
22
     * @param array    $compilationTargets A list of target compilers, each one handles a specific target (an array, a DoctrineQueryBuilder, ...)
23
     */
24
    public function __construct(Compiler $compiler, array $compilationTargets = [])
25
    {
26
        $this->compiler = $compiler;
27
28
        foreach ($compilationTargets as $targetCompiler) {
29
            $this->registerCompilationTarget($targetCompiler);
30
        }
31
    }
32
33
    /**
34
     * Registers a new target compiler.
35
     *
36
     * @param CompilationTarget $compilationTarget The target compiler to register.
37
     */
38
    public function registerCompilationTarget(CompilationTarget $compilationTarget)
39
    {
40
        $this->compilationTargets[] = $compilationTarget;
41
    }
42
43
    /**
44
     * Apply the filters on the target using the given rule and parameters.
45
     * The target compiler to use is determined at runtime using the registered ones.
46
     *
47
     * @param mixed  $target           The target to filter.
48
     * @param string $rule             The rule to apply.
49
     * @param array  $parameters       The parameters used in the rule.
50
     * @param array  $executionContext The execution context.
51
     *
52
     * @return mixed
53
     */
54 View Code Duplication
    public function applyFilter($target, $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...
55
    {
56
        $parameters = $this->normalizeParameters($parameters);
57
        $targetCompiler = $this->findTargetCompiler($target, CompilationTarget::MODE_APPLY_FILTER);
58
        $executor       = $this->compiler->compile($rule, $targetCompiler);
59
60
        return $executor->applyFilter($target, $parameters, $targetCompiler->getOperators(), new ExecutionContext($executionContext));
61
    }
62
63
    /**
64
     * Filters a target using the given rule and parameters.
65
     * The target compiler to use is determined at runtime using the registered ones.
66
     *
67
     * @param mixed  $target           The target to filter.
68
     * @param string $rule             The rule to apply.
69
     * @param array  $parameters       The parameters used in the rule.
70
     * @param array  $executionContext The execution context.
71
     *
72
     * @return \Traversable The filtered target.
73
     */
74 View Code Duplication
    public function filter($target, $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...
75
    {
76
        $parameters = $this->normalizeParameters($parameters);
77
        $targetCompiler = $this->findTargetCompiler($target, CompilationTarget::MODE_FILTER);
78
        $executor       = $this->compiler->compile($rule, $targetCompiler);
79
80
        return $executor->filter($target, $parameters, $targetCompiler->getOperators(), new ExecutionContext($executionContext));
81
    }
82
83
    /**
84
     * Filters a target using the given specification.
85
     * The targetCompiler to use is determined at runtime using the registered ones.
86
     *
87
     * @param mixed         $target           The target to filter.
88
     * @param Specification $spec             The specification to apply.
89
     * @param array         $executionContext The execution context.
90
     *
91
     * @return mixed The filtered target.
92
     */
93
    public function filterSpec($target, Specification $spec, array $executionContext = [])
94
    {
95
        return $this->filter($target, $spec->getRule(), $spec->getParameters(), $executionContext);
96
    }
97
98
    /**
99
     * Apply the filters on a target using the given specification.
100
     * The targetCompiler to use is determined at runtime using the registered ones.
101
     *
102
     * @param mixed         $target           The target to filter.
103
     * @param Specification $spec             The specification to apply.
104
     * @param array         $executionContext The execution context.
105
     */
106
    public function applyFilterSpec($target, Specification $spec, array $executionContext = [])
107
    {
108
        return $this->applyFilter($target, $spec->getRule(), $spec->getParameters(), $executionContext);
109
    }
110
111
    /**
112
     * Tells if a target satisfies the given rule and parameters.
113
     * The target compiler to use is determined at runtime using the registered ones.
114
     *
115
     * @param mixed  $target           The target.
116
     * @param string $rule             The rule to test.
117
     * @param array  $parameters       The parameters used in the rule.
118
     * @param array  $executionContext The execution context.
119
     *
120
     * @return boolean
121
     */
122 View Code Duplication
    public function satisfies($target, $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...
123
    {
124
        $targetCompiler = $this->findTargetCompiler($target, CompilationTarget::MODE_SATISFIES);
125
        $executor       = $this->compiler->compile($rule, $targetCompiler);
126
127
        return $executor->satisfies($target, $parameters, $targetCompiler->getOperators(), new ExecutionContext($executionContext));
128
    }
129
130
    /**
131
     * Tells if a target satisfies the given specification.
132
     * The target compiler to use is determined at runtime using the registered ones.
133
     *
134
     * @param mixed         $target           The target.
135
     * @param Specification $spec             The specification to use.
136
     * @param array         $executionContext The execution context.
137
     *
138
     * @return boolean
139
     */
140
    public function satisfiesSpec($target, Specification $spec, array $executionContext = [])
141
    {
142
        return $this->satisfies($target, $spec->getRule(), $spec->getParameters(), $executionContext);
143
    }
144
145
    /**
146
     * Finds a target compiler supporting the given target.
147
     *
148
     * @param mixed  $target The target to filter.
149
     * @param string $mode   The execution mode (MODE_FILTER or MODE_SATISFIES).
150
     *
151
     * @throws TargetUnsupportedException
152
     *
153
     * @return CompilationTarget
154
     */
155
    private function findTargetCompiler($target, $mode)
156
    {
157
        /** @var CompilationTarget $targetCompiler */
158
        foreach ($this->compilationTargets as $targetCompiler) {
159
            if ($targetCompiler->supports($target, $mode)) {
160
                return $targetCompiler;
161
            }
162
        }
163
164
        throw new TargetUnsupportedException('The given target is not supported.');
165
    }
166
    
167
    /**
168
     * Ensures positional parameters are prefixed with a string
169
     * 
170
     * @param array $params The parameters to normalize
171
     * 
172
     * @return array
173
     */
174
    private function normalizeParameters(array $params) 
175
    {
176
        $normalizedParams = [];
177
        $prefix = '_';
178
        foreach ($params as $key => $value) {
179
            if (is_int($key)) {
180
                $key = $prefix.(string) $key;
181
            }
182
            $normalizedParams[$key] = $value;
183
        }
184
        return $normalizedParams;
185
    }
186
}
187