1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Formularium\Frontend\Vue\VueCode; |
4
|
|
|
|
5
|
|
|
use Formularium\Datatype; |
6
|
|
|
use Formularium\Frontend\Vue\VueCode; |
7
|
|
|
|
8
|
|
|
use function Safe\json_encode; |
9
|
|
|
|
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|Datatype $type |
35
|
|
|
* @param bool $required |
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 |
82
|
|
|
{ |
83
|
|
|
return 'v-bind:' . $this->name . '="model.' . $this->name . '"'; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|