1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql\Column; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Expression\ArrayExpression; |
8
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
9
|
|
|
use Yiisoft\Db\Pgsql\ArrayParser; |
10
|
|
|
use Yiisoft\Db\Pgsql\Schema; |
11
|
|
|
use Yiisoft\Db\Schema\Column\AbstractColumnSchema; |
|
|
|
|
12
|
|
|
use Yiisoft\Db\Schema\Column\ColumnSchemaInterface; |
|
|
|
|
13
|
|
|
use Yiisoft\Db\Schema\Column\DoubleColumnSchema; |
|
|
|
|
14
|
|
|
use Yiisoft\Db\Schema\Column\JsonColumnSchema; |
|
|
|
|
15
|
|
|
use Yiisoft\Db\Schema\Column\StringColumnSchema; |
|
|
|
|
16
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
17
|
|
|
|
18
|
|
|
use function array_map; |
19
|
|
|
use function array_walk_recursive; |
20
|
|
|
use function is_array; |
21
|
|
|
use function is_iterable; |
22
|
|
|
use function is_string; |
23
|
|
|
|
24
|
|
|
final class ArrayColumnSchema extends AbstractColumnSchema |
25
|
|
|
{ |
26
|
|
|
private ColumnSchemaInterface|null $column = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int The dimension of array, must be greater than 0. |
30
|
|
|
*/ |
31
|
|
|
private int $dimension = 1; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set dimension of an array, must be greater than 0. |
35
|
|
|
*/ |
36
|
|
|
public function dimension(int $dimension): void |
37
|
|
|
{ |
38
|
|
|
$this->dimension = $dimension; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function getColumn(): ColumnSchemaInterface |
42
|
|
|
{ |
43
|
|
|
if ($this->column === null) { |
44
|
|
|
if ($this->getType() === Schema::TYPE_BIT) { |
45
|
|
|
$this->column = new BitColumnSchema($this->getName()); |
46
|
|
|
$this->column->size($this->getSize()); |
|
|
|
|
47
|
|
|
} else { |
48
|
|
|
$this->column = match ($this->getPhpType()) { |
49
|
|
|
SchemaInterface::PHP_TYPE_INTEGER => new IntegerColumnSchema($this->getName()), |
50
|
|
|
SchemaInterface::PHP_TYPE_DOUBLE => new DoubleColumnSchema($this->getName()), |
51
|
|
|
SchemaInterface::PHP_TYPE_BOOLEAN => new BooleanColumnSchema($this->getName()), |
52
|
|
|
SchemaInterface::PHP_TYPE_RESOURCE => new BinaryColumnSchema($this->getName()), |
53
|
|
|
SchemaInterface::PHP_TYPE_ARRAY => new JsonColumnSchema($this->getName()), |
54
|
|
|
default => new StringColumnSchema($this->getName()), |
55
|
|
|
}; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->column; |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return int Get the dimension of the array. |
64
|
|
|
*/ |
65
|
|
|
public function getDimension(): int |
66
|
|
|
{ |
67
|
|
|
return $this->dimension; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function dbTypecast(mixed $value): ExpressionInterface|null |
71
|
|
|
{ |
72
|
|
|
if ($value === null || $value instanceof ExpressionInterface) { |
73
|
|
|
return $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($this->dimension === 1 && is_array($value)) { |
77
|
|
|
$value = array_map([$this->getColumn(), 'dbTypecast'], $value); |
78
|
|
|
} else { |
79
|
|
|
$value = $this->dbTypecastArray($value, $this->dimension); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return new ArrayExpression($value, $this->getDbType(), $this->dimension); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Recursively converts array values for use in a db query. |
87
|
|
|
* |
88
|
|
|
* @param mixed $value The array or iterable object. |
89
|
|
|
* @param int $dimension The array dimension. Should be more than 0. |
90
|
|
|
* |
91
|
|
|
* @return array Converted values. |
92
|
|
|
*/ |
93
|
|
|
private function dbTypecastArray(mixed $value, int $dimension): array |
94
|
|
|
{ |
95
|
|
|
if (!is_iterable($value)) { |
96
|
|
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$items = []; |
100
|
|
|
$column = $this->getColumn(); |
101
|
|
|
|
102
|
|
|
/** @psalm-var mixed $val */ |
103
|
|
|
foreach ($value as $val) { |
104
|
|
|
if ($dimension > 1) { |
105
|
|
|
$items[] = $this->dbTypecastArray($val, $dimension - 1); |
106
|
|
|
} else { |
107
|
|
|
/** @psalm-var mixed */ |
108
|
|
|
$items[] = $column->dbTypecast($val); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $items; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function phpTypecast(mixed $value): array|null |
116
|
|
|
{ |
117
|
|
|
if (is_string($value)) { |
118
|
|
|
$value = (new ArrayParser())->parse($value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if (!is_array($value)) { |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if ($this->getType() === SchemaInterface::TYPE_STRING) { |
126
|
|
|
return $value; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$column = $this->getColumn(); |
130
|
|
|
|
131
|
|
|
if ($this->dimension === 1 && $this->getType() !== SchemaInterface::TYPE_JSON) { |
132
|
|
|
return array_map([$column, 'phpTypecast'], $value); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
array_walk_recursive($value, function (string|null &$val) use ($column): void { |
136
|
|
|
/** @psalm-var mixed $val */ |
137
|
|
|
$val = $column->phpTypecast($val); |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
return $value; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths