[ Index ] |
PHP Cross Reference of phpBB-3.3.14-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace Zend\Code; 4 5 use Zend\Code\Exception\InvalidArgumentException; 6 7 class DeclareStatement 8 { 9 public const TICKS = 'ticks'; 10 public const STRICT_TYPES = 'strict_types'; 11 public const ENCODING = 'encoding'; 12 13 private const ALLOWED = [ 14 self::TICKS => 'integer', 15 self::STRICT_TYPES => 'integer', 16 self::ENCODING => 'string', 17 ]; 18 19 /** 20 * @var string 21 */ 22 protected $directive; 23 24 /** 25 * @var int|string 26 */ 27 protected $value; 28 29 private function __construct(string $directive, $value) 30 { 31 $this->directive = $directive; 32 $this->value = $value; 33 } 34 35 /** 36 * @return string 37 */ 38 public function getDirective(): string 39 { 40 return $this->directive; 41 } 42 43 /** 44 * @return int|string 45 */ 46 public function getValue() 47 { 48 return $this->value; 49 } 50 51 /** 52 * @param int $value 53 * @return self 54 */ 55 public static function ticks(int $value): self 56 { 57 return new self(self::TICKS, $value); 58 } 59 60 /** 61 * @param int $value 62 * @return self 63 */ 64 public static function strictTypes(int $value): self 65 { 66 return new self(self::STRICT_TYPES, $value); 67 } 68 69 /** 70 * @param string $value 71 * @return self 72 */ 73 public static function encoding(string $value): self 74 { 75 return new self(self::ENCODING, $value); 76 } 77 78 public static function fromArray(array $config): self 79 { 80 $directive = key($config); 81 $value = $config[$directive]; 82 83 if (! isset(self::ALLOWED[$directive])) { 84 throw new InvalidArgumentException( 85 sprintf( 86 'Declare directive must be one of: %s.', 87 implode(', ', array_keys(self::ALLOWED)) 88 ) 89 ); 90 } 91 92 if (gettype($value) !== self::ALLOWED[$directive]) { 93 throw new InvalidArgumentException( 94 sprintf( 95 'Declare value invalid. Expected %s, got %s.', 96 self::ALLOWED[$directive], 97 gettype($value) 98 ) 99 ); 100 } 101 102 $method = str_replace('_', '', lcfirst(ucwords($directive, '_'))); 103 104 return self::{$method}($value); 105 } 106 107 /** 108 * @return string 109 */ 110 public function getStatement(): string 111 { 112 $value = is_string($this->value) ? '\'' . $this->value . '\'' : $this->value; 113 114 return sprintf('declare(%s=%s);', $this->directive, $value); 115 } 116 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Mon Nov 25 19:05:08 2024 | Cross-referenced by PHPXref 0.7.1 |