You are here

public function ClosureStrategy::__construct in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/zendframework/zend-hydrator/src/Strategy/ClosureStrategy.php \Zend\Hydrator\Strategy\ClosureStrategy::__construct()

You can describe how your values will extract and hydrate, like this:

<code> $hydrator->addStrategy('category', new ClosureStrategy( function (Category $value) { return (int) $value->id; }, function ($value) { return new Category((int) $value); } )); </code>

Parameters

callable $extractFunc - anonymous function, that extract values: from object

callable $hydrateFunc - anonymous function, that hydrate values: into object

File

vendor/zendframework/zend-hydrator/src/Strategy/ClosureStrategy.php, line 59

Class

ClosureStrategy

Namespace

Zend\Hydrator\Strategy

Code

public function __construct($extractFunc = null, $hydrateFunc = null) {
  if (isset($extractFunc)) {
    if (!is_callable($extractFunc)) {
      throw new \Exception('$extractFunc must be callable');
    }
    $this->extractFunc = $extractFunc;
  }
  else {
    $this->extractFunc = function ($value) {
      return $value;
    };
  }
  if (isset($hydrateFunc)) {
    if (!is_callable($hydrateFunc)) {
      throw new \Exception('$hydrateFunc must be callable');
    }
    $this->hydrateFunc = $hydrateFunc;
  }
  else {
    $this->hydrateFunc = function ($value) {
      return $value;
    };
  }
}