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