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
10 namespace Zend\Stdlib\Hydrator\Strategy
;
12 class ClosureStrategy
implements StrategyInterface
15 * Function, used in extract method, default:
21 protected $extractFunc = null;
24 * Function, used in hydrate method, default:
30 protected $hydrateFunc = null;
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;
39 * return new Category((int) $value);
43 * @param callable $extractFunc - anonymous function, that extract values
45 * @param callable $hydrateFunc - anonymous function, that hydrate values
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;
57 $this->extractFunc
= function ($value) {
62 if (isset($hydrateFunc)) {
63 if (!is_callable($hydrateFunc)) {
64 throw new \
Exception('$hydrateFunc must be callable');
67 $this->hydrateFunc
= $hydrateFunc;
69 $this->hydrateFunc
= function ($value) {
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
;
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
;