|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Console\Library\Printer\Model; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Console\Library\Printer\Model\Abstracts\AbstractClassPrinter; |
|
6
|
|
|
use Leonidas\Contracts\System\Model\SetAccessProviderInterface; |
|
7
|
|
|
use Leonidas\Library\System\Model\Abstracts\DatableAccessProviderTrait; |
|
8
|
|
|
use Leonidas\Library\System\Model\SetAccessProvider; |
|
9
|
|
|
use Nette\PhpGenerator\PhpNamespace; |
|
10
|
|
|
|
|
11
|
|
|
class ModelSetAccessProviderPrinter extends AbstractClassPrinter |
|
12
|
|
|
{ |
|
13
|
|
|
protected string $model; |
|
14
|
|
|
|
|
15
|
|
|
protected string $single; |
|
16
|
|
|
|
|
17
|
|
|
protected bool $isDatable; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(string $namespace, string $class, string $model, string $single, bool $isDatable = true) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct($namespace, $class); |
|
22
|
|
|
|
|
23
|
|
|
$this->model = $model; |
|
24
|
|
|
$this->single = $single; |
|
25
|
|
|
$this->isDatable = $isDatable; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function setupClass(PhpNamespace $namespace): object |
|
29
|
|
|
{ |
|
30
|
|
|
$base = SetAccessProvider::class; |
|
31
|
|
|
$contract = SetAccessProviderInterface::class; |
|
32
|
|
|
$datableTrait = DatableAccessProviderTrait::class; |
|
33
|
|
|
|
|
34
|
|
|
$class = $namespace |
|
35
|
|
|
->addUse($base) |
|
36
|
|
|
->addUse($contract) |
|
37
|
|
|
->addUse($this->model) |
|
38
|
|
|
->addClass($this->class); |
|
39
|
|
|
|
|
40
|
|
|
$class->addImplement($contract); |
|
41
|
|
|
$class->setExtends($base); |
|
42
|
|
|
|
|
43
|
|
|
if ($this->isDatable) { |
|
44
|
|
|
$namespace->addUse($datableTrait); |
|
45
|
|
|
$class->addTrait($datableTrait); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$class->addMethod('__construct') |
|
49
|
|
|
->setPublic() |
|
50
|
|
|
->setBody(sprintf( |
|
51
|
|
|
'parent::__construct($%s, $this->resolvedSetters($%s));', |
|
52
|
|
|
$this->single, |
|
53
|
|
|
$this->single |
|
54
|
|
|
)) |
|
55
|
|
|
->addParameter($this->single) |
|
56
|
|
|
->setType($this->model); |
|
57
|
|
|
|
|
58
|
|
|
$class->addMethod('resolvedSetters') |
|
59
|
|
|
->setPublic() |
|
60
|
|
|
->setReturnType('array') |
|
61
|
|
|
->setBody($this->getResolvedSettersMethodBody()) |
|
62
|
|
|
->addParameter($this->single) |
|
63
|
|
|
->setType($this->model); |
|
64
|
|
|
|
|
65
|
|
|
return $class; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getResolvedSettersMethodBody(): string |
|
69
|
|
|
{ |
|
70
|
|
|
$base = 'return [];'; |
|
71
|
|
|
$datable = trim($base, ';') . ' + $this->resolvedDatableSetters($%s);'; |
|
72
|
|
|
|
|
73
|
|
|
return $this->isDatable ? sprintf($datable, $this->single) : $base; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|