@@ -60,7 +60,7 @@ |
||
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | foreach ($filterArray as $param => $filters) { |
| 63 | - if(!\is_string($param)){ |
|
| 63 | + if (!\is_string($param)) { |
|
| 64 | 64 | throw new InvalidParamException(sprintf('Key in FILTER param is not a string')); |
| 65 | 65 | } |
| 66 | 66 | if (\is_array($filters)) { |
@@ -15,168 +15,168 @@ |
||
| 15 | 15 | |
| 16 | 16 | class RequestParser |
| 17 | 17 | { |
| 18 | - public const SORT_IDENTIFIER = 'sort'; |
|
| 19 | - public const FILTER_IDENTIFIER = 'filter'; |
|
| 20 | - |
|
| 21 | - public const LIMIT_IDENTIFIER = 'limit'; |
|
| 22 | - public const OFFSET_IDENTIFIER = 'offset'; |
|
| 23 | - public const PAGE_IDENTIFIER = 'page'; |
|
| 24 | - public const PHRASE_IDENTIFIER = 'phrase'; |
|
| 25 | - |
|
| 26 | - |
|
| 27 | - private $queryParams = []; |
|
| 28 | - /** |
|
| 29 | - * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort |
|
| 30 | - */ |
|
| 31 | - private $sort; |
|
| 32 | - /** |
|
| 33 | - * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter |
|
| 34 | - */ |
|
| 35 | - private $filter; |
|
| 36 | - /** |
|
| 37 | - * @var integer|null |
|
| 38 | - */ |
|
| 39 | - private $limit; |
|
| 40 | - /** |
|
| 41 | - * @var integer|null |
|
| 42 | - */ |
|
| 43 | - private $page; |
|
| 44 | - /** |
|
| 45 | - * @var integer|null |
|
| 46 | - */ |
|
| 47 | - private $offset; |
|
| 48 | - /** |
|
| 49 | - * @var string|null |
|
| 50 | - */ |
|
| 51 | - private $phrase; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * RequestParser constructor. |
|
| 55 | - * |
|
| 56 | - * @param string $query |
|
| 57 | - * |
|
| 58 | - * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception |
|
| 59 | - * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException |
|
| 60 | - * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException |
|
| 61 | - */ |
|
| 62 | - public function __construct(string $query) |
|
| 63 | - { |
|
| 64 | - $this->parse($query); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter |
|
| 69 | - */ |
|
| 70 | - public function getFilter(): Filter |
|
| 71 | - { |
|
| 72 | - return $this->filter; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * @param int|null $default |
|
| 77 | - * |
|
| 78 | - * @return int|null |
|
| 79 | - */ |
|
| 80 | - public function getLimit(int $default = null): ?int |
|
| 81 | - { |
|
| 82 | - return $this->limit ?? $default; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @param int|null $default |
|
| 87 | - * |
|
| 88 | - * @return int|null |
|
| 89 | - */ |
|
| 90 | - public function getOffset(int $default = null): ?int |
|
| 91 | - { |
|
| 92 | - return $this->offset ?? $default; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @param int|null $default |
|
| 97 | - * |
|
| 98 | - * @return int|null |
|
| 99 | - */ |
|
| 100 | - public function getPage(int $default = null): ?int |
|
| 101 | - { |
|
| 102 | - return $this->page ?? $default; |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * @return null|string |
|
| 107 | - */ |
|
| 108 | - public function getPhrase(): ?string |
|
| 109 | - { |
|
| 110 | - return $this->phrase; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @param string $param |
|
| 115 | - * @param string|null $type If NULL return value AS IS |
|
| 116 | - * @param mixed|null $default |
|
| 117 | - * |
|
| 118 | - * @return mixed|null |
|
| 119 | - */ |
|
| 120 | - public function getQueryParam(string $param, string $type = null, $default = null) |
|
| 121 | - { |
|
| 122 | - if (isset($this->queryParams[$param])) { |
|
| 123 | - if ($type !== null) { |
|
| 124 | - $type = strtolower($type); |
|
| 125 | - if (!\in_array($type, ['boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array'])) { |
|
| 126 | - throw new \InvalidArgumentException(sprintf('Unsupported type [%s]', $type)); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $var = $this->queryParams[$param]; |
|
| 130 | - |
|
| 131 | - if ($type === 'array' && \is_string($var)) { |
|
| 132 | - $var = explode(',', $var); |
|
| 133 | - } elseif ($type === 'string' && \is_array($var)) { |
|
| 134 | - $var = implode(',', $var); |
|
| 135 | - } elseif (!settype($var, strtolower($type))) { |
|
| 136 | - throw new \RuntimeException(sprintf('Cannot set type [%s]', $type)); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $var; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $this->queryParams[$param]; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - return $default ?? null; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort |
|
| 150 | - */ |
|
| 151 | - public function getSort(): Sort |
|
| 152 | - { |
|
| 153 | - return $this->sort; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @param string $query |
|
| 158 | - * |
|
| 159 | - * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException |
|
| 160 | - * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException |
|
| 161 | - */ |
|
| 162 | - private function parse(string $query): void |
|
| 163 | - { |
|
| 164 | - parse_str($query, $this->queryParams); |
|
| 165 | - |
|
| 166 | - $this->sort = new Sort($this->getQueryParam(self::SORT_IDENTIFIER, 'string')); |
|
| 167 | - $this->filter = new Filter($this->getQueryParam(self::FILTER_IDENTIFIER, 'array', [])); |
|
| 168 | - $this->limit = $this->getQueryParam('limit', 'int'); |
|
| 169 | - if (null !== $this->limit && $this->limit < 0) { |
|
| 170 | - throw new InvalidParamException('Limit value cannot be lower than 0'); |
|
| 171 | - } |
|
| 172 | - $this->offset = $this->getQueryParam('offset', 'int'); |
|
| 173 | - if (null !== $this->offset && $this->offset < 0) { |
|
| 174 | - throw new InvalidParamException('Offset value cannot be lower than 0'); |
|
| 175 | - } |
|
| 176 | - $this->page = $this->getQueryParam('page', 'int'); |
|
| 177 | - if (null !== $this->page && $this->page < 0) { |
|
| 178 | - throw new InvalidParamException('Page value cannot be lower than 0'); |
|
| 179 | - } |
|
| 180 | - $this->phrase = $this->getQueryParam('phrase', 'string'); |
|
| 181 | - } |
|
| 18 | + public const SORT_IDENTIFIER = 'sort'; |
|
| 19 | + public const FILTER_IDENTIFIER = 'filter'; |
|
| 20 | + |
|
| 21 | + public const LIMIT_IDENTIFIER = 'limit'; |
|
| 22 | + public const OFFSET_IDENTIFIER = 'offset'; |
|
| 23 | + public const PAGE_IDENTIFIER = 'page'; |
|
| 24 | + public const PHRASE_IDENTIFIER = 'phrase'; |
|
| 25 | + |
|
| 26 | + |
|
| 27 | + private $queryParams = []; |
|
| 28 | + /** |
|
| 29 | + * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort |
|
| 30 | + */ |
|
| 31 | + private $sort; |
|
| 32 | + /** |
|
| 33 | + * @var \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter |
|
| 34 | + */ |
|
| 35 | + private $filter; |
|
| 36 | + /** |
|
| 37 | + * @var integer|null |
|
| 38 | + */ |
|
| 39 | + private $limit; |
|
| 40 | + /** |
|
| 41 | + * @var integer|null |
|
| 42 | + */ |
|
| 43 | + private $page; |
|
| 44 | + /** |
|
| 45 | + * @var integer|null |
|
| 46 | + */ |
|
| 47 | + private $offset; |
|
| 48 | + /** |
|
| 49 | + * @var string|null |
|
| 50 | + */ |
|
| 51 | + private $phrase; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * RequestParser constructor. |
|
| 55 | + * |
|
| 56 | + * @param string $query |
|
| 57 | + * |
|
| 58 | + * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception |
|
| 59 | + * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException |
|
| 60 | + * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException |
|
| 61 | + */ |
|
| 62 | + public function __construct(string $query) |
|
| 63 | + { |
|
| 64 | + $this->parse($query); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Filter |
|
| 69 | + */ |
|
| 70 | + public function getFilter(): Filter |
|
| 71 | + { |
|
| 72 | + return $this->filter; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * @param int|null $default |
|
| 77 | + * |
|
| 78 | + * @return int|null |
|
| 79 | + */ |
|
| 80 | + public function getLimit(int $default = null): ?int |
|
| 81 | + { |
|
| 82 | + return $this->limit ?? $default; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @param int|null $default |
|
| 87 | + * |
|
| 88 | + * @return int|null |
|
| 89 | + */ |
|
| 90 | + public function getOffset(int $default = null): ?int |
|
| 91 | + { |
|
| 92 | + return $this->offset ?? $default; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @param int|null $default |
|
| 97 | + * |
|
| 98 | + * @return int|null |
|
| 99 | + */ |
|
| 100 | + public function getPage(int $default = null): ?int |
|
| 101 | + { |
|
| 102 | + return $this->page ?? $default; |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * @return null|string |
|
| 107 | + */ |
|
| 108 | + public function getPhrase(): ?string |
|
| 109 | + { |
|
| 110 | + return $this->phrase; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @param string $param |
|
| 115 | + * @param string|null $type If NULL return value AS IS |
|
| 116 | + * @param mixed|null $default |
|
| 117 | + * |
|
| 118 | + * @return mixed|null |
|
| 119 | + */ |
|
| 120 | + public function getQueryParam(string $param, string $type = null, $default = null) |
|
| 121 | + { |
|
| 122 | + if (isset($this->queryParams[$param])) { |
|
| 123 | + if ($type !== null) { |
|
| 124 | + $type = strtolower($type); |
|
| 125 | + if (!\in_array($type, ['boolean', 'bool', 'integer', 'int', 'float', 'double', 'string', 'array'])) { |
|
| 126 | + throw new \InvalidArgumentException(sprintf('Unsupported type [%s]', $type)); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $var = $this->queryParams[$param]; |
|
| 130 | + |
|
| 131 | + if ($type === 'array' && \is_string($var)) { |
|
| 132 | + $var = explode(',', $var); |
|
| 133 | + } elseif ($type === 'string' && \is_array($var)) { |
|
| 134 | + $var = implode(',', $var); |
|
| 135 | + } elseif (!settype($var, strtolower($type))) { |
|
| 136 | + throw new \RuntimeException(sprintf('Cannot set type [%s]', $type)); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $var; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + return $this->queryParams[$param]; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + return $default ?? null; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @return \mrcnpdlk\Lib\UrlSearchParser\Criteria\Sort |
|
| 150 | + */ |
|
| 151 | + public function getSort(): Sort |
|
| 152 | + { |
|
| 153 | + return $this->sort; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @param string $query |
|
| 158 | + * |
|
| 159 | + * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\EmptyParamException |
|
| 160 | + * @throws \mrcnpdlk\Lib\UrlSearchParser\Exception\InvalidParamException |
|
| 161 | + */ |
|
| 162 | + private function parse(string $query): void |
|
| 163 | + { |
|
| 164 | + parse_str($query, $this->queryParams); |
|
| 165 | + |
|
| 166 | + $this->sort = new Sort($this->getQueryParam(self::SORT_IDENTIFIER, 'string')); |
|
| 167 | + $this->filter = new Filter($this->getQueryParam(self::FILTER_IDENTIFIER, 'array', [])); |
|
| 168 | + $this->limit = $this->getQueryParam('limit', 'int'); |
|
| 169 | + if (null !== $this->limit && $this->limit < 0) { |
|
| 170 | + throw new InvalidParamException('Limit value cannot be lower than 0'); |
|
| 171 | + } |
|
| 172 | + $this->offset = $this->getQueryParam('offset', 'int'); |
|
| 173 | + if (null !== $this->offset && $this->offset < 0) { |
|
| 174 | + throw new InvalidParamException('Offset value cannot be lower than 0'); |
|
| 175 | + } |
|
| 176 | + $this->page = $this->getQueryParam('page', 'int'); |
|
| 177 | + if (null !== $this->page && $this->page < 0) { |
|
| 178 | + throw new InvalidParamException('Page value cannot be lower than 0'); |
|
| 179 | + } |
|
| 180 | + $this->phrase = $this->getQueryParam('phrase', 'string'); |
|
| 181 | + } |
|
| 182 | 182 | } |