|
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\Generator\Domain\Builders\Entity\TypeHintTrait; |
|
14
|
|
|
use FlexPHP\Schema\Constants\Keyword; |
|
15
|
|
|
|
|
16
|
|
|
final class FactoryBuilder extends AbstractBuilder |
|
17
|
|
|
{ |
|
18
|
|
|
use TypeHintTrait; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(string $entity, array $properties) |
|
21
|
|
|
{ |
|
22
|
|
|
$entity = $this->getPascalCase($this->getSingularize($entity)); |
|
23
|
|
|
$item = $this->getCamelCase($this->getSingularize($entity)); |
|
24
|
|
|
$setters = $this->getSetterWithCasting($properties); |
|
25
|
|
|
|
|
26
|
|
|
parent::__construct(\compact('entity', 'item', 'setters')); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function getFileTemplate(): string |
|
30
|
|
|
{ |
|
31
|
|
|
return 'Factory.php.twig'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
protected function getPathTemplate(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return \sprintf('%1$s/FlexPHP/Factory', parent::getPathTemplate()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function getSetterWithCasting(array $properties): array |
|
40
|
|
|
{ |
|
41
|
|
|
return \array_reduce($properties, function (array $result, array $attributes): array { |
|
42
|
|
|
$result[] = [ |
|
43
|
|
|
'pascal' => $this->getPascalCase($attributes[Keyword::NAME]), |
|
44
|
|
|
'camel' => $this->getCamelCase($attributes[Keyword::NAME]), |
|
45
|
|
|
'typehint' => $this->guessTypeHint($attributes[Keyword::DATATYPE]), |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
return $result; |
|
49
|
|
|
}, []); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|