1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle 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 Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Integer comparison filter to allow a comparison such as "> 42". 19 * 20 * @package core 21 * @category table 22 * @copyright 2020 Andrew Nicols <andrew@nicols.co.uk> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 declare(strict_types=1); 27 28 namespace core_table\local\filter; 29 30 use InvalidArgumentException; 31 use TypeError; 32 33 /** 34 * Class representing an integer filter. 35 * 36 * @package core 37 * @copyright 2020 Andrew Nicols <andrew@nicols.co.uk> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class numeric_comparison_filter extends filter { 41 /** 42 * Get the authoritative direction. 43 * 44 * @param string $direction The supplied direction 45 * @return string The authoritative direction 46 */ 47 protected function get_direction(string $direction): string { 48 $validdirections = [ 49 '=' => '==', 50 '==' => '==', 51 '===' => '===', 52 53 '>' => '>', 54 '=>' => '=>', 55 '<' => '<', 56 '<=' => '<=', 57 ]; 58 59 if (!array_key_exists($direction, $validdirections)) { 60 throw new InvalidArgumentException("Invalid direction specified '{$direction}'."); 61 } 62 63 return $validdirections[$direction]; 64 } 65 66 /** 67 * Add a value to the filter. 68 * 69 * @param string $value A json-encoded array containing a direction, and comparison value 70 * @return self 71 */ 72 public function add_filter_value($value): parent { 73 if (!is_string($value)) { 74 $type = gettype($value); 75 if ($type === 'object') { 76 $type = get_class($value); 77 } 78 79 throw new TypeError( 80 "The value supplied was of type '{$type}'. A string representing a json-encoded value was expected." 81 ); 82 } 83 84 $data = json_decode($value); 85 86 if ($data === null) { 87 throw new InvalidArgumentException( 88 "A json-encoded object containing both a direction, and comparison value was expected." 89 ); 90 } 91 92 if (!is_object($data)) { 93 $type = gettype($value); 94 throw new InvalidArgumentException( 95 "The value supplied was a json encoded '{$type}'. " . 96 "An object containing both a direction, and comparison value was expected." 97 ); 98 } 99 100 if (!property_exists($data, 'direction')) { 101 throw new InvalidArgumentException("A 'direction' must be provided."); 102 } 103 $direction = $this->get_direction($data->direction); 104 105 if (!property_exists($data, 'value')) { 106 throw new InvalidArgumentException("A 'value' must be provided."); 107 } 108 $value = $data->value; 109 110 if (!is_numeric($value)) { 111 $type = gettype($value); 112 if ($type === 'object') { 113 $type = get_class($value); 114 } 115 116 throw new TypeError("The value supplied was of type '{$type}'. A numeric value was expected."); 117 } 118 119 $fullvalue = (object) [ 120 'direction' => $direction, 121 'value' => $value, 122 ]; 123 124 if (array_search($fullvalue, $this->filtervalues) !== false) { 125 // Remove duplicates. 126 return $this; 127 } 128 129 $this->filtervalues[] = $fullvalue; 130 131 return $this; 132 } 133 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body