argument registration
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Reflection / ObjectAccess.php
1 <?php
2
3 /* *
4 * This script belongs to the FLOW3 framework. *
5 * *
6 * It is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU Lesser General Public License as published by the *
8 * Free Software Foundation, either version 3 of the License, or (at your *
9 * option) any later version. *
10 * *
11 * This script is distributed in the hope that it will be useful, but *
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
14 * General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Lesser General Public *
17 * License along with the script. *
18 * If not, see http://www.gnu.org/licenses/lgpl.html *
19 * *
20 * The TYPO3 project - inspiring people to share! *
21 * */
22
23 /**
24 * @package FLOW3
25 * @subpackage Reflection
26 * @version $Id: ObjectAccess.php 2031 2009-03-24 11:36:56Z robert $
27 */
28 /**
29 * Provides methods to call appropriate getter/setter on an object given the
30 * property name. It does this following these rules:
31 * - if the target object is an instance of ArrayAccess, it gets/sets the property
32 * - if public getter/setter method exists, call it.
33 * - if public property exists, return/set the value of it.
34 * - else, throw exception
35 *
36 * @package
37 * @subpackage
38 * @version $Id: ObjectAccess.php 2031 2009-03-24 11:36:56Z robert $
39 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
40 */
41 class Tx_ExtBase_Reflection_ObjectAccess {
42
43 const ACCESS_GET = 0;
44 const ACCESS_SET = 1;
45 const ACCESS_PUBLIC = 2;
46
47 /**
48 * Get a property of a given object.
49 * Tries to get the property the following ways:
50 * - if the target object is an instance of ArrayAccess, it gets the property
51 * on it if it exists.
52 * - if public getter method exists, call it.
53 * - if public property exists, return the value of it.
54 * - else, throw exception
55 *
56 * @param object $object Object to get the property from
57 * @param string $propertyName name of the property to retrieve
58 * @return object Value of the property.
59 * @author Robert Lemke <robert@typo3.org>
60 * @author Sebastian Kurfürst <sebastian@typo3.org>
61 */
62 static public function getProperty($object, $propertyName) {
63 if (!is_object($object)) throw new InvalidArgumentException('$object must be an object, ' . gettype($object). ' given.', 1237301367);
64 if (!is_string($propertyName)) throw new InvalidArgumentException('Given property name is not of type string.', 1231178303);
65
66 if (is_callable(array($object, $getterMethodName = self::buildGetterMethodName($propertyName)))) {
67 return call_user_func(array($object, $getterMethodName));
68 } elseif ($object instanceof ArrayAccess && isset($object[$propertyName])) {
69 return $object[$propertyName];
70 } elseif (array_key_exists($propertyName, get_object_vars($object))) {
71 return $object->$propertyName;
72 }
73 return NULL;
74 }
75
76 /**
77 * Set a property for a given object.
78 * Tries to set the property the following ways:
79 * - if public setter method exists, call it.
80 * - if public property exists, set it directly.
81 * - if the target object is an instance of ArrayAccess, it sets the property
82 * on it without checking if it existed.
83 * - else, return FALSE
84 *
85 * @param object $object The target object
86 * @param string $propertyName Name of the property to set
87 * @param object $propertyValue Value of the property
88 * @return void
89 * @throws Tx_ExtBase_Reflection_Exception if property was could not be set
90 * @author Sebastian Kurfürst <sebastian@typo3.org>
91 * @author Robert Lemke <robert@typo3.org>
92 */
93 static public function setProperty($object, $propertyName, $propertyValue) {
94 if (!is_object($object)) throw new InvalidArgumentException('$object must be an object, ' . gettype($object). ' given.', 1237301368);
95 if (!is_string($propertyName)) throw new InvalidArgumentException('Given property name is not of type string.', 1231178878);
96
97 if (is_callable(array($object, $setterMethodName = self::buildSetterMethodName($propertyName)))) {
98 call_user_func(array($object, $setterMethodName), $propertyValue);
99 } elseif ($object instanceof \ArrayAccess) {
100 $object[$propertyName] = $propertyValue;
101 } elseif (array_key_exists($propertyName, get_object_vars($object))) {
102 $object->$propertyName = $propertyValue;
103 } else {
104 return FALSE;
105 }
106 return TRUE;
107 }
108
109 /**
110 * Returns an array of properties which can be get/set with the getProperty
111 * and setProperty methods.
112 * Includes the following properties:
113 * - which can be set through a public setter method.
114 * - public properties which can be directly set.
115 *
116 * @param object $object Object to receive property names for
117 * @return array Array of all declared property names
118 * @author Sebastian Kurfürst <sebastian@typo3.org>
119 * @todo What to do with ArrayAccess
120 */
121 static public function getAccessiblePropertyNames($object) {
122 if (!is_object($object)) throw new InvalidArgumentException('$object must be an object, ' . gettype($object). ' given.', 1237301369);
123 $declaredPropertyNames = array_keys(get_class_vars(get_class($object)));
124
125 foreach (get_class_methods($object) as $methodName) {
126 if (substr($methodName, 0, 3) === 'get') {
127 $declaredPropertyNames[] = lcfirst(substr($methodName, 3));
128 }
129 }
130
131 $propertyNames = array_unique($declaredPropertyNames);
132 sort($propertyNames);
133 return $propertyNames;
134 }
135
136 /**
137 * Get all properties (names and their current values) of the current
138 * $object that are accessible through this class.
139 *
140 * @param object $object Object to get all properties from.
141 * @return array Associative array of all properties.
142 * @author Sebastian Kurfürst <sebastian@typo3.org>
143 * @todo What to do with ArrayAccess
144 */
145 static public function getAccessibleProperties($object) {
146 if (!is_object($object)) throw new InvalidArgumentException('$object must be an object, ' . gettype($object). ' given.', 1237301370);
147 $properties = array();
148 foreach (self::getAccessiblePropertyNames($object) as $propertyName) {
149 $properties[$propertyName] = self::getProperty($object, $propertyName);
150 }
151 return $properties;
152 }
153
154 /**
155 * Build the getter method name for a given property by capitalizing the
156 * first letter of the property, and prepending it with "get".
157 *
158 * @param string $property Name of the property
159 * @return string Name of the getter method name
160 * @author Sebastian Kurfürst <sebastian@typo3.org>
161 */
162 static protected function buildGetterMethodName($property) {
163 return 'get' . ucfirst($property);
164 }
165
166 /**
167 * Build the setter method name for a given property by capitalizing the
168 * first letter of the property, and prepending it with "set".
169 *
170 * @param string $property Name of the property
171 * @return string Name of the setter method name
172 * @author Sebastian Kurfürst <sebastian@typo3.org>
173 */
174 static protected function buildSetterMethodName($property) {
175 return 'set' . ucfirst($property);
176 }
177 }
178
179
180 ?>