1 | <?php |
||
25 | abstract class AbstractFactory implements ProviderFactoryInterface |
||
26 | { |
||
27 | protected static $dependencies = []; |
||
28 | |||
29 | /** |
||
30 | * @param array $config |
||
31 | * |
||
32 | * @return Provider |
||
33 | */ |
||
34 | abstract protected function getProvider(array $config): Provider; |
||
35 | |||
36 | /** |
||
37 | * {@inheritdoc} |
||
38 | */ |
||
39 | 22 | public function createProvider(array $options = []): Provider |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 22 | public static function validate(array $options, $providerName) |
|
54 | { |
||
55 | 22 | static::verifyDependencies(); |
|
56 | |||
57 | 22 | $resolver = new OptionsResolver(); |
|
58 | 22 | static::configureOptionResolver($resolver); |
|
59 | |||
60 | try { |
||
61 | 22 | $resolver->resolve($options); |
|
62 | } catch (\Exception $e) { |
||
63 | $message = sprintf( |
||
64 | 'Error while configure provider "%s". Verify your configuration at "bazinga_geocoder.providers.%s.options". %s', |
||
65 | $providerName, |
||
66 | $providerName, |
||
67 | $e->getMessage() |
||
68 | ); |
||
69 | |||
70 | throw new InvalidConfigurationException($message, $e->getCode(), $e); |
||
71 | } |
||
72 | 22 | } |
|
73 | |||
74 | /** |
||
75 | * Make sure that we have the required class and throw and exception if we don't. |
||
76 | * |
||
77 | * @throws \LogicException |
||
78 | */ |
||
79 | 22 | protected static function verifyDependencies() |
|
80 | { |
||
81 | 22 | foreach (static::$dependencies as $dependency) { |
|
82 | 22 | if (!class_exists($dependency['requiredClass'])) { |
|
83 | throw new \LogicException( |
||
84 | sprintf( |
||
85 | 'You must install the "%s" package to use the "%s" factory.', |
||
86 | $dependency['packageName'], |
||
87 | 22 | static::class |
|
88 | ) |
||
89 | ); |
||
90 | } |
||
91 | } |
||
92 | 22 | } |
|
93 | |||
94 | /** |
||
95 | * By default we do not have any options to configure. A factory should override this function and confgure |
||
96 | * the options resolver. |
||
97 | * |
||
98 | * @param OptionsResolver $resolver |
||
99 | */ |
||
100 | 1 | protected static function configureOptionResolver(OptionsResolver $resolver) |
|
103 | } |
||
104 |