Skip to content

Commit 4563aae

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.4
2 parents d97fded + e31579e commit 4563aae

File tree

10 files changed

+281
-165
lines changed

10 files changed

+281
-165
lines changed

system/Config/Factories.php

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* instantiation checks.
2525
*
2626
* @method static BaseConfig|null config(...$arguments)
27+
* @method static Model|null models(string $name, array $options = [], ?ConnectionInterface &$conn = null)
2728
*/
2829
class Factories
2930
{
@@ -69,23 +70,6 @@ class Factories
6970
*/
7071
protected static $instances = [];
7172

72-
/**
73-
* This method is only to prevent PHPStan error.
74-
* If we have a solution, we can remove this method.
75-
* See https://github.com/codeigniter4/CodeIgniter4/pull/5358
76-
*
77-
* @template T of Model
78-
*
79-
* @phpstan-param class-string<T> $name
80-
*
81-
* @return Model
82-
* @phpstan-return T
83-
*/
84-
public static function models(string $name, array $options = [], ?ConnectionInterface &$conn = null)
85-
{
86-
return self::__callStatic('models', [$name, $options, $conn]);
87-
}
88-
8973
/**
9074
* Loads instances based on the method component name. Either
9175
* creates a new instance or returns an existing shared instance.

system/Database/BaseBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1997,7 +1997,7 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
19971997
}
19981998

19991999
if (isset($this->QBOptions['setQueryAsData'])) {
2000-
$data = $this->QBOptions['setQueryAsData'];
2000+
$data = $this->QBOptions['setQueryAsData'] . "\n";
20012001
} else {
20022002
$data = 'VALUES ' . implode(', ', $this->formatValues($values)) . "\n";
20032003
}

system/Validation/Rules.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ public function required_with($str = null, ?string $fields = null, array $data =
263263
// If the field is present we can safely assume that
264264
// the field is here, no matter whether the corresponding
265265
// search field is present or not.
266-
$fields = explode(',', $fields);
267266
$present = $this->required($str ?? '');
268267

269268
if ($present) {
@@ -272,11 +271,14 @@ public function required_with($str = null, ?string $fields = null, array $data =
272271

273272
// Still here? Then we fail this test if
274273
// any of the fields are present in $data
275-
// as $fields is the lis
274+
// as $fields is the list
276275
$requiredFields = [];
277276

278-
foreach ($fields as $field) {
279-
if ((array_key_exists($field, $data) && ! empty($data[$field])) || (strpos($field, '.') !== false && ! empty(dot_array_search($field, $data)))) {
277+
foreach (explode(',', $fields) as $field) {
278+
if (
279+
(array_key_exists($field, $data) && ! empty($data[$field]))
280+
|| (strpos($field, '.') !== false && ! empty(dot_array_search($field, $data)))
281+
) {
280282
$requiredFields[] = $field;
281283
}
282284
}
@@ -285,7 +287,7 @@ public function required_with($str = null, ?string $fields = null, array $data =
285287
}
286288

287289
/**
288-
* The field is required when all of the other fields are present
290+
* The field is required when all the other fields are present
289291
* in the data but not required.
290292
*
291293
* Example (field is required when the id or email field is missing):
@@ -296,28 +298,36 @@ public function required_with($str = null, ?string $fields = null, array $data =
296298
* @param string|null $otherFields The param fields of required_without[].
297299
* @param string|null $field This rule param fields aren't present, this field is required.
298300
*/
299-
public function required_without($str = null, ?string $otherFields = null, array $data = [], ?string $error = null, ?string $field = null): bool
300-
{
301+
public function required_without(
302+
$str = null,
303+
?string $otherFields = null,
304+
array $data = [],
305+
?string $error = null,
306+
?string $field = null
307+
): bool {
301308
if ($otherFields === null || empty($data)) {
302309
throw new InvalidArgumentException('You must supply the parameters: otherFields, data.');
303310
}
304311

305312
// If the field is present we can safely assume that
306313
// the field is here, no matter whether the corresponding
307314
// search field is present or not.
308-
$otherFields = explode(',', $otherFields);
309-
$present = $this->required($str ?? '');
315+
$present = $this->required($str ?? '');
310316

311317
if ($present) {
312318
return true;
313319
}
314320

315321
// Still here? Then we fail this test if
316322
// any of the fields are not present in $data
317-
foreach ($otherFields as $otherField) {
318-
if ((strpos($otherField, '.') === false) && (! array_key_exists($otherField, $data) || empty($data[$otherField]))) {
323+
foreach (explode(',', $otherFields) as $otherField) {
324+
if (
325+
(strpos($otherField, '.') === false)
326+
&& (! array_key_exists($otherField, $data) || empty($data[$otherField]))
327+
) {
319328
return false;
320329
}
330+
321331
if (strpos($otherField, '.') !== false) {
322332
if ($field === null) {
323333
throw new InvalidArgumentException('You must supply the parameters: field.');

system/Validation/StrictRules/Rules.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ public function required_with($str = null, ?string $fields = null, array $data =
347347
}
348348

349349
/**
350-
* The field is required when all of the other fields are present
350+
* The field is required when all the other fields are present
351351
* in the data but not required.
352352
*
353353
* Example (field is required when the id or email field is missing):
@@ -358,8 +358,13 @@ public function required_with($str = null, ?string $fields = null, array $data =
358358
* @param string|null $otherFields The param fields of required_without[].
359359
* @param string|null $field This rule param fields aren't present, this field is required.
360360
*/
361-
public function required_without($str = null, ?string $otherFields = null, array $data = [], ?string $error = null, ?string $field = null): bool
362-
{
361+
public function required_without(
362+
$str = null,
363+
?string $otherFields = null,
364+
array $data = [],
365+
?string $error = null,
366+
?string $field = null
367+
): bool {
363368
return $this->nonStrictRules->required_without($str, $otherFields, $data, $error, $field);
364369
}
365370
}

system/Validation/Validation.php

Lines changed: 96 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -248,87 +248,30 @@ public function getValidated(): array
248248
* so that we can collect all of the first errors.
249249
*
250250
* @param array|string $value
251-
* @param array|null $rules
252-
* @param array|null $data The array of data to validate, with `DBGroup`.
251+
* @param array $rules
252+
* @param array $data The array of data to validate, with `DBGroup`.
253253
* @param string|null $originalField The original asterisk field name like "foo.*.bar".
254254
*/
255255
protected function processRules(
256256
string $field,
257257
?string $label,
258258
$value,
259-
$rules = null,
260-
?array $data = null,
259+
$rules = null, // @TODO remove `= null`
260+
?array $data = null, // @TODO remove `= null`
261261
?string $originalField = null
262262
): bool {
263263
if ($data === null) {
264264
throw new InvalidArgumentException('You must supply the parameter: data.');
265265
}
266266

267-
if (in_array('if_exist', $rules, true)) {
268-
$flattenedData = array_flatten_with_dots($data);
269-
$ifExistField = $field;
270-
271-
if (strpos($field, '.*') !== false) {
272-
// We'll change the dot notation into a PCRE pattern that can be used later
273-
$ifExistField = str_replace('\.\*', '\.(?:[^\.]+)', preg_quote($field, '/'));
274-
$dataIsExisting = false;
275-
$pattern = sprintf('/%s/u', $ifExistField);
276-
277-
foreach (array_keys($flattenedData) as $item) {
278-
if (preg_match($pattern, $item) === 1) {
279-
$dataIsExisting = true;
280-
break;
281-
}
282-
}
283-
} else {
284-
$dataIsExisting = array_key_exists($ifExistField, $flattenedData);
285-
}
286-
287-
unset($ifExistField, $flattenedData);
288-
289-
if (! $dataIsExisting) {
290-
// we return early if `if_exist` is not satisfied. we have nothing to do here.
291-
return true;
292-
}
293-
294-
// Otherwise remove the if_exist rule and continue the process
295-
$rules = array_filter($rules, static fn ($rule) => $rule instanceof Closure || $rule !== 'if_exist');
267+
$rules = $this->processIfExist($field, $rules, $data);
268+
if ($rules === true) {
269+
return true;
296270
}
297271

298-
if (in_array('permit_empty', $rules, true)) {
299-
if (
300-
! in_array('required', $rules, true)
301-
&& (is_array($value) ? $value === [] : trim((string) $value) === '')
302-
) {
303-
$passed = true;
304-
305-
foreach ($rules as $rule) {
306-
if (! $this->isClosure($rule) && preg_match('/(.*?)\[(.*)\]/', $rule, $match)) {
307-
$rule = $match[1];
308-
$param = $match[2];
309-
310-
if (! in_array($rule, ['required_with', 'required_without'], true)) {
311-
continue;
312-
}
313-
314-
// Check in our rulesets
315-
foreach ($this->ruleSetInstances as $set) {
316-
if (! method_exists($set, $rule)) {
317-
continue;
318-
}
319-
320-
$passed = $passed && $set->{$rule}($value, $param, $data);
321-
break;
322-
}
323-
}
324-
}
325-
326-
if ($passed === true) {
327-
return true;
328-
}
329-
}
330-
331-
$rules = array_filter($rules, static fn ($rule) => $rule instanceof Closure || $rule !== 'permit_empty');
272+
$rules = $this->processPermitEmpty($value, $rules, $data);
273+
if ($rules === true) {
274+
return true;
332275
}
333276

334277
foreach ($rules as $i => $rule) {
@@ -404,6 +347,92 @@ protected function processRules(
404347
return true;
405348
}
406349

350+
/**
351+
* @param array $data The array of data to validate, with `DBGroup`.
352+
*
353+
* @return array|true The modified rules or true if we return early
354+
*/
355+
private function processIfExist(string $field, array $rules, array $data)
356+
{
357+
if (in_array('if_exist', $rules, true)) {
358+
$flattenedData = array_flatten_with_dots($data);
359+
$ifExistField = $field;
360+
361+
if (strpos($field, '.*') !== false) {
362+
// We'll change the dot notation into a PCRE pattern that can be used later
363+
$ifExistField = str_replace('\.\*', '\.(?:[^\.]+)', preg_quote($field, '/'));
364+
$dataIsExisting = false;
365+
$pattern = sprintf('/%s/u', $ifExistField);
366+
367+
foreach (array_keys($flattenedData) as $item) {
368+
if (preg_match($pattern, $item) === 1) {
369+
$dataIsExisting = true;
370+
break;
371+
}
372+
}
373+
} else {
374+
$dataIsExisting = array_key_exists($ifExistField, $flattenedData);
375+
}
376+
377+
if (! $dataIsExisting) {
378+
// we return early if `if_exist` is not satisfied. we have nothing to do here.
379+
return true;
380+
}
381+
382+
// Otherwise remove the if_exist rule and continue the process
383+
$rules = array_filter($rules, static fn ($rule) => $rule instanceof Closure || $rule !== 'if_exist');
384+
}
385+
386+
return $rules;
387+
}
388+
389+
/**
390+
* @param array|string $value
391+
* @param array $data The array of data to validate, with `DBGroup`.
392+
*
393+
* @return array|true The modified rules or true if we return early
394+
*/
395+
private function processPermitEmpty($value, array $rules, array $data)
396+
{
397+
if (in_array('permit_empty', $rules, true)) {
398+
if (
399+
! in_array('required', $rules, true)
400+
&& (is_array($value) ? $value === [] : trim((string) $value) === '')
401+
) {
402+
$passed = true;
403+
404+
foreach ($rules as $rule) {
405+
if (! $this->isClosure($rule) && preg_match('/(.*?)\[(.*)\]/', $rule, $match)) {
406+
$rule = $match[1];
407+
$param = $match[2];
408+
409+
if (! in_array($rule, ['required_with', 'required_without'], true)) {
410+
continue;
411+
}
412+
413+
// Check in our rulesets
414+
foreach ($this->ruleSetInstances as $set) {
415+
if (! method_exists($set, $rule)) {
416+
continue;
417+
}
418+
419+
$passed = $passed && $set->{$rule}($value, $param, $data);
420+
break;
421+
}
422+
}
423+
}
424+
425+
if ($passed === true) {
426+
return true;
427+
}
428+
}
429+
430+
$rules = array_filter($rules, static fn ($rule) => $rule instanceof Closure || $rule !== 'permit_empty');
431+
}
432+
433+
return $rules;
434+
}
435+
407436
/**
408437
* @param Closure|string $rule
409438
*/

system/View/Cell.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ protected function determineClass(string $library): array
175175
}
176176

177177
// locate and return an instance of the cell
178-
$class = Factories::cells($class);
178+
$object = Factories::cells($class);
179179

180-
if (! is_object($class)) {
180+
if (! is_object($object)) {
181181
throw ViewException::forInvalidCellClass($class);
182182
}
183183

@@ -186,7 +186,7 @@ protected function determineClass(string $library): array
186186
}
187187

188188
return [
189-
$class,
189+
$object,
190190
$method,
191191
];
192192
}

0 commit comments

Comments
 (0)