You are here

class ClosureStrategy in Zircon Profile 8

Same name in this branch
  1. 8 vendor/zendframework/zend-hydrator/src/Strategy/ClosureStrategy.php \Zend\Hydrator\Strategy\ClosureStrategy
  2. 8 vendor/zendframework/zend-stdlib/src/Hydrator/Strategy/ClosureStrategy.php \Zend\Stdlib\Hydrator\Strategy\ClosureStrategy
Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-hydrator/src/Strategy/ClosureStrategy.php \Zend\Hydrator\Strategy\ClosureStrategy

Hierarchy

Expanded class hierarchy of ClosureStrategy

1 file declares its use of ClosureStrategy
ClosureStrategy.php in vendor/zendframework/zend-stdlib/src/Hydrator/Strategy/ClosureStrategy.php

File

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

Namespace

Zend\Hydrator\Strategy
View source
class ClosureStrategy implements StrategyInterface {

  /**
   * Function, used in extract method, default:
   *
   * <code>
   * function ($value) {
   *     return $value;
   * };
   * </code>
   *
   * @var callable
   */
  protected $extractFunc = null;

  /**
   * Function, used in hydrate method, default:
   *
   * <code>
   * function ($value) {
   *     return $value;
   * };
   * </code>
   *
   * @var callable
   */
  protected $hydrateFunc = null;

  /**
   * 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>
   *
   * @param callable $extractFunc - anonymous function, that extract values
   *     from object
   * @param callable $hydrateFunc - anonymous function, that hydrate values
   *     into object
   */
  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;
      };
    }
  }

  /**
   * Converts the given value so that it can be extracted by the hydrator.
   *
   * @param  mixed $value  The original value.
   * @param  array $object The object is optionally provided as context.
   * @return mixed Returns the value that should be extracted.
   */
  public function extract($value, $object = null) {
    $func = $this->extractFunc;
    return $func($value, $object);
  }

  /**
   * Converts the given value so that it can be hydrated by the hydrator.
   *
   * @param  mixed $value The original value.
   * @param  array $data  The whole data is optionally provided as context.
   * @return mixed Returns the value that should be hydrated.
   */
  public function hydrate($value, $data = null) {
    $func = $this->hydrateFunc;
    return $func($value, $data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClosureStrategy::$extractFunc protected property Function, used in extract method, default:
ClosureStrategy::$hydrateFunc protected property Function, used in hydrate method, default:
ClosureStrategy::extract public function Converts the given value so that it can be extracted by the hydrator. Overrides StrategyInterface::extract
ClosureStrategy::hydrate public function Converts the given value so that it can be hydrated by the hydrator. Overrides StrategyInterface::hydrate
ClosureStrategy::__construct public function You can describe how your values will extract and hydrate, like this: