|
10 | 10 | /** |
11 | 11 | * Parses the --fields option syntax: |
12 | 12 | * |
13 | | - * name:string, price:decimal:nullable, status:enum:int, sku:string:unique |
| 13 | + * name:string, price:decimal:nullable, status:enum:int, parent_id:int |
14 | 14 | * |
15 | | - * Segments after the type are modifiers (nullable, unique, required) or, for |
16 | | - * enum fields, a backing type (string|int — defaults to string). |
| 15 | + * Segments after the name are a type (or alias like int→integer), then |
| 16 | + * modifiers (nullable, unique, required). For enum fields, string|int |
| 17 | + * after `enum` sets the backing type (defaults to string). |
17 | 18 | */ |
18 | 19 | final class FieldParser |
19 | 20 | { |
20 | 21 | private const MODIFIERS = ['nullable', 'unique', 'required']; |
21 | 22 |
|
22 | 23 | private const ENUM_BACKINGS = ['string', 'int']; |
23 | 24 |
|
| 25 | + private const TYPE_ALIASES = [ |
| 26 | + 'int' => 'integer', |
| 27 | + 'bool' => 'boolean', |
| 28 | + 'bigint' => 'biginteger', |
| 29 | + ]; |
| 30 | + |
24 | 31 | /** |
25 | 32 | * @return list<Field> |
26 | 33 | */ |
@@ -58,29 +65,25 @@ private static function parseField(string $segment): Field |
58 | 65 | continue; |
59 | 66 | } |
60 | 67 |
|
61 | | - if (in_array($lower, self::ENUM_BACKINGS, true)) { |
| 68 | + // `status:enum:int` — int/string after enum is the backing type, |
| 69 | + // not a field-type rewrite of the already-chosen enum. |
| 70 | + if ($type === 'enum' && in_array($lower, self::ENUM_BACKINGS, true)) { |
62 | 71 | $enumBacking = $lower; |
63 | 72 |
|
64 | 73 | continue; |
65 | 74 | } |
66 | 75 |
|
67 | | - if (! Field::isSupportedType($lower)) { |
| 76 | + $resolved = self::TYPE_ALIASES[$lower] ?? $lower; |
| 77 | + |
| 78 | + if (! Field::isSupportedType($resolved)) { |
68 | 79 | throw new InvalidArgumentException(sprintf( |
69 | 80 | 'Unknown field type or modifier [%s] for field [%s].', |
70 | 81 | $part, |
71 | 82 | $name, |
72 | 83 | )); |
73 | 84 | } |
74 | 85 |
|
75 | | - $type = $lower; |
76 | | - } |
77 | | - |
78 | | - if ($enumBacking !== 'string' && $type !== 'enum') { |
79 | | - throw new InvalidArgumentException(sprintf( |
80 | | - 'Backing type [%s] is only valid for enum fields (field [%s]).', |
81 | | - $enumBacking, |
82 | | - $name, |
83 | | - )); |
| 86 | + $type = $resolved; |
84 | 87 | } |
85 | 88 |
|
86 | 89 | return new Field( |
|
0 commit comments