Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

ClosureStrategy.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 2.89 KiB


001  <?php
002  /**
003   * Zend Framework (http://framework.zend.com/)
004   *
005   * @link      http://github.com/zendframework/zf2 for the canonical source repository
006   * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
007   * @license   http://framework.zend.com/license/new-bsd New BSD License
008   */
009   
010  namespace Zend\Stdlib\Hydrator\Strategy;
011   
012  class ClosureStrategy implements StrategyInterface
013  {
014      /**
015       * Function, used in extract method, default:
016       * function ($value) {
017       *     return $value;
018       * };
019       * @var callable
020       */
021      protected $extractFunc = null;
022   
023      /**
024       * Function, used in hydrate method, default:
025       * function ($value) {
026       *     return $value;
027       * };
028       * @var callable
029       */
030      protected $hydrateFunc = null;
031   
032      /**
033       * You can describe how your values will extract and hydrate, like this:
034       * $hydrator->addStrategy('category', new ClosureStrategy(
035       *     function (Category $value) {
036       *         return (int) $value->id;
037       *     },
038       *     function ($value) {
039       *         return new Category((int) $value);
040       *     }
041       * ));
042       *
043       * @param callable $extractFunc - anonymous function, that extract values
044       * from object
045       * @param callable $hydrateFunc - anonymous function, that hydrate values
046       * into object
047       */
048      public function __construct($extractFunc = null, $hydrateFunc = null)
049      {
050          if (isset($extractFunc)) {
051              if (!is_callable($extractFunc)) {
052                  throw new \Exception('$extractFunc must be callable');
053              }
054   
055              $this->extractFunc = $extractFunc;
056          } else {
057              $this->extractFunc = function ($value) {
058                  return $value;
059              };
060          }
061   
062          if (isset($hydrateFunc)) {
063              if (!is_callable($hydrateFunc)) {
064                  throw new \Exception('$hydrateFunc must be callable');
065              }
066   
067              $this->hydrateFunc = $hydrateFunc;
068          } else {
069              $this->hydrateFunc = function ($value) {
070                  return $value;
071              };
072          }
073      }
074   
075      /**
076       * Converts the given value so that it can be extracted by the hydrator.
077       *
078       * @param  mixed $value  The original value.
079       * @param  array $object The object is optionally provided as context.
080       * @return mixed Returns the value that should be extracted.
081       */
082      public function extract($value, $object = null)
083      {
084          $func = $this->extractFunc;
085   
086          return $func($value, $object);
087      }
088   
089      /**
090       * Converts the given value so that it can be hydrated by the hydrator.
091       *
092       * @param  mixed $value The original value.
093       * @param  array $data  The whole data is optionally provided as context.
094       * @return mixed Returns the value that should be hydrated.
095       */
096      public function hydrate($value, $data = null)
097      {
098          $func = $this->hydrateFunc;
099   
100          return $func($value, $data);
101      }
102  }
103