|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of FlexPHP. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Freddie Gar <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
1 |
|
*/ |
|
10
|
|
|
namespace FlexPHP\Generator\Domain\Builders\UseCase; |
|
11
|
1 |
|
|
|
12
|
|
|
use FlexPHP\Generator\Domain\Builders\AbstractBuilder; |
|
13
|
|
|
use FlexPHP\Schema\SchemaAttributeInterface; |
|
14
|
1 |
|
use FlexPHP\Schema\SchemaInterface; |
|
15
|
|
|
|
|
16
|
1 |
|
final class UseCaseBuilder extends AbstractBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
private string $action; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(SchemaInterface $schema, string $action) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->action = $this->getInflector()->camelAction($action); |
|
23
|
|
|
|
|
24
|
|
|
$entity = $this->getInflector()->entity($schema->name()); |
|
25
|
|
|
$item = $this->getInflector()->item($schema->name()); |
|
26
|
|
|
$items = $this->getInflector()->items($schema->name()); |
|
27
|
|
|
$action = $this->getInflector()->pascalAction($action); |
|
28
|
|
|
$fkFns = $this->getFkFunctions($this->getFkCheck($schema->fkRelations())); |
|
29
|
|
|
$properties = \array_reduce( |
|
30
|
|
|
$schema->attributes(), |
|
31
|
|
|
function ($result, SchemaAttributeInterface $property) { |
|
32
|
|
|
$result[$this->getInflector()->camelProperty($property->name())] = $property->properties(); |
|
33
|
|
|
|
|
34
|
|
|
return $result; |
|
35
|
|
|
}, |
|
36
|
|
|
[] |
|
37
|
|
|
); |
|
38
|
|
|
$header = self::getHeaderFile(); |
|
39
|
|
|
|
|
40
|
|
|
parent::__construct(\compact('header', 'entity', 'item', 'items', 'action', 'properties', 'fkFns')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function getFileTemplate(): string |
|
44
|
|
|
{ |
|
45
|
|
|
if (\in_array($this->action, ['index', 'create', 'read', 'update', 'delete', 'login'])) { |
|
46
|
|
|
return $this->action . '.php.twig'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return 'default.php.twig'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function getPathTemplate(): string |
|
53
|
|
|
{ |
|
54
|
|
|
return \sprintf('%1$s/FlexPHP/UseCase', parent::getPathTemplate()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function getFkCheck(array $relations): array |
|
58
|
|
|
{ |
|
59
|
|
|
return \array_reduce($relations, function (array $result, array $relation) { |
|
60
|
|
|
if (!empty($relation['check'])) { |
|
61
|
|
|
$result[] = $relation; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $result; |
|
65
|
|
|
}, []); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|