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
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\Generator\Domain\Builders\Javascript; |
11
|
|
|
|
12
|
|
|
use FlexPHP\Generator\Domain\Builders\AbstractBuilder; |
13
|
|
|
use FlexPHP\Schema\SchemaAttributeInterface; |
14
|
|
|
use FlexPHP\Schema\SchemaInterface; |
15
|
|
|
|
16
|
|
|
final class JavascriptBuilder extends AbstractBuilder |
17
|
|
|
{ |
18
|
|
|
public function __construct(SchemaInterface $schema) |
19
|
|
|
{ |
20
|
|
|
$route = $this->getInflector()->route($schema->name()); |
21
|
|
|
$form = $this->getInflector()->form($schema->name()); |
22
|
|
|
$fkRels = $this->getNotBlameFkRelations($schema); |
23
|
|
|
|
24
|
|
|
parent::__construct(\compact('route', 'form', 'fkRels')); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function getFileTemplate(): string |
28
|
|
|
{ |
29
|
|
|
return 'Javascript.js.twig'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function getPathTemplate(): string |
33
|
|
|
{ |
34
|
|
|
return \sprintf('%1$s/FlexPHP/Javascript', parent::getPathTemplate()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function getNotBlameFkRelations(SchemaInterface $schema): array |
38
|
|
|
{ |
39
|
|
|
$properties = \array_map(function (SchemaAttributeInterface $property) { |
40
|
|
|
if ($property->isBlameBy()) { |
41
|
|
|
return $property->name(); |
42
|
|
|
} |
43
|
|
|
}, $schema->attributes()); |
44
|
|
|
|
45
|
|
|
$properties = \array_filter($properties); |
46
|
|
|
$fkRels = $this->getFkRelations($schema->fkRelations()); |
47
|
|
|
|
48
|
|
|
return \array_filter($fkRels, function (array $fkRel) use ($properties) { |
49
|
|
|
if (!\in_array($fkRel['pk'], $properties)) { |
50
|
|
|
return $fkRel; |
51
|
|
|
} |
52
|
|
|
}); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|