[ 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\Strategy; 11 12 class ClosureStrategy implements StrategyInterface 13 { 14 /** 15 * Function, used in extract method, default: 16 * function ($value) { 17 * return $value; 18 * }; 19 * @var callable 20 */ 21 protected $extractFunc = null; 22 23 /** 24 * Function, used in hydrate method, default: 25 * function ($value) { 26 * return $value; 27 * }; 28 * @var callable 29 */ 30 protected $hydrateFunc = null; 31 32 /** 33 * You can describe how your values will extract and hydrate, like this: 34 * $hydrator->addStrategy('category', new ClosureStrategy( 35 * function (Category $value) { 36 * return (int) $value->id; 37 * }, 38 * function ($value) { 39 * return new Category((int) $value); 40 * } 41 * )); 42 * 43 * @param callable $extractFunc - anonymous function, that extract values 44 * from object 45 * @param callable $hydrateFunc - anonymous function, that hydrate values 46 * into object 47 */ 48 public function __construct($extractFunc = null, $hydrateFunc = null) 49 { 50 if (isset($extractFunc)) { 51 if (!is_callable($extractFunc)) { 52 throw new \Exception('$extractFunc must be callable'); 53 } 54 55 $this->extractFunc = $extractFunc; 56 } else { 57 $this->extractFunc = function ($value) { 58 return $value; 59 }; 60 } 61 62 if (isset($hydrateFunc)) { 63 if (!is_callable($hydrateFunc)) { 64 throw new \Exception('$hydrateFunc must be callable'); 65 } 66 67 $this->hydrateFunc = $hydrateFunc; 68 } else { 69 $this->hydrateFunc = function ($value) { 70 return $value; 71 }; 72 } 73 } 74 75 /** 76 * Converts the given value so that it can be extracted by the hydrator. 77 * 78 * @param mixed $value The original value. 79 * @param array $object The object is optionally provided as context. 80 * @return mixed Returns the value that should be extracted. 81 */ 82 public function extract($value, $object = null) 83 { 84 $func = $this->extractFunc; 85 86 return $func($value, $object); 87 } 88 89 /** 90 * Converts the given value so that it can be hydrated by the hydrator. 91 * 92 * @param mixed $value The original value. 93 * @param array $data The whole data is optionally provided as context. 94 * @return mixed Returns the value that should be hydrated. 95 */ 96 public function hydrate($value, $data = null) 97 { 98 $func = $this->hydrateFunc; 99 100 return $func($value, $data); 101 } 102 }
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 |