1 | <?php |
||
23 | class ColumnRenderer |
||
24 | { |
||
25 | /** |
||
26 | * Render columns in table based on string definition. |
||
27 | * |
||
28 | * Example: |
||
29 | * renderColumns([ |
||
30 | * 'id' => 'primary', |
||
31 | * 'time' => 'datetime, nullable', |
||
32 | * 'status' => 'enum(active, disabled)' |
||
33 | * ], [ |
||
34 | * 'status' => 'active', |
||
35 | * 'time' => null, //idential as if define column as null |
||
36 | * ], $table); |
||
37 | * |
||
38 | * Attention, new table instance will be returned! |
||
39 | * |
||
40 | * @param array $fields |
||
41 | * @param array $defaults |
||
42 | * @param AbstractTable $table |
||
43 | * |
||
44 | * @return AbstractTable |
||
45 | * |
||
46 | * @throws ColumnRenderException |
||
47 | */ |
||
48 | public function renderColumns( |
||
67 | |||
68 | /** |
||
69 | * Cast (specify) column schema based on provided column definition and default value. |
||
70 | * Spiral will force default values (internally) for every NOT NULL column except primary keys! |
||
71 | * |
||
72 | * Column definition are compatible with database Migrations and AbstractColumn types. |
||
73 | * |
||
74 | * Column definition examples (by default all columns has flag NOT NULL): |
||
75 | * protected $schema = [ |
||
76 | * 'id' => 'primary', |
||
77 | * 'name' => 'string', //Default length is 255 characters. |
||
78 | * 'email' => 'string(255), nullable', //Can be NULL |
||
79 | * 'status' => 'enum(active, pending, disabled)', //Enum values, trimmed |
||
80 | * 'balance' => 'decimal(10, 2)', |
||
81 | * 'message' => 'text, null', //Alias for nullable |
||
82 | * 'time_expired' => 'timestamp' |
||
83 | * ]; |
||
84 | * |
||
85 | * Attention, column state will be affected! |
||
86 | * |
||
87 | * @see AbstractColumn |
||
88 | * |
||
89 | * @todo convert column type exception into definition |
||
90 | * |
||
91 | * @param AbstractColumn $column |
||
92 | * @param string $definition |
||
93 | * @param bool $hasDefault Must be set to true if default value was set by user. |
||
94 | * @param mixed $default Default value declared by record schema. |
||
95 | * |
||
96 | * @return mixed |
||
97 | * |
||
98 | * @throws DefinitionException |
||
99 | */ |
||
100 | public function declareColumn( |
||
168 | |||
169 | /** |
||
170 | * Cast default value based on column type. Required to prevent conflicts when not nullable |
||
171 | * column added to existed table with data in. |
||
172 | * |
||
173 | * @param AbstractColumn $column |
||
174 | * |
||
175 | * @return mixed |
||
176 | */ |
||
177 | public function castDefault(AbstractColumn $column) |
||
199 | } |
As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next
break
.There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.