1
<?php
2

3
/*
4
 * This file is part of the Solarium package.
5
 *
6
 * For the full copyright and license information, please view the COPYING
7
 * file that was distributed with this source code.
8
 */
9

10
namespace Solarium\Core;
11

12
use Solarium\Exception\InvalidArgumentException;
13

14
/**
15
 * Base class for configurable classes.
16
 *
17
 * All classes extending this class are  configurable using the constructor or
18
 * setOption calls. This is the base for many Solarium classes, providing a
19
 * uniform interface for various models.
20
 */
21
class Configurable implements ConfigurableInterface
22
{
23
    /**
24
     * Default options.
25
     *
26
     * @var array
27
     */
28
    protected $options = [];
29

30
    /**
31
     * Constructor.
32
     *
33
     * If options are passed they will be merged with {@link $options} using
34
     * the {@link setOptions()} method.
35
     *
36
     * After handling the options the {@link _init()} method is called.
37
     *
38
     * @param array|\Zend_Config $options
39
     *
40
     * @throws InvalidArgumentException
41
     */
42 12
    public function __construct($options = null)
43
    {
44 12
        if (null !== $options) {
45 12
            $this->setOptions($options);
46
        } else {
47 12
            $this->init();
48
        }
49
    }
50

51
    /**
52
     * Set options.
53
     *
54
     * If $options is an object, it will be converted into an array by calling
55
     * its toArray method. This is compatible with the Zend_Config classes in
56
     * Zend Framework, but can also easily be implemented in any other object.
57
     * If $options does not have the toArray method, the internal method will
58
     * be used instead.
59
     *
60
     * @param array|\Zend_Config $options
61
     * @param bool               $overwrite True for overwriting existing options, false
62
     *                                      for merging (new values overwrite old ones if needed)
63
     *
64
     * @throws InvalidArgumentException
65
     *
66
     * @return self
67
     */
68 12
    public function setOptions($options, bool $overwrite = false): ConfigurableInterface
69
    {
70 12
        if (null !== $options) {
71
            // first convert to array if needed
72 12
            if (!\is_array($options)) {
73 12
                if (\is_object($options)) {
74 12
                    $options = (!method_exists($options, 'toArray') ? $this->toArray($options) : $options->toArray());
75
                } else {
76 0
                    throw new InvalidArgumentException('Options value given to the setOptions() method must be an array or a Zend_Config object');
77
                }
78
            }
79

80 12
            if (true === $overwrite) {
81 12
                $this->options = $options;
82
            } else {
83 12
                $this->options = array_merge($this->options, $options);
84
            }
85

86 12
            if (true === \is_callable([$this, 'initLocalParameters'])) {
87 12
                $this->initLocalParameters();
88
            }
89
            // re-init for new options
90 12
            $this->init();
91
        }
92

93 12
        return $this;
94
    }
95

96
    /**
97
     * Get an option value by name.
98
     *
99
     * If the option is empty or not set a NULL value will be returned.
100
     *
101
     * @param string $name
102
     *
103
     * @return mixed|null
104
     */
105 12
    public function getOption(string $name)
106
    {
107 12
        return $this->options[$name] ?? null;
108
    }
109

110
    /**
111
     * Get all options.
112
     *
113
     * @return array
114
     */
115 12
    public function getOptions(): array
116
    {
117 12
        return $this->options;
118
    }
119

120
    /**
121
     * Initialization hook.
122
     *
123
     * Can be used by classes for special behaviour. For instance some options
124
     * have extra setup work in their 'set' method that also need to be called
125
     * when the option is passed as a constructor argument.
126
     *
127
     * This hook is called by the constructor after saving the constructor
128
     * arguments in {@link $options}
129
     *
130
     * This empty implementation can optionally be implemented in
131
     * descending classes. It's not an abstract method on purpose, there are
132
     * many cases where no initialization is needed.
133
     */
134 12
    protected function init()
135
    {
136
    }
137

138
    /**
139
     * Set an option.
140
     *
141
     * @param string $name
142
     * @param mixed  $value
143
     *
144
     * @return self Provides fluent interface
145
     */
146 12
    protected function setOption(string $name, $value): self
147
    {
148 12
        $this->options[$name] = $value;
149

150 12
        return $this;
151
    }
152

153
    /**
154
     * Turns an object array into an associative multidimensional array.
155
     *
156
     * @param $object object|object[]
157
     *
158
     * @return array
159
     */
160 0
    protected function toArray($object): array
161
    {
162 0
        if (\is_object($object)) {
163 0
            return (array) $object;
164
        }
165

166
        /*
167
        * Return array converted to object
168
        * Using __METHOD__ (Magic constant)
169
        * for recursive call
170
        */
171 0
        return array_map(__METHOD__, $object);
172
    }
173
}

Read our documentation on viewing source code .

Loading