1 | <?php |
||
2 | |||
3 | namespace HexMakina\Crudites\Schema; |
||
4 | |||
5 | use HexMakina\BlackBox\Database\{SchemaInterface, SchemaAttributeInterface}; |
||
0 ignored issues
–
show
The type
HexMakina\BlackBox\Datab...chemaAttributeInterface was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
6 | |||
7 | class SchemaAttribute implements SchemaAttributeInterface |
||
8 | { |
||
9 | private array $column; |
||
10 | |||
11 | /** |
||
12 | * @param SchemaInterface $schema The schema to which the column belongs. |
||
13 | * @param string $table The table to which the column belongs. |
||
14 | * @param string $column The column name. |
||
15 | * |
||
16 | * @throws \InvalidArgumentException If the column does not exist. |
||
17 | */ |
||
18 | public function __construct(SchemaInterface $schema, string $table, string $column) |
||
19 | { |
||
20 | $this->column = $schema->column($table, $column); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * @return bool True if the column is nullable, false otherwise. |
||
25 | */ |
||
26 | public function nullable(): bool |
||
27 | { |
||
28 | return !empty($this->column['nullable']); |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @return mixed The default value of the column, or null if no default is set. |
||
33 | */ |
||
34 | public function default() |
||
35 | { |
||
36 | return $this->column['default'] ?? null; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @return array<string> The possible values of the column, or an empty array if the column is not an enum. |
||
41 | */ |
||
42 | public function enums(): array |
||
43 | { |
||
44 | $ret = []; |
||
45 | |||
46 | $rx = '/enum\(\'(.+)\'\)/i'; |
||
47 | $column_type = $this->column['column_type']; |
||
48 | |||
49 | $m = []; |
||
50 | if (preg_match($rx, $column_type, $m) === 1) { |
||
51 | $ret = explode("','", $m[1]); |
||
52 | } |
||
53 | |||
54 | return $ret; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return The SchemaInterface type of the column. |
||
0 ignored issues
–
show
The type
HexMakina\Crudites\Schema\The was not found. Maybe you did not declare it correctly or list all dependencies?
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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
59 | * |
||
60 | * @see https://dev.mysql.com/doc/refman/8.0/en/data-types.html |
||
61 | */ |
||
62 | public function type(): ?string |
||
63 | { |
||
64 | |||
65 | return $this->column['type']; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @return int The length of the column, or -1 if the column is not a string. |
||
70 | * |
||
71 | * @see https://dev.mysql.com/doc/refman/8.0/en/char.html |
||
72 | */ |
||
73 | public function length(): ?int |
||
74 | { |
||
75 | return $this->column['length'] ?? null; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return int The precision of the column, or -1 if the column is not numeric. |
||
80 | * |
||
81 | * @see https://dev.mysql.com/doc/refman/8.0/en/precision-math-decimal-characteristics.html |
||
82 | */ |
||
83 | public function precision(): ?int |
||
84 | { |
||
85 | return $this->column['precision'] ?? null; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return int The scale of the column, or nullif the column is not numeric. |
||
90 | * |
||
91 | * @see https://dev.mysql.com/doc/refman/8.0/en/precision-math-decimal-characteristics.html |
||
92 | */ |
||
93 | public function scale(): ?int |
||
94 | { |
||
95 | return $this->column['scale'] ?? null; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @return bool True if the column is auto-incremented, false otherwise. |
||
100 | * |
||
101 | * @see https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html |
||
102 | */ |
||
103 | public function isAuto(): bool |
||
104 | { |
||
105 | return !empty($this->column['auto_increment']); |
||
106 | } |
||
107 | |||
108 | public function validateValue($value = null): ?string |
||
109 | { |
||
110 | if ($value !== null) { |
||
111 | $error = $this->validateValueWithType($value); |
||
112 | if (!empty($error)) { |
||
113 | return $error; |
||
114 | } |
||
115 | } else if (!$this->nullable() && $this->default() === null) { |
||
116 | return 'ERR_REQUIRED_VALUE'; |
||
117 | } |
||
118 | |||
119 | return null; |
||
120 | } |
||
121 | |||
122 | public function validateValueWithType($value = null): ?string |
||
123 | { |
||
124 | switch ($this->type()) { |
||
125 | case SchemaAttributeInterface::TYPE_DATE: |
||
126 | case SchemaAttributeInterface::TYPE_TIME: |
||
127 | case SchemaAttributeInterface::TYPE_TIMESTAMP: |
||
128 | case SchemaAttributeInterface::TYPE_DATETIME: |
||
129 | if (date_create($value) === false) { |
||
130 | return 'ERR_DATETIME_FORMAT'; |
||
131 | } |
||
132 | break; |
||
133 | |||
134 | case SchemaAttributeInterface::TYPE_YEAR: |
||
135 | if (preg_match('#^\d{4}$#', $value) !== 1) { |
||
136 | return 'ERR_YEAR_FORMAT'; |
||
137 | } |
||
138 | break; |
||
139 | |||
140 | case SchemaAttributeInterface::TYPE_INTEGER: |
||
141 | case SchemaAttributeInterface::TYPE_FLOAT: |
||
142 | case SchemaAttributeInterface::TYPE_DECIMAL: |
||
143 | if (!is_numeric($value)) { |
||
144 | return 'ERR_NUMERIC_FORMAT'; |
||
145 | } |
||
146 | break; |
||
147 | |||
148 | case SchemaAttributeInterface::TYPE_STRING: |
||
149 | case SchemaAttributeInterface::TYPE_TEXT: |
||
150 | if (strlen($value) > $this->length()) { |
||
151 | return 'ERR_TEXT_TOO_LONG'; |
||
152 | } |
||
153 | break; |
||
154 | |||
155 | case SchemaAttributeInterface::TYPE_ENUM: |
||
156 | if (!in_array($value, $this->enums())) { |
||
157 | return 'ERR_INVALID_ENUM_VALUE'; |
||
158 | } |
||
159 | break; |
||
160 | |||
161 | default: |
||
162 | return null; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 |
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