-
Notifications
You must be signed in to change notification settings - Fork 7
/
RuleInvoker.php
144 lines (131 loc) · 3.68 KB
/
RuleInvoker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 3.2.12
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Datasource;
/**
* Contains logic for invoking an application rule.
*
* Combined with Cake\Datasource\RuleChecker as an implementation
* detail to de-duplicate rule decoration and provide cleaner separation
* of duties.
*
* @internal
*/
class RuleInvoker
{
/**
* The rule name
*
* @var string|null
*/
protected $name;
/**
* Rule options
*
* @var array
*/
protected $options = [];
/**
* Rule callable
*
* @var callable
*/
protected $rule;
/**
* Constructor
*
* ### Options
*
* - `errorField` The field errors should be set onto.
* - `message` The error message.
*
* Individual rules may have additional options that can be
* set here. Any options will be passed into the rule as part of the
* rule $scope.
*
* @param callable $rule The rule to be invoked.
* @param ?string $name The name of the rule. Used in error messsages.
* @param array $options The options for the rule. See above.
*/
public function __construct(callable $rule, ?string $name, array $options = [])
{
$this->rule = $rule;
$this->name = $name;
$this->options = $options;
}
/**
* Set options for the rule invocation.
*
* Old options will be merged with the new ones.
*
* @param array $options The options to set.
* @return $this
*/
public function setOptions(array $options)
{
$this->options = $options + $this->options;
return $this;
}
/**
* Set the rule name.
*
* Only truthy names will be set.
*
* @param string|null $name The name to set.
* @return $this
*/
public function setName(?string $name)
{
if ($name) {
$this->name = $name;
}
return $this;
}
/**
* Invoke the rule.
*
* @param \Cake\Datasource\EntityInterface $entity The entity the rule
* should apply to.
* @param array $scope The rule's scope/options.
* @return bool Whether or not the rule passed.
*/
public function __invoke(EntityInterface $entity, array $scope): bool
{
$rule = $this->rule;
$pass = $rule($entity, $this->options + $scope);
if ($pass === true || empty($this->options['errorField'])) {
return $pass === true;
}
$message = 'invalid';
if (isset($this->options['message'])) {
$message = $this->options['message'];
}
if (is_string($pass)) {
$message = $pass;
}
if ($this->name) {
$message = [$this->name => $message];
} else {
$message = [$message];
}
$errorField = $this->options['errorField'];
$entity->setError($errorField, $message);
if ($entity instanceof InvalidPropertyInterface && isset($entity->{$errorField})) {
$invalidValue = $entity->{$errorField};
$entity->setInvalidField($errorField, $invalidValue);
}
return $pass === true;
}
}