Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 113 lines (105 sloc) 4.003 kb
176f5d0 @renanbr Added configurator classes
authored
1 <?php
2 /**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category Zend
16 * @package Zend_Stdlib
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
19 */
20
21 /**
22 * @namespace
23 */
24 namespace Zend\Stdlib;
25
26 /**
27 * @category Zend
28 * @package Zend_Stdlib
29 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
30 * @license http://framework.zend.com/license/new-bsd New BSD License
31 */
32 abstract class Configurator
33 {
34 /**
35 * Configure a target object with the provided options
36 *
37 * The options passed in must be traversable with option names for keys.
38 * Option names are case insensitive and are stripped of underscores. By
39 * convention, option names are in lowercase and with underscores separated
40 * words.
41 *
42 * The target object is expected to have a setter method for each option
43 * passed. By convention, setters are named using 'set' prefix. Only public
44 * and non-static methods are called, everything else is ignored.
45 *
46 * Example: option_name -> setOptionName()
47 *
48 * Optionally target may implement \Zend\Stdlib\Configurable. Once it
49 * happens, configurator interprets getOptionNames()'s return. This method
50 * returns option names for the target. Only specified options in the return
51 * are configured, everything else is ignored.
52 *
53 * Example: array('max_length', 'timeout') -> setMaxLength() and setTimeout()
54 *
55 * Configurable implementation can change setter method. Optionally keys
56 * from option names provide non-standard setter method names.
57 *
58 * Example: array('setMaxLen' => 'max_length') -> setMaxLen() instead of setMaxlength()
59 *
60 * @param object $object The object that needs to be configured
61 * @param \Traversable $options The configuration to apply
62 * @throws \Zend\Stdlib\Exception\InvalidArgumentException
63 * @return void
64 */
65 public static function configure($object, $options)
66 {
67 // check arguments
68 if (!is_object($object)) {
69 throw new Exception\InvalidArgumentException(
70 'Target should be an object');
71 }
72 if (!$options instanceof Traversable && !is_array($options)) {
73 throw new Exception\InvalidArgumentException(
74 'Options should implement Traversable');
75 }
76
77 $normalize = function ($name) {
78 return strtolower(str_replace('_', '', $name));
79 };
80
81 // normalize option names
82 foreach (array_keys($options) as $key) {
83 $options[$normalize($key)] = array_shift($options);
84 }
85 if ($object instanceof Configurable) {
86 $names = (array) $object->getOptionNames();
87 array_walk($names, $normalize);
88 } else {
89 $names = array_keys($options);
90 }
91
92 // retrieve available methods
93 $methods = array_map('strtolower', get_class_methods($object));
94
95 foreach ($names as $setter => $name) {
96 if (isset($options[$name])) {
97 // define setter
98 if (!is_string($setter)) {
99 $setter = "set$name";
100 if (!in_array($setter, $methods)) {
101 $setter = null;
102 }
103 }
104
105 // trigger setter
106 if ($setter) {
107 $object->$setter($options[$name]);
108 }
109 }
110 }
111 }
112 }
Something went wrong with that request. Please try again.