/
ExistsIn.php
168 lines (150 loc) · 5.4 KB
/
ExistsIn.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?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.0.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM\Rule;
use Cake\Datasource\EntityInterface;
use Cake\ORM\Association;
use Cake\ORM\Table;
use RuntimeException;
/**
* Checks that the value provided in a field exists as the primary key of another
* table.
*/
class ExistsIn
{
/**
* The list of fields to check
*
* @var array
*/
protected $_fields;
/**
* The repository where the field will be looked for
*
* @var \Cake\ORM\Table|\Cake\ORM\Association|string
*/
protected $_repository;
/**
* Options for the constructor
*
* @var array
*/
protected $_options = [];
/**
* Constructor.
*
* Available option for $options is 'allowNullableNulls' flag.
* Set to true to accept composite foreign keys where one or more nullable columns are null.
*
* @param string|array $fields The field or fields to check existence as primary key.
* @param \Cake\ORM\Table|\Cake\ORM\Association|string $repository The repository where the
* field will be looked for, or the association name for the repository.
* @param array $options The options that modify the rules behavior.
* Options 'allowNullableNulls' will make the rule pass if given foreign keys are set to `null`.
* Notice: allowNullableNulls cannot pass by database columns set to `NOT NULL`.
*/
public function __construct($fields, $repository, array $options = [])
{
$options += ['allowNullableNulls' => false];
$this->_options = $options;
$this->_fields = (array)$fields;
$this->_repository = $repository;
}
/**
* Performs the existence check
*
* @param \Cake\Datasource\EntityInterface $entity The entity from where to extract the fields
* @param array $options Options passed to the check,
* where the `repository` key is required.
* @throws \RuntimeException When the rule refers to an undefined association.
* @return bool
*/
public function __invoke(EntityInterface $entity, array $options): bool
{
if (is_string($this->_repository)) {
if (!$options['repository']->hasAssociation($this->_repository)) {
throw new RuntimeException(sprintf(
"ExistsIn rule for '%s' is invalid. '%s' is not associated with '%s'.",
implode(', ', $this->_fields),
$this->_repository,
get_class($options['repository'])
));
}
$repository = $options['repository']->getAssociation($this->_repository);
$this->_repository = $repository;
}
$fields = $this->_fields;
$source = $target = $this->_repository;
if ($target instanceof Association) {
$bindingKey = (array)$target->getBindingKey();
$realTarget = $target->getTarget();
} else {
$bindingKey = (array)$target->getPrimaryKey();
$realTarget = $target;
}
if (!empty($options['_sourceTable']) && $realTarget === $options['_sourceTable']) {
return true;
}
if (!empty($options['repository'])) {
$source = $options['repository'];
}
if ($source instanceof Association) {
$source = $source->getSource();
}
if (!$entity->extract($this->_fields, true)) {
return true;
}
if ($this->_fieldsAreNull($entity, $source)) {
return true;
}
if ($this->_options['allowNullableNulls']) {
$schema = $source->getSchema();
foreach ($fields as $i => $field) {
if ($schema->getColumn($field) && $schema->isNullable($field) && $entity->get($field) === null) {
unset($bindingKey[$i], $fields[$i]);
}
}
}
$primary = array_map(
function ($key) use ($target) {
return $target->aliasField($key) . ' IS';
},
$bindingKey
);
$conditions = array_combine(
$primary,
$entity->extract($fields)
);
return $target->exists($conditions);
}
/**
* Checks whether or not the given entity fields are nullable and null.
*
* @param \Cake\Datasource\EntityInterface $entity The entity to check.
* @param \Cake\ORM\Table $source The table to use schema from.
* @return bool
*/
protected function _fieldsAreNull(EntityInterface $entity, Table $source): bool
{
$nulls = 0;
$schema = $source->getSchema();
foreach ($this->_fields as $field) {
if ($schema->getColumn($field) && $schema->isNullable($field) && $entity->get($field) === null) {
$nulls++;
}
}
return $nulls === count($this->_fields);
}
}