Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Stdlib / Hydrator / Strategy / ClosureStrategy.php
blobe2b32a640c3f0ee354259ecb93e7e20ab098a4aa
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Stdlib\Hydrator\Strategy;
12 class ClosureStrategy implements StrategyInterface
14 /**
15 * Function, used in extract method, default:
16 * function ($value) {
17 * return $value;
18 * };
19 * @var callable
21 protected $extractFunc = null;
23 /**
24 * Function, used in hydrate method, default:
25 * function ($value) {
26 * return $value;
27 * };
28 * @var callable
30 protected $hydrateFunc = null;
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 * ));
43 * @param callable $extractFunc - anonymous function, that extract values
44 * from object
45 * @param callable $hydrateFunc - anonymous function, that hydrate values
46 * into object
48 public function __construct($extractFunc = null, $hydrateFunc = null)
50 if (isset($extractFunc)) {
51 if (!is_callable($extractFunc)) {
52 throw new \Exception('$extractFunc must be callable');
55 $this->extractFunc = $extractFunc;
56 } else {
57 $this->extractFunc = function ($value) {
58 return $value;
62 if (isset($hydrateFunc)) {
63 if (!is_callable($hydrateFunc)) {
64 throw new \Exception('$hydrateFunc must be callable');
67 $this->hydrateFunc = $hydrateFunc;
68 } else {
69 $this->hydrateFunc = function ($value) {
70 return $value;
75 /**
76 * Converts the given value so that it can be extracted by the hydrator.
78 * @param mixed $value The original value.
79 * @return mixed Returns the value that should be extracted.
81 public function extract($value)
83 $func = $this->extractFunc;
85 return $func($value);
88 /**
89 * Converts the given value so that it can be hydrated by the hydrator.
91 * @param mixed $value The original value.
92 * @return mixed Returns the value that should be hydrated.
94 public function hydrate($value)
96 $func = $this->hydrateFunc;
98 return $func($value);