[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10 namespace Zend\Stdlib\Hydrator; 11 12 use Zend\Stdlib\Exception; 13 14 class ArraySerializable extends AbstractHydrator 15 { 16 /** 17 * Extract values from the provided object 18 * 19 * Extracts values via the object's getArrayCopy() method. 20 * 21 * @param object $object 22 * @return array 23 * @throws Exception\BadMethodCallException for an $object not implementing getArrayCopy() 24 */ 25 public function extract($object) 26 { 27 if (!is_callable(array($object, 'getArrayCopy'))) { 28 throw new Exception\BadMethodCallException( 29 sprintf('%s expects the provided object to implement getArrayCopy()', __METHOD__) 30 ); 31 } 32 33 $data = $object->getArrayCopy(); 34 $filter = $this->getFilter(); 35 36 foreach ($data as $name => $value) { 37 if (!$filter->filter($name)) { 38 unset($data[$name]); 39 continue; 40 } 41 $extractedName = $this->extractName($name, $object); 42 // replace the original key with extracted, if differ 43 if ($extractedName !== $name) { 44 unset($data[$name]); 45 $name = $extractedName; 46 } 47 $data[$name] = $this->extractValue($name, $value, $object); 48 } 49 50 return $data; 51 } 52 53 /** 54 * Hydrate an object 55 * 56 * Hydrates an object by passing $data to either its exchangeArray() or 57 * populate() method. 58 * 59 * @param array $data 60 * @param object $object 61 * @return object 62 * @throws Exception\BadMethodCallException for an $object not implementing exchangeArray() or populate() 63 */ 64 public function hydrate(array $data, $object) 65 { 66 $replacement = array(); 67 foreach ($data as $key => $value) { 68 $name = $this->hydrateName($key, $data); 69 $replacement[$name] = $this->hydrateValue($name, $value, $data); 70 } 71 72 if (is_callable(array($object, 'exchangeArray'))) { 73 $object->exchangeArray($replacement); 74 } elseif (is_callable(array($object, 'populate'))) { 75 $object->populate($replacement); 76 } else { 77 throw new Exception\BadMethodCallException( 78 sprintf('%s expects the provided object to implement exchangeArray() or populate()', __METHOD__) 79 ); 80 } 81 return $object; 82 } 83 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |