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

BooleanStrategy.php

Zuletzt modifiziert: 09.10.2024, 12:58 - Dateigröße: 3.21 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  use Zend\Stdlib\Exception\InvalidArgumentException;
013   
014  /**
015   * This Strategy extracts and hydrates int and string values to Boolean values
016   *
017   * @package Zend\Stdlib\Hydrator\Strategy
018   */
019  final class BooleanStrategy implements StrategyInterface
020  {
021      /**
022       * @var int|string
023       */
024      private $trueValue;
025   
026      /**
027       * @var int|string
028       */
029      private $falseValue;
030   
031      /**
032       * @param int|string $trueValue
033       * @param int|string $falseValue
034       * @throws InvalidArgumentException
035       */
036      public function __construct($trueValue, $falseValue)
037      {
038          if (!is_int($trueValue) && !is_string($trueValue)) {
039              throw new InvalidArgumentException(sprintf(
040                  'Unable to instantiate BooleanStrategy. Expected int or string as $trueValue. %s was given',
041                  is_object($trueValue) ? get_class($trueValue) : gettype($trueValue)
042              ));
043          }
044   
045          if (!is_int($falseValue) && !is_string($falseValue)) {
046              throw new InvalidArgumentException(sprintf(
047                  'Unable to instantiate BooleanStrategy. Expected int or string as $falseValue. %s was given',
048                  is_object($falseValue) ? get_class($falseValue) : gettype($falseValue)
049              ));
050          }
051   
052          $this->trueValue  = $trueValue;
053          $this->falseValue = $falseValue;
054      }
055   
056      /**
057       * Converts the given value so that it can be extracted by the hydrator.
058       *
059       * @param  bool $value The original value.
060       * @throws InvalidArgumentException
061       * @return int|string Returns the value that should be extracted.
062       */
063      public function extract($value)
064      {
065          if (!is_bool($value)) {
066              throw new InvalidArgumentException(sprintf(
067                  'Unable to extract. Expected bool. %s was given.',
068                  is_object($value) ? get_class($value) : gettype($value)
069              ));
070          }
071   
072          return $value === true ? $this->trueValue : $this->falseValue;
073      }
074   
075      /**
076       * Converts the given value so that it can be hydrated by the hydrator.
077       *
078       * @param  int|string $value The original value.
079       * @throws InvalidArgumentException
080       * @return bool Returns the value that should be hydrated.
081       */
082      public function hydrate($value)
083      {
084          if (!is_string($value) && !is_int($value)) {
085              throw new InvalidArgumentException(sprintf(
086                  'Unable to hydrate. Expected string or int. %s was given.',
087                  is_object($value) ? get_class($value) : gettype($value)
088              ));
089          }
090   
091          if ($value === $this->trueValue) {
092              return true;
093          }
094   
095          if ($value === $this->falseValue) {
096              return false;
097          }
098   
099          throw new InvalidArgumentException(sprintf(
100              'Unexpected value %s can\'t be hydrated. Expect %s or %s as Value.',
101              $value,
102              $this->trueValue,
103              $this->falseValue
104          ));
105      }
106  }
107