.github
2 years ago
Dsn.php
4 years ago
InvalidQueryParameterTypeException.php
4 years ago
LICENSE
4 years ago
QueryBag.php
4 years ago
README.md
4 years ago
composer.json
2 years ago
QueryBag.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Enqueue\Dsn; |
| 6 | |
| 7 | class QueryBag |
| 8 | { |
| 9 | /** |
| 10 | * @var array |
| 11 | */ |
| 12 | private $query; |
| 13 | |
| 14 | public function __construct(array $query) |
| 15 | { |
| 16 | $this->query = $query; |
| 17 | } |
| 18 | |
| 19 | public function toArray(): array |
| 20 | { |
| 21 | return $this->query; |
| 22 | } |
| 23 | |
| 24 | public function getString(string $name, string $default = null): ?string |
| 25 | { |
| 26 | return array_key_exists($name, $this->query) ? $this->query[$name] : $default; |
| 27 | } |
| 28 | |
| 29 | public function getDecimal(string $name, int $default = null): ?int |
| 30 | { |
| 31 | $value = $this->getString($name); |
| 32 | if (null === $value) { |
| 33 | return $default; |
| 34 | } |
| 35 | |
| 36 | if (false == preg_match('/^[\+\-]?[0-9]*$/', $value)) { |
| 37 | throw InvalidQueryParameterTypeException::create($name, 'decimal'); |
| 38 | } |
| 39 | |
| 40 | return (int) $value; |
| 41 | } |
| 42 | |
| 43 | public function getOctal(string $name, int $default = null): ?int |
| 44 | { |
| 45 | $value = $this->getString($name); |
| 46 | if (null === $value) { |
| 47 | return $default; |
| 48 | } |
| 49 | |
| 50 | if (false == preg_match('/^0[\+\-]?[0-7]*$/', $value)) { |
| 51 | throw InvalidQueryParameterTypeException::create($name, 'octal'); |
| 52 | } |
| 53 | |
| 54 | return intval($value, 8); |
| 55 | } |
| 56 | |
| 57 | public function getFloat(string $name, float $default = null): ?float |
| 58 | { |
| 59 | $value = $this->getString($name); |
| 60 | if (null === $value) { |
| 61 | return $default; |
| 62 | } |
| 63 | |
| 64 | if (false == is_numeric($value)) { |
| 65 | throw InvalidQueryParameterTypeException::create($name, 'float'); |
| 66 | } |
| 67 | |
| 68 | return (float) $value; |
| 69 | } |
| 70 | |
| 71 | public function getBool(string $name, bool $default = null): ?bool |
| 72 | { |
| 73 | $value = $this->getString($name); |
| 74 | if (null === $value) { |
| 75 | return $default; |
| 76 | } |
| 77 | |
| 78 | if (in_array($value, ['', '0', 'false'], true)) { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | if (in_array($value, ['1', 'true'], true)) { |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | throw InvalidQueryParameterTypeException::create($name, 'bool'); |
| 87 | } |
| 88 | |
| 89 | public function getArray(string $name, array $default = []): self |
| 90 | { |
| 91 | if (false == array_key_exists($name, $this->query)) { |
| 92 | return new self($default); |
| 93 | } |
| 94 | |
| 95 | $value = $this->query[$name]; |
| 96 | |
| 97 | if (is_array($value)) { |
| 98 | return new self($value); |
| 99 | } |
| 100 | |
| 101 | throw InvalidQueryParameterTypeException::create($name, 'array'); |
| 102 | } |
| 103 | } |
| 104 |