source: sandbox/expresso-solr/expressoMail1_2/inc/solrclient/library/Solarium/Configurable.php @ 7576

Revision 7576, 5.3 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
1<?php
2/**
3 * Copyright 2011 Bas de Nooijer. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 *    this listof conditions and the following disclaimer in the documentation
13 *    and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 * The views and conclusions contained in the software and documentation are
28 * those of the authors and should not be interpreted as representing official
29 * policies, either expressed or implied, of the copyright holder.
30 *
31 * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
32 * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
33 * @link http://www.solarium-project.org/
34 *
35 * @package Solarium
36 */
37
38/**
39 * Base class for configurable classes
40 *
41 * All classes extending this class are  configurable using the constructor or
42 * setOption calls. This is the base for many Solarium classes, providing a
43 * uniform interface for various models.
44 *
45 * @package Solarium
46 */
47class Solarium_Configurable
48{
49
50    /**
51     * Default options
52     *
53     * @var array
54     */
55    protected $_options = array(
56    );
57
58    /**
59     * Constructor
60     *
61     * If options are passed they will be merged with {@link $_options} using
62     * the {@link setOptions()} method.
63     *
64     * After handling the options the {@link _init()} method is called.
65     *
66     * @throws Solarium_Exception
67     * @param array|Zend_Config $options
68     * @return void
69     */
70    public function __construct($options = null)
71    {
72        if (null !== $options) {
73            $this->setOptions($options);
74        } else {
75            $this->_init();
76        }
77    }
78
79    /**
80     * Set options
81     *
82     * If $options is an object it will be converted into an array by called
83     * it's toArray method. This is compatible with the Zend_Config classes in
84     * Zend Framework, but can also easily be implemented in any other object.
85     *
86     * @throws Solarium_Exception
87     * @param array|Zend_Config $options
88     * @param boolean $overwrite True for overwriting existing options, false
89     *  for merging (new values overwrite old ones if needed)
90     *
91     * @return void
92     */
93    public function setOptions($options, $overwrite = false)
94    {
95        if (null !== $options) {
96            // first convert to array if needed
97            if (!is_array($options)) {
98                if (is_object($options)) {
99                    $options = $options->toArray();
100                } else {
101                    throw new Solarium_Exception('Options must be an array or a Zend_Config object');
102                }
103            }
104
105            if (true == $overwrite) {
106                $this->_options = $options;
107            } else {
108                $this->_options = array_merge($this->_options, $options);
109            }
110
111            // re-init for new options
112            $this->_init();
113        }
114    }
115
116    /**
117     * Initialization hook
118     *
119     * Can be used by classes for special behaviour. For instance some options
120     * have extra setup work in their 'set' method that also need to be called
121     * when the option is passed as a constructor argument.
122     *
123     * This hook is called by the constructor after saving the constructor
124     * arguments in {@link $_options}
125     *
126     * @internal This empty implementation can optionally be implemented in
127     *  descending classes. It's not an abstract method on purpose, there are
128     *  many cases where no initialization is needed.
129     *
130     * @return void
131     */
132    protected function _init()
133    {
134
135    }
136
137    /**
138     * Set an option
139     *
140     * @param string $name
141     * @param mixed $value
142     * @return Solarium_Configurable
143     */
144    protected function _setOption($name, $value)
145    {
146        $this->_options[$name] = $value;
147
148        return $this;
149    }
150
151    /**
152     * Get an option value by name
153     *
154     * If the option is empty or not set a NULL value will be returned.
155     *
156     * @param string $name
157     * @return mixed
158     */
159    public function getOption($name)
160    {
161        if (isset($this->_options[$name])) {
162            return $this->_options[$name];
163        } else {
164            return null;
165        }
166    }
167
168    /**
169     * Get all options
170     *
171     * @return array
172     */
173    public function getOptions()
174    {
175        return $this->_options;
176    }
177
178}
Note: See TracBrowser for help on using the repository browser.