Total Complexity | 11 |
Total Lines | 74 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php declare(strict_types=1); |
||
10 | class Prop |
||
11 | { |
||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | public $name; |
||
16 | |||
17 | /** |
||
18 | * @var string|Datatype |
||
19 | */ |
||
20 | public $type = ''; |
||
21 | |||
22 | /** |
||
23 | * @var bool |
||
24 | */ |
||
25 | public $required; |
||
26 | |||
27 | /** |
||
28 | * @var mixed |
||
29 | */ |
||
30 | public $default; |
||
31 | |||
32 | /** |
||
33 | * @param string $name |
||
34 | * @param string $code |
||
35 | * @param string $type |
||
36 | * @param mixed $default |
||
37 | */ |
||
38 | public function __construct(string $name, $type = '', bool $required = false, $default = null) |
||
39 | { |
||
40 | $this->name = $name; |
||
41 | $this->type = $type; |
||
42 | $this->required = $required; |
||
43 | $this->default = $default; |
||
44 | } |
||
45 | |||
46 | public function toStruct(): string |
||
47 | { |
||
48 | $t = VueCode::mapTypeToJS($this->type); |
||
49 | return "\"{$this->name}\": {\n". |
||
50 | " type: {$t}" . |
||
51 | ($this->required ?? false ? ", required: true" : '') . |
||
52 | ($this->default ?? false ? ", default: " . $this->default : '') . |
||
53 | " } "; |
||
54 | } |
||
55 | |||
56 | public function typeAsString(): string |
||
57 | { |
||
58 | if (is_string($this->type)) { |
||
59 | return $this->type; |
||
60 | } else { |
||
61 | return $this->type->getName(); |
||
62 | } |
||
63 | } |
||
64 | |||
65 | public function toDecorator(): string |
||
66 | { |
||
67 | $t = VueCode::mapTypeToJS($this->type); |
||
|
|||
68 | $options = []; |
||
69 | if ($this->default) { |
||
70 | $options["default"] = $this->default; |
||
71 | } |
||
72 | return "@Prop(" . |
||
73 | ($options ? json_encode($options, JSON_PRETTY_PRINT) : '{}') . |
||
74 | ') readonly ' . |
||
75 | $this->name . |
||
76 | ($this->required ? '!' : '') . |
||
77 | ': ' . |
||
78 | $this->typeAsString() . ";\n"; |
||
79 | } |
||
80 | |||
81 | public function toBind(): string |
||
84 | } |
||
85 | } |
||
86 |