Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
73.33% |
22 / 30 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| DatatableRequest | |
73.33% |
22 / 30 |
|
62.50% |
5 / 8 |
20.85 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getRows | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| getPage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getSortBy | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getSortOrder | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| getSortType | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getFilters | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| fromGlobals | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\tao\model\datatable\implementation; |
| 23 | |
| 24 | use oat\tao\model\datatable\DatatableRequest as DatatableRequestInterface; |
| 25 | use Psr\Http\Message\ServerRequestInterface; |
| 26 | use GuzzleHttp\Psr7\ServerRequest; |
| 27 | |
| 28 | /** |
| 29 | * Class DatatableRequest |
| 30 | * @package oat\tao\model\datatable |
| 31 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 32 | */ |
| 33 | class DatatableRequest implements DatatableRequestInterface |
| 34 | { |
| 35 | public const DEFAULT_ROWS = 25; |
| 36 | public const DEFAULT_PAGE = 1; |
| 37 | public const DEFAULT_SORT_BY = null; |
| 38 | public const DEFAULT_SORT_ORDER = 'asc'; |
| 39 | public const DEFAULT_SORT_TYPE = 'string'; |
| 40 | public const DEFAULT_FILTERS = []; |
| 41 | |
| 42 | /** |
| 43 | * @var array |
| 44 | */ |
| 45 | private $requestParams; |
| 46 | |
| 47 | /** |
| 48 | * DatatableRequest constructor. |
| 49 | * @param ServerRequestInterface $request |
| 50 | */ |
| 51 | public function __construct(ServerRequestInterface $request) |
| 52 | { |
| 53 | $bodyParams = $request->getParsedBody(); |
| 54 | if ($bodyParams === null) { |
| 55 | $bodyParams = []; |
| 56 | } |
| 57 | $queryParams = $request->getQueryParams(); |
| 58 | $this->requestParams = array_merge($bodyParams, $queryParams); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get amount of records per page |
| 63 | * @return integer |
| 64 | */ |
| 65 | public function getRows() |
| 66 | { |
| 67 | $rows = isset($this->requestParams[self::PARAM_ROWS]) |
| 68 | ? $this->requestParams[self::PARAM_ROWS] |
| 69 | : self::DEFAULT_ROWS; |
| 70 | |
| 71 | return (int)$rows; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get page number |
| 76 | * @return integer |
| 77 | */ |
| 78 | public function getPage() |
| 79 | { |
| 80 | $page = $this->requestParams[self::PARAM_PAGE] ?? self::DEFAULT_PAGE; |
| 81 | return (int)$page; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get sorting column name |
| 86 | * @return string |
| 87 | */ |
| 88 | public function getSortBy() |
| 89 | { |
| 90 | $sortBy = isset($this->requestParams[self::PARAM_SORT_BY]) ? |
| 91 | $this->requestParams[self::PARAM_SORT_BY] : self::DEFAULT_SORT_BY; |
| 92 | return $sortBy; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Get sorting direction |
| 97 | * @return string |
| 98 | */ |
| 99 | public function getSortOrder() |
| 100 | { |
| 101 | $sortOrder = isset($this->requestParams[self::PARAM_SORT_ORDER]) ? |
| 102 | $this->requestParams[self::PARAM_SORT_ORDER] : self::DEFAULT_SORT_ORDER; |
| 103 | $sortOrder = mb_strtolower($sortOrder); |
| 104 | |
| 105 | if (!in_array($sortOrder, ['asc', 'desc'])) { |
| 106 | $sortOrder = self::DEFAULT_SORT_ORDER; |
| 107 | } |
| 108 | |
| 109 | return $sortOrder; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Get sorting type |
| 114 | * @return string |
| 115 | */ |
| 116 | public function getSortType() |
| 117 | { |
| 118 | $sortType = isset($this->requestParams[self::PARAM_SORT_TYPE]) ? |
| 119 | $this->requestParams[self::PARAM_SORT_TYPE] : self::DEFAULT_SORT_TYPE; |
| 120 | $sortType = mb_strtolower($sortType); |
| 121 | |
| 122 | if (!in_array($sortType, ['string', 'numeric'])) { |
| 123 | $sortType = self::DEFAULT_SORT_TYPE; |
| 124 | } |
| 125 | |
| 126 | return $sortType; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Get filters |
| 131 | * @return array |
| 132 | */ |
| 133 | public function getFilters() |
| 134 | { |
| 135 | $filters = isset($this->requestParams[self::PARAM_FILTERS]) ? |
| 136 | $this->requestParams[self::PARAM_FILTERS] : self::DEFAULT_FILTERS; |
| 137 | |
| 138 | return $filters; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get DatatableRequest populated from superglobal arrays |
| 143 | * @return DatatableRequestInterface |
| 144 | */ |
| 145 | public static function fromGlobals() |
| 146 | { |
| 147 | return new static(ServerRequest::fromGlobals()); |
| 148 | } |
| 149 | } |