1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silverback\ApiComponentBundle\Factory\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Silverback\ApiComponentBundle\Entity\Content\Component\AbstractComponent; |
7
|
|
|
use Silverback\ApiComponentBundle\Exception\InvalidEntityException; |
8
|
|
|
use Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException; |
9
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
10
|
|
|
|
11
|
|
|
abstract class AbstractFactory implements FactoryInterface |
12
|
|
|
{ |
13
|
|
|
public const COMPONENT_CLASSES = [ |
14
|
|
|
'className' => null, |
15
|
|
|
'parent' => null |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var ObjectManager |
20
|
|
|
*/ |
21
|
|
|
protected $manager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var null|array |
25
|
|
|
*/ |
26
|
|
|
protected $ops; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ValidatorInterface |
30
|
|
|
*/ |
31
|
|
|
private $validator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ObjectManager $manager |
35
|
|
|
* @param ValidatorInterface $validator |
36
|
|
|
*/ |
37
|
56 |
|
public function __construct( |
38
|
|
|
ObjectManager $manager, |
39
|
|
|
ValidatorInterface $validator |
40
|
|
|
) { |
41
|
56 |
|
$this->manager = $manager; |
42
|
56 |
|
$this->validator = $validator; |
43
|
56 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $component |
47
|
|
|
* @param array|null $ops |
48
|
|
|
* @param array $ignoreOps |
49
|
|
|
*/ |
50
|
28 |
|
protected function init($component, ?array $ops = null, ?array $ignoreOps = null): void |
51
|
|
|
{ |
52
|
28 |
|
$this->setOptions($ops); |
53
|
28 |
|
foreach ($this->ops as $op=>$value) { |
54
|
|
|
if ( |
55
|
27 |
|
null !== $value && |
56
|
|
|
( |
57
|
23 |
|
null === $ignoreOps || |
58
|
27 |
|
!\in_array($op, $ignoreOps, true) |
59
|
|
|
) |
60
|
|
|
) { |
61
|
21 |
|
$setter = 'set' . ucfirst($op); |
62
|
21 |
|
if (\is_array($value)) { |
63
|
1 |
|
$component->$setter(...$value); |
64
|
|
|
} else { |
65
|
27 |
|
$component->$setter($value); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
28 |
|
$this->manager->persist($component); |
70
|
28 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array|null $ops |
74
|
|
|
* @throws \Silverback\ApiComponentBundle\Exception\InvalidFactoryOptionException |
75
|
|
|
*/ |
76
|
30 |
|
protected function setOptions(?array $ops): void |
77
|
|
|
{ |
78
|
30 |
|
if (!$ops) { |
79
|
6 |
|
$ops = []; |
80
|
|
|
} |
81
|
30 |
|
$this->ops = array_filter( |
82
|
30 |
|
array_merge(static::defaultOps(), $ops), |
83
|
30 |
|
function ($key) { |
84
|
29 |
|
if (!array_key_exists($key, static::defaultOps())) { |
85
|
2 |
|
throw new InvalidFactoryOptionException( |
86
|
2 |
|
sprintf('%s is not a valid option for the factory %s', $key, \get_class($this)) |
87
|
|
|
); |
88
|
|
|
} |
89
|
28 |
|
return true; |
90
|
30 |
|
}, |
91
|
30 |
|
ARRAY_FILTER_USE_KEY |
92
|
|
|
); |
93
|
28 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $component |
97
|
|
|
* @return bool |
98
|
|
|
* @throws \Silverback\ApiComponentBundle\Exception\InvalidEntityException |
99
|
|
|
*/ |
100
|
25 |
|
protected function validate($component): bool |
101
|
|
|
{ |
102
|
25 |
|
$errors = $this->validator->validate($component); |
103
|
25 |
|
if (\count($errors)) { |
104
|
|
|
throw new InvalidEntityException($errors); |
105
|
|
|
} |
106
|
25 |
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
3 |
|
protected static function defaultOps(): array |
113
|
|
|
{ |
114
|
3 |
|
return []; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array|null $ops |
119
|
|
|
*/ |
120
|
|
|
abstract public function create(?array $ops = null); |
121
|
|
|
} |
122
|
|
|
|