1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Everlution\Navigation\Factory\Build; |
6
|
|
|
|
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class NavigationItemConfig. |
11
|
|
|
* @author Ivan Barlog <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
abstract class Config |
14
|
|
|
{ |
15
|
|
|
const OPTION_CLASS = 'class'; |
16
|
|
|
|
17
|
|
|
/** @var OptionsResolver */ |
18
|
|
|
protected $resolver; |
19
|
|
|
/** @var array */ |
20
|
|
|
protected $supportedClasses = []; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->resolver = new OptionsResolver(); |
25
|
|
|
|
26
|
|
|
$this->resolver->setRequired([self::OPTION_CLASS]); |
27
|
|
|
$this->resolver->setAllowedTypes(self::OPTION_CLASS, 'string'); |
28
|
|
|
|
29
|
|
|
$this->config(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $className |
34
|
|
|
* @param array $arguments |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
abstract protected function getObject(string $className, array $arguments); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $object |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
abstract protected function getArray($object): array; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param array $config |
47
|
|
|
* @return array |
48
|
|
|
*/ |
49
|
|
|
protected function resolve(array $config): array |
50
|
|
|
{ |
51
|
|
|
return $this->resolver->resolve($config); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $class |
56
|
|
|
*/ |
57
|
|
|
protected function checkIfSupport(string $class) |
58
|
|
|
{ |
59
|
|
|
if (false === in_array($class, $this->supportedClasses)) { |
60
|
|
|
throw new UnsupportedItemClassException($class, $this->supportedClasses); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Specifies additional options |
66
|
|
|
* This method is called after initial OptionsResolver setup |
67
|
|
|
* Use OptionsResolver within this method |
68
|
|
|
* |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
abstract protected function config(); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $config |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
protected function popClassName(array $config): string |
78
|
|
|
{ |
79
|
|
|
$class = $config[self::OPTION_CLASS]; |
80
|
|
|
unset($config[self::OPTION_CLASS]); |
81
|
|
|
|
82
|
|
|
return $class; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|