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