[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10 namespace Zend\Stdlib\Hydrator\Strategy; 11 12 use Zend\Stdlib\Exception\InvalidArgumentException; 13 14 /** 15 * This Strategy extracts and hydrates int and string values to Boolean values 16 * 17 * @package Zend\Stdlib\Hydrator\Strategy 18 */ 19 final class BooleanStrategy implements StrategyInterface 20 { 21 /** 22 * @var int|string 23 */ 24 private $trueValue; 25 26 /** 27 * @var int|string 28 */ 29 private $falseValue; 30 31 /** 32 * @param int|string $trueValue 33 * @param int|string $falseValue 34 * @throws InvalidArgumentException 35 */ 36 public function __construct($trueValue, $falseValue) 37 { 38 if (!is_int($trueValue) && !is_string($trueValue)) { 39 throw new InvalidArgumentException(sprintf( 40 'Unable to instantiate BooleanStrategy. Expected int or string as $trueValue. %s was given', 41 is_object($trueValue) ? get_class($trueValue) : gettype($trueValue) 42 )); 43 } 44 45 if (!is_int($falseValue) && !is_string($falseValue)) { 46 throw new InvalidArgumentException(sprintf( 47 'Unable to instantiate BooleanStrategy. Expected int or string as $falseValue. %s was given', 48 is_object($falseValue) ? get_class($falseValue) : gettype($falseValue) 49 )); 50 } 51 52 $this->trueValue = $trueValue; 53 $this->falseValue = $falseValue; 54 } 55 56 /** 57 * Converts the given value so that it can be extracted by the hydrator. 58 * 59 * @param bool $value The original value. 60 * @throws InvalidArgumentException 61 * @return int|string Returns the value that should be extracted. 62 */ 63 public function extract($value) 64 { 65 if (!is_bool($value)) { 66 throw new InvalidArgumentException(sprintf( 67 'Unable to extract. Expected bool. %s was given.', 68 is_object($value) ? get_class($value) : gettype($value) 69 )); 70 } 71 72 return $value === true ? $this->trueValue : $this->falseValue; 73 } 74 75 /** 76 * Converts the given value so that it can be hydrated by the hydrator. 77 * 78 * @param int|string $value The original value. 79 * @throws InvalidArgumentException 80 * @return bool Returns the value that should be hydrated. 81 */ 82 public function hydrate($value) 83 { 84 if (!is_string($value) && !is_int($value)) { 85 throw new InvalidArgumentException(sprintf( 86 'Unable to hydrate. Expected string or int. %s was given.', 87 is_object($value) ? get_class($value) : gettype($value) 88 )); 89 } 90 91 if ($value === $this->trueValue) { 92 return true; 93 } 94 95 if ($value === $this->falseValue) { 96 return false; 97 } 98 99 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 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |