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\Factory; |
11
|
|
|
|
12
|
|
|
use FlexPHP\Generator\Domain\Builders\AbstractBuilder; |
13
|
|
|
use FlexPHP\Schema\SchemaAttributeInterface; |
14
|
|
|
use FlexPHP\Schema\SchemaInterface; |
15
|
|
|
|
16
|
|
|
final class FactoryBuilder extends AbstractBuilder |
17
|
|
|
{ |
18
|
|
|
public function __construct(SchemaInterface $schema) |
19
|
|
|
{ |
20
|
|
|
$entity = $this->getInflector()->entity($schema->name()); |
21
|
|
|
$item = $this->getInflector()->item($schema->name()); |
22
|
|
|
$setters = $this->getSetterWithCasting($schema->attributes()); |
23
|
|
|
$fkFns = $this->getFkFunctions($schema->fkRelations()); |
24
|
|
|
$fkRels = $this->getFkRelations($schema->fkRelations()); |
25
|
|
|
$header = self::getHeaderFile(); |
26
|
|
|
|
27
|
|
|
parent::__construct(\compact('entity', 'item', 'setters', 'fkFns', 'fkRels', 'header')); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
protected function getFileTemplate(): string |
31
|
|
|
{ |
32
|
|
|
return 'Factory.php.twig'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function getPathTemplate(): string |
36
|
|
|
{ |
37
|
|
|
return \sprintf('%1$s/FlexPHP/Factory', parent::getPathTemplate()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private function getSetterWithCasting(array $properties): array |
41
|
|
|
{ |
42
|
|
|
return \array_reduce($properties, function (array $result, SchemaAttributeInterface $attributes): array { |
43
|
|
|
$result[] = [ |
44
|
|
|
'pascal' => $this->getInflector()->pascalProperty($attributes->name()), |
45
|
|
|
'camel' => $this->getInflector()->camelProperty($attributes->name()), |
46
|
|
|
'typehint' => $attributes->typeHint(), |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
return $result; |
50
|
|
|
}, []); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|