|
| 1 | +.. index:: |
| 2 | + single: PropertyAccess |
| 3 | + single: Components; PropertyAccess |
| 4 | + |
| 5 | +The PropertyAccess Component |
| 6 | +============================ |
| 7 | + |
| 8 | + PropertyAccess component provides function to read and write from/to an |
| 9 | + object or array using a simple string notation. |
| 10 | + |
| 11 | +.. versionadded:: 2.2 |
| 12 | + The PropertyAccess Component is new to Symfony 2.2. Previously, the |
| 13 | + ``PropertyPath`` class was located in the ``Form`` component. |
| 14 | + |
| 15 | +Installation |
| 16 | +------------ |
| 17 | + |
| 18 | +You can install the component in two different ways: |
| 19 | + |
| 20 | +* Use the official Git repository (https://github.com/symfony/PropertyAccess); |
| 21 | +* :doc:`Install it via Composer</components/using_components>` * (``symfony/property-access`` on `Packagist`_). |
| 22 | + |
| 23 | +Usage |
| 24 | +----- |
| 25 | + |
| 26 | +The entry point of this component is the |
| 27 | +:method:`Symfony\\Component\\PropertyAccess\\PropertyAccess::getPropertyAccessor` |
| 28 | +factory. This factory will create a new instance of the |
| 29 | +:class:`Symfony\\Component\\PropertyAccess\PropertyAccessor` class with the |
| 30 | +default configuration:: |
| 31 | + |
| 32 | + use Symfony\Component\PropertyAccess\PropertyAccess; |
| 33 | + |
| 34 | + $accessor = PropertyAccess:getPropertyAccessor(); |
| 35 | + |
| 36 | +Reading from arrays |
| 37 | +------------------- |
| 38 | + |
| 39 | +You can read an array with the |
| 40 | +:method:`Symfony\\Component\\PropertyAccess\PropertyAccessor::getValue` |
| 41 | +method. This is done using the index notation that is used in PHP:: |
| 42 | + |
| 43 | + // ... |
| 44 | + $person = array( |
| 45 | + 'first_name' => 'Wouter', |
| 46 | + ); |
| 47 | + |
| 48 | + echo $accessor->getValue($persons, '[first_name]'); // 'Wouter' |
| 49 | + echo $accessor->getValue($person, '[age]'); // null |
| 50 | + |
| 51 | +As you can see, the method will return ``null`` if the index does not exists. |
| 52 | + |
| 53 | +You can also use multi dimensional arrays:: |
| 54 | + |
| 55 | + // ... |
| 56 | + $persons = array( |
| 57 | + array( |
| 58 | + 'first_name' => 'Wouter', |
| 59 | + ), |
| 60 | + array( |
| 61 | + 'first_name' => 'Ryan', |
| 62 | + ) |
| 63 | + ); |
| 64 | + |
| 65 | + echo $accessor->getValue($persons, '[0][first_name]'); // 'Wouter' |
| 66 | + echo $accessor->getValue($persons, '[1][first_name]'); // 'Ryan' |
| 67 | + |
| 68 | +Reading from objects |
| 69 | +-------------------- |
| 70 | + |
| 71 | +The ``getValue`` method is a very robust method. You can see all features if |
| 72 | +you are working with objects. |
| 73 | + |
| 74 | +Using properties |
| 75 | +~~~~~~~~~~~~~~~~ |
| 76 | + |
| 77 | +We can read properties without the index notation, instead we use the dot |
| 78 | +notation:: |
| 79 | + |
| 80 | + // ... |
| 81 | + $person = new Person(); |
| 82 | + $person->firstName = 'Wouter'; |
| 83 | + |
| 84 | + echo $accessor->getValue($person, 'first_name'); // 'Wouter' |
| 85 | + |
| 86 | + $child = new Person(); |
| 87 | + $child->firstName = 'Bar'; |
| 88 | + $person->children = array($child); |
| 89 | + |
| 90 | + echo $accessor->getValue($person, 'children[0].first_name'); // 'Bar' |
| 91 | + |
| 92 | +.. caution:: |
| 93 | + |
| 94 | + This option is the last option used by the ``PropertyAccessor``. It tries |
| 95 | + to find the other options before using the property. If you have a public |
| 96 | + property that have a getter to, it will use the getter. |
| 97 | + |
| 98 | +Using getters |
| 99 | +~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +The ``getValue`` method also supports reading using getters. The method will |
| 102 | +be created using common naming conventions for getters. It camelizes the |
| 103 | +property name (``first_name`` becomes ``FirstName``) and prefixes it with |
| 104 | +``get``. So the actual method becomes ``getFirstName``:: |
| 105 | + |
| 106 | + // ... |
| 107 | + class Person |
| 108 | + { |
| 109 | + private $firstName = 'Wouter'; |
| 110 | + |
| 111 | + public function getFirstName() |
| 112 | + { |
| 113 | + return $this->firstName; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + $person = new Person(); |
| 118 | + |
| 119 | + echo $accessor->getValue($person, 'first_name'); // 'Wouter' |
| 120 | + |
| 121 | +Using hassers/issers |
| 122 | +~~~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +And it doesn't even stop there. If there is no getter found, the accessor will |
| 125 | +look for a isser or hasser. This method is created using the same way as |
| 126 | +getters, this means that you can do something like this:: |
| 127 | + |
| 128 | + // ... |
| 129 | + class Person |
| 130 | + { |
| 131 | + private $author = true; |
| 132 | + private $children = array(); |
| 133 | + |
| 134 | + public function isAuthor() |
| 135 | + { |
| 136 | + return $this->author; |
| 137 | + } |
| 138 | + |
| 139 | + public function hasChildren() |
| 140 | + { |
| 141 | + return 0 !== count($this->children); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + $person = new Person(); |
| 146 | + |
| 147 | + if ($accessor->getValue($person, 'author')) { |
| 148 | + echo 'He is an author'; |
| 149 | + } |
| 150 | + if ($accessor->getValue($person, 'children')) { |
| 151 | + echo 'He has children'; |
| 152 | + } |
| 153 | + |
| 154 | +This will produce: ``He is an author`` |
| 155 | + |
| 156 | +Magic Methods |
| 157 | +~~~~~~~~~~~~~ |
| 158 | + |
| 159 | +At last, the ``getValue`` can use the magic ``__get`` too:: |
| 160 | + |
| 161 | + // ... |
| 162 | + class Person |
| 163 | + { |
| 164 | + private $children = array( |
| 165 | + 'wouter' => array(...), |
| 166 | + ); |
| 167 | + |
| 168 | + public function __get($id) |
| 169 | + { |
| 170 | + return $this->children[$id]; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + $person = new Person(); |
| 175 | + |
| 176 | + echo $accessor->getValue($person, 'Wouter'); // array(...) |
| 177 | + |
| 178 | +Writing to arrays |
| 179 | +----------------- |
| 180 | + |
| 181 | +The ``PropertyAccessor`` class can do more than just reading an array, it can |
| 182 | +also write to an array. This can be achieved using the |
| 183 | +:method:`Symfony\\Component\\PropertyAccess\\PropertyAccessor::setValue` |
| 184 | +method:: |
| 185 | + |
| 186 | + // ... |
| 187 | + $person = array(); |
| 188 | + |
| 189 | + $accessor->setValue($person, '[first_name]', 'Wouter'); |
| 190 | + |
| 191 | + echo $accessor->getValue($person, '[first_name]'); // 'Wouter' |
| 192 | + // or |
| 193 | + // echo $person['first_name']; // 'Wouter' |
| 194 | + |
| 195 | +Writing to objects |
| 196 | +------------------ |
| 197 | + |
| 198 | +The ``setValue`` method has the same features as the ``getValue`` method. You |
| 199 | +can use setters, the magic ``__set`` or properties to set values:: |
| 200 | + |
| 201 | + // ... |
| 202 | + class Person |
| 203 | + { |
| 204 | + public $firstName; |
| 205 | + private $lastName; |
| 206 | + private $children = array(); |
| 207 | + |
| 208 | + public function setLastName($name) |
| 209 | + { |
| 210 | + $this->lastName = $name; |
| 211 | + } |
| 212 | + |
| 213 | + public function __set($property, $value) |
| 214 | + { |
| 215 | + $this->$property = $value; |
| 216 | + } |
| 217 | + |
| 218 | + // ... |
| 219 | + } |
| 220 | + |
| 221 | + $person = new Person(); |
| 222 | + |
| 223 | + $accessor->setValue($person, 'firstName', 'Wouter'); |
| 224 | + $accessor->setValue($person, 'lastName', 'de Jong'); |
| 225 | + $accessor->setValue($person, 'children', array(new Person())); |
| 226 | + |
| 227 | + echo $person->firstName; // 'Wouter' |
| 228 | + echo $person->getLastName(); // 'de Jong' |
| 229 | + echo $person->children; // array(Person()); |
| 230 | + |
| 231 | +Mixing objects and arrays |
| 232 | +------------------------- |
| 233 | + |
| 234 | +You can also mix objects and arrays:: |
| 235 | + |
| 236 | + // ... |
| 237 | + class Person |
| 238 | + { |
| 239 | + public $firstName; |
| 240 | + private $children = array(); |
| 241 | + |
| 242 | + public function setChildren($children) |
| 243 | + { |
| 244 | + return $this->children; |
| 245 | + } |
| 246 | + |
| 247 | + public function getChildren() |
| 248 | + { |
| 249 | + return $this->children; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + $person = new Person(); |
| 254 | + |
| 255 | + $accessor->setValue($person, 'children[0]', new Person); |
| 256 | + // equal to $person->getChildren()[0] = new Person() |
| 257 | + |
| 258 | + $accessor->setValue($person, 'children[0].firstName', 'Wouter'); |
| 259 | + // equal to $person->getChildren()[0]->firstName = 'Wouter' |
| 260 | + |
| 261 | + echo 'Hello '.$accessor->getValue($person, 'children[0].firstName'); // 'Wouter' |
| 262 | + // equal to $person->getChildren()[0]->firstName |
| 263 | + |
| 264 | +.. _Packagist: https://packagist.org/packages/symfony/property-access |
0 commit comments