[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of JSON-API. 5 * 6 * (c) Toby Zerner <[email protected]> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Tobscure\JsonApi; 13 14 use Tobscure\JsonApi\Exception\InvalidParameterException; 15 16 class Parameters 17 { 18 /** 19 * @var array 20 */ 21 protected $input; 22 23 /** 24 * @param array $input 25 */ 26 public function __construct(array $input) 27 { 28 $this->input = $input; 29 } 30 31 /** 32 * Get the includes. 33 * 34 * @param array $available 35 * 36 * @throws \Tobscure\JsonApi\Exception\InvalidParameterException 37 * 38 * @return array 39 */ 40 public function getInclude(array $available = []) 41 { 42 if ($include = $this->getInput('include')) { 43 $relationships = explode(',', $include); 44 45 $invalid = array_diff($relationships, $available); 46 47 if (count($invalid)) { 48 throw new InvalidParameterException( 49 'Invalid includes ['.implode(',', $invalid).']', 50 1, 51 null, 52 'include' 53 ); 54 } 55 56 return $relationships; 57 } 58 59 return []; 60 } 61 62 /** 63 * Get number of offset. 64 * 65 * @param int|null $perPage 66 * 67 * @throws \Tobscure\JsonApi\Exception\InvalidParameterException 68 * 69 * @return int 70 */ 71 public function getOffset($perPage = null) 72 { 73 if ($perPage && ($offset = $this->getOffsetFromNumber($perPage))) { 74 return $offset; 75 } 76 77 $offset = (int) $this->getPage('offset'); 78 79 if ($offset < 0) { 80 throw new InvalidParameterException('page[offset] must be >=0', 2, null, 'page[offset]'); 81 } 82 83 return $offset; 84 } 85 86 /** 87 * Calculate the offset based on the page[number] parameter. 88 * 89 * @param int $perPage 90 * 91 * @throws \Tobscure\JsonApi\Exception\InvalidParameterException 92 * 93 * @return int 94 */ 95 protected function getOffsetFromNumber($perPage) 96 { 97 $page = (int) $this->getPage('number'); 98 99 if ($page <= 1) { 100 return 0; 101 } 102 103 return ($page - 1) * $perPage; 104 } 105 106 /** 107 * Get the limit. 108 * 109 * @param int|null $max 110 * 111 * @return int|null 112 */ 113 public function getLimit($max = null) 114 { 115 $limit = $this->getPage('limit') ?: $this->getPage('size') ?: null; 116 117 if ($limit && $max) { 118 $limit = min($max, $limit); 119 } 120 121 return $limit; 122 } 123 124 /** 125 * Get the sort. 126 * 127 * @param array $available 128 * 129 * @throws \Tobscure\JsonApi\Exception\InvalidParameterException 130 * 131 * @return array 132 */ 133 public function getSort(array $available = []) 134 { 135 $sort = []; 136 137 if ($input = $this->getInput('sort')) { 138 $fields = explode(',', $input); 139 140 foreach ($fields as $field) { 141 if (substr($field, 0, 1) === '-') { 142 $field = substr($field, 1); 143 $order = 'desc'; 144 } else { 145 $order = 'asc'; 146 } 147 148 $sort[$field] = $order; 149 } 150 151 $invalid = array_diff(array_keys($sort), $available); 152 153 if (count($invalid)) { 154 throw new InvalidParameterException( 155 'Invalid sort fields ['.implode(',', $invalid).']', 156 3, 157 null, 158 'sort' 159 ); 160 } 161 } 162 163 return $sort; 164 } 165 166 /** 167 * Get the fields requested for inclusion. 168 * 169 * @return array 170 */ 171 public function getFields() 172 { 173 $fields = $this->getInput('fields'); 174 175 if (! is_array($fields)) { 176 return []; 177 } 178 179 return array_map(function ($fields) { 180 return explode(',', $fields); 181 }, $fields); 182 } 183 184 /** 185 * Get a filter item. 186 * 187 * @return mixed 188 */ 189 public function getFilter() 190 { 191 return $this->getInput('filter'); 192 } 193 194 /** 195 * Get an input item. 196 * 197 * @param string $key 198 * @param null $default 199 * 200 * @return mixed 201 */ 202 protected function getInput($key, $default = null) 203 { 204 return isset($this->input[$key]) ? $this->input[$key] : $default; 205 } 206 207 /** 208 * Get the page. 209 * 210 * @param string $key 211 * 212 * @return string 213 */ 214 protected function getPage($key) 215 { 216 $page = $this->getInput('page'); 217 218 return isset($page[$key]) ? $page[$key] : ''; 219 } 220 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |