-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
ExpressionTypeCasterTrait.php
80 lines (70 loc) · 2.43 KB
/
ExpressionTypeCasterTrait.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
<?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.3.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Database\Type;
use Cake\Database\TypeFactory;
/**
* Offers a method to convert values to ExpressionInterface objects
* if the type they should be converted to implements ExpressionTypeInterface
*/
trait ExpressionTypeCasterTrait
{
/**
* Conditionally converts the passed value to an ExpressionInterface object
* if the type class implements the ExpressionTypeInterface. Otherwise,
* returns the value unmodified.
*
* @param mixed $value The value to convert to ExpressionInterface
* @param string|null $type The type name
* @return mixed
*/
protected function _castToExpression(mixed $value, ?string $type = null): mixed
{
if ($type === null) {
return $value;
}
$baseType = str_replace('[]', '', $type);
$converter = TypeFactory::build($baseType);
if (!$converter instanceof ExpressionTypeInterface) {
return $value;
}
$multi = $type !== $baseType;
if ($multi) {
/** @var \Cake\Database\Type\ExpressionTypeInterface $converter */
return array_map([$converter, 'toExpression'], $value);
}
return $converter->toExpression($value);
}
/**
* Returns an array with the types that require values to
* be casted to expressions, out of the list of type names
* passed as parameter.
*
* @param array $types List of type names
* @return array
*/
protected function _requiresToExpressionCasting(array $types): array
{
$result = [];
$types = array_filter($types);
foreach ($types as $k => $type) {
$object = TypeFactory::build($type);
if ($object instanceof ExpressionTypeInterface) {
$result[$k] = $object;
}
}
return $result;
}
}