MDL-67913 core_table: Add filter classes
This commit is contained in:
committed by
Simey Lameze
parent
1142e1bc83
commit
44effcb419
@@ -0,0 +1,254 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Table filterset.
|
||||
*
|
||||
* @package core
|
||||
* @category table
|
||||
* @copyright 2020 Andrew Nicols <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace core_table\local\filter;
|
||||
|
||||
use Countable;
|
||||
use InvalidArgumentException;
|
||||
use Iterator;
|
||||
|
||||
/**
|
||||
* Class representing a generic filter of any type.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2020 Andrew Nicols <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class filter implements Countable, Iterator {
|
||||
|
||||
/** @var in The default filter type (ANY) */
|
||||
const JOINTYPE_DEFAULT = 1;
|
||||
|
||||
/** @var int None of the following match */
|
||||
const JOINTYPE_NONE = 0;
|
||||
|
||||
/** @var int Any of the following match */
|
||||
const JOINTYPE_ANY = 1;
|
||||
|
||||
/** @var int All of the following match */
|
||||
const JOINTYPE_ALL = 2;
|
||||
|
||||
/** @var string The name of this filter */
|
||||
protected $name = null;
|
||||
|
||||
/** @var int The join type currently in use */
|
||||
protected $jointype = self::JOINTYPE_DEFAULT;
|
||||
|
||||
/** @var array The list of active filter values */
|
||||
protected $filtervalues = [];
|
||||
|
||||
/** @var int[] valid join types */
|
||||
protected $jointypes = [
|
||||
self::JOINTYPE_NONE,
|
||||
self::JOINTYPE_ANY,
|
||||
self::JOINTYPE_ALL,
|
||||
];
|
||||
|
||||
/** @var int The current iterator position */
|
||||
protected $iteratorposition = null;
|
||||
|
||||
/**
|
||||
* Constructor for the generic filter class.
|
||||
*
|
||||
* @param string $name The name of the current filter.
|
||||
* @param int $jointype The join to use when combining the filters.
|
||||
* See the JOINTYPE_ constants for further information on the field.
|
||||
* @param mixed[] $values An array of filter objects to be applied.
|
||||
*/
|
||||
public function __construct(string $name, ?int $jointype = null, ?array $values = null) {
|
||||
$this->name = $name;
|
||||
|
||||
if ($jointype !== null) {
|
||||
$this->set_join_type($jointype);
|
||||
}
|
||||
|
||||
if (!empty($values)) {
|
||||
foreach ($values as $value) {
|
||||
$this->add_filter_value($value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the iterator position.
|
||||
*/
|
||||
public function reset_iterator(): void {
|
||||
$this->iteratorposition = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current filter value.
|
||||
*/
|
||||
public function current() {
|
||||
if ($this->iteratorposition === null) {
|
||||
$this->rewind();
|
||||
}
|
||||
|
||||
if ($this->iteratorposition === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->filtervalues[$this->iteratorposition];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current position of the iterator.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function key() {
|
||||
if ($this->iteratorposition === null) {
|
||||
$this->rewind();
|
||||
}
|
||||
|
||||
return $this->iteratorposition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind the Iterator position to the start.
|
||||
*/
|
||||
public function rewind(): void {
|
||||
if ($this->iteratorposition === null) {
|
||||
$this->sort_filter_values();
|
||||
}
|
||||
|
||||
if (count($this->filtervalues)) {
|
||||
$this->iteratorposition = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move to the next value in the list.
|
||||
*/
|
||||
public function next(): void {
|
||||
++$this->iteratorposition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current position is valid.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function valid(): bool {
|
||||
return isset($this->filtervalues[$this->iteratorposition]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the number of contexts.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function count(): int {
|
||||
return count($this->filtervalues);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the name of the filter.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the type of join to employ for the filter.
|
||||
*
|
||||
* @param int $jointype The join type to use using one of the supplied constants
|
||||
* @return self
|
||||
*/
|
||||
public function set_join_type(int $jointype): self {
|
||||
if (array_search($jointype, $this->jointypes) === false) {
|
||||
throw new InvalidArgumentException('Invalid join type specified');
|
||||
}
|
||||
|
||||
$this->jointype = $jointype;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the currently specified join type.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_join_type(): int {
|
||||
return $this->jointype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a value to the filter.
|
||||
*
|
||||
* @param mixed $value
|
||||
* @return self
|
||||
*/
|
||||
public function add_filter_value($value): self {
|
||||
if ($value === null) {
|
||||
// Null values are usually invalid.
|
||||
return $this;
|
||||
}
|
||||
|
||||
if ($value === '') {
|
||||
// Empty strings are invalid.
|
||||
return $this;
|
||||
}
|
||||
|
||||
if (array_search($value, $this->filtervalues) !== false) {
|
||||
// Remove duplicates.
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->filtervalues[] = $value;
|
||||
|
||||
// Reset the iterator position.
|
||||
$this->reset_iterator();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort the filter values to ensure reliable, and consistent output.
|
||||
*/
|
||||
protected function sort_filter_values(): void {
|
||||
// Sort the filter values to ensure consistent output.
|
||||
// Note: This is not a locale-aware sort, but we don't need this.
|
||||
// It's primarily for consistency, not for actual sorting.
|
||||
sort($this->filtervalues);
|
||||
|
||||
$this->reset_iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current filter values.
|
||||
*
|
||||
* @return mixed[]
|
||||
*/
|
||||
public function get_filter_values(): array {
|
||||
$this->sort_filter_values();
|
||||
return $this->filtervalues;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Table filterset.
|
||||
*
|
||||
* @package core
|
||||
* @category table
|
||||
* @copyright 2020 Andrew Nicols <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace core_table\local\filter;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use UnexpectedValueException;
|
||||
use moodle_exception;
|
||||
|
||||
/**
|
||||
* Class representing a set of filters.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2020 Andrew Nicols <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
abstract class filterset {
|
||||
/** @var in The default filter type (ANY) */
|
||||
const JOINTYPE_DEFAULT = 1;
|
||||
|
||||
/** @var int None of the following match */
|
||||
const JOINTYPE_NONE = 0;
|
||||
|
||||
/** @var int Any of the following match */
|
||||
const JOINTYPE_ANY = 1;
|
||||
|
||||
/** @var int All of the following match */
|
||||
const JOINTYPE_ALL = 2;
|
||||
|
||||
/** @var int The join type currently in use */
|
||||
protected $jointype = self::JOINTYPE_DEFAULT;
|
||||
|
||||
/** @var array The list of combined filter types */
|
||||
protected $filtertypes = null;
|
||||
|
||||
/** @var array The list of active filters */
|
||||
protected $filters = [];
|
||||
|
||||
/** @var int[] valid join types */
|
||||
protected $jointypes = [
|
||||
self::JOINTYPE_NONE,
|
||||
self::JOINTYPE_ANY,
|
||||
self::JOINTYPE_ALL,
|
||||
];
|
||||
|
||||
/**
|
||||
* Specify the type of join to employ for the filter.
|
||||
*
|
||||
* @param int $jointype The join type to use using one of the supplied constants
|
||||
* @return self
|
||||
*/
|
||||
public function set_join_type(int $jointype): self {
|
||||
if (array_search($jointype, $this->jointypes) === false) {
|
||||
throw new InvalidArgumentException('Invalid join type specified');
|
||||
}
|
||||
|
||||
$this->jointype = $jointype;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the currently specified join type.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_join_type(): int {
|
||||
return $this->jointype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the specified filter.
|
||||
*
|
||||
* @param filter $filter
|
||||
* @return self
|
||||
*/
|
||||
public function add_filter(filter $filter): self {
|
||||
$filtername = $filter->get_name();
|
||||
|
||||
if (array_key_exists($filtername, $this->filters)) {
|
||||
// This filter already exists.
|
||||
if ($this->filters[$filtername] === $filter) {
|
||||
// This is the same value as already added.
|
||||
// Just ignore it.
|
||||
return $this;
|
||||
}
|
||||
|
||||
// This is a different value to last time. Fail as this is not supported.
|
||||
throw new UnexpectedValueException(
|
||||
"A filter of type '{$filtername}' has already been added. Check that you have the correct filter."
|
||||
);
|
||||
}
|
||||
|
||||
// Ensure that the filter is both known, and is of the correct type.
|
||||
$validtypes = $this->get_all_filtertypes();
|
||||
|
||||
if (!array_key_exists($filtername, $validtypes)) {
|
||||
// Unknown filter.
|
||||
throw new InvalidArgumentException(
|
||||
"The filter '{$filtername}' was not recognised."
|
||||
);
|
||||
}
|
||||
|
||||
// Check that the filter is of the correct type.
|
||||
if (!is_a($filter, $validtypes[$filtername])) {
|
||||
$actualtype = get_class($filter);
|
||||
$requiredtype = $validtypes[$filtername];
|
||||
|
||||
throw new InvalidArgumentException(
|
||||
"The filter '{$filtername}' was incorrectly specified as a {$actualtype}. It must be a {$requiredtype}."
|
||||
);
|
||||
}
|
||||
|
||||
// All good. Add the filter.
|
||||
$this->filters[$filtername] = $filter;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the specified filter from the supplied params.
|
||||
*
|
||||
* @param string $filtername The name of the filter to create
|
||||
* @param mixed[] ...$args Additional arguments used to create this filter type
|
||||
* @return self
|
||||
*/
|
||||
public function add_filter_from_params(string $filtername, ...$args): self {
|
||||
// Fetch the list of valid filters by name.
|
||||
$validtypes = $this->get_all_filtertypes();
|
||||
|
||||
if (!array_key_exists($filtername, $validtypes)) {
|
||||
// Unknown filter.
|
||||
throw new InvalidArgumentException(
|
||||
"The filter '{$filtername}' was not recognised."
|
||||
);
|
||||
}
|
||||
|
||||
$filterclass = $validtypes[$filtername];
|
||||
|
||||
if (!class_exists($filterclass)) {
|
||||
// Filter class cannot be class autoloaded.
|
||||
throw new InvalidArgumentException(
|
||||
"The filter class '{$filterclass}' for filter '{$filtername}' could not be found."
|
||||
);
|
||||
}
|
||||
|
||||
// Pass all supplied arguments to the constructor when adding a new filter.
|
||||
// This allows for a wider definition of the the filter in child classes.
|
||||
$this->add_filter(new $filterclass($filtername, ...$args));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the current set of filters.
|
||||
*
|
||||
* @return filter[]
|
||||
*/
|
||||
public function get_filters(): array {
|
||||
// Sort the filters by their name to ensure consistent output.
|
||||
// Note: This is not a locale-aware sort, but we don't need this.
|
||||
// It's primarily for consistency, not for actual sorting.
|
||||
asort($this->filters);
|
||||
|
||||
return $this->filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether the filter has been added or not.
|
||||
*
|
||||
* @param string $filtername
|
||||
* @return bool
|
||||
*/
|
||||
public function has_filter(string $filtername): bool {
|
||||
// We do not check if the filtername is valid, only that it exists.
|
||||
// This is an existence check and there is no benefit to doing any more.
|
||||
return array_key_exists($filtername, $this->filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the named filter.
|
||||
*
|
||||
* @param string $filtername
|
||||
* @return filter
|
||||
*/
|
||||
public function get_filter(string $filtername): filter {
|
||||
if (!array_key_exists($filtername, $this->get_all_filtertypes())) {
|
||||
throw new UnexpectedValueException("The filter specified ({$filtername}) is invalid.");
|
||||
}
|
||||
|
||||
if (!array_key_exists($filtername, $this->filters)) {
|
||||
throw new UnexpectedValueException("The filter specified ({$filtername}) has not been created.");
|
||||
}
|
||||
|
||||
return $this->filters[$filtername];
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirm whether the filter has been correctly specified.
|
||||
*
|
||||
* @throws moodle_exception
|
||||
*/
|
||||
public function check_validity(): void {
|
||||
// Ensure that all required filters are present.
|
||||
$missing = [];
|
||||
foreach (array_keys($this->get_required_filters()) as $filtername) {
|
||||
if (!array_key_exists($filtername, $this->filters)) {
|
||||
$missing[] = $filtername;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($missing)) {
|
||||
throw new moodle_exception(
|
||||
'missingrequiredfields',
|
||||
'core_table',
|
||||
'',
|
||||
implode(get_string('listsep', 'langconfig') . ' ', $missing)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of required filters in an array of filtername => filter class type.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_required_filters(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of optional filters in an array of filtername => filter class type.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_optional_filters(): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all filter valid types in an array of filtername => filter class type.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_all_filtertypes(): array {
|
||||
if ($this->filtertypes === null) {
|
||||
$required = $this->get_required_filters();
|
||||
$optional = $this->get_optional_filters();
|
||||
|
||||
$conflicts = array_keys(array_intersect_key($required, $optional));
|
||||
|
||||
if (!empty($conflicts)) {
|
||||
throw new InvalidArgumentException(
|
||||
"Some filter types are both required, and optional: " . implode(', ', $conflicts)
|
||||
);
|
||||
}
|
||||
|
||||
$this->filtertypes = array_merge($required, $optional);
|
||||
asort($this->filtertypes);
|
||||
}
|
||||
|
||||
return $this->filtertypes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Integer filter.
|
||||
*
|
||||
* @package core
|
||||
* @category table
|
||||
* @copyright 2020 Andrew Nicols <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace core_table\local\filter;
|
||||
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Class representing an integer filter.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2020 Andrew Nicols <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class integer_filter extends filter {
|
||||
/**
|
||||
* Add a value to the filter.
|
||||
*
|
||||
* @param int $values
|
||||
* @return self
|
||||
*/
|
||||
public function add_filter_value($value): parent {
|
||||
if (!is_int($value)) {
|
||||
$type = gettype($value);
|
||||
if ($type === 'object') {
|
||||
$type = get_class($value);
|
||||
}
|
||||
|
||||
throw new TypeError("The value supplied was of type '{$type}'. An integer was expected.");
|
||||
}
|
||||
|
||||
if (array_search($value, $this->filtervalues) !== false) {
|
||||
// Remove duplicates.
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->filtervalues[] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Integer comparison filter to allow a comparison such as "> 42".
|
||||
*
|
||||
* @package core
|
||||
* @category table
|
||||
* @copyright 2020 Andrew Nicols <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace core_table\local\filter;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Class representing an integer filter.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2020 Andrew Nicols <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class numeric_comparison_filter extends filter {
|
||||
/**
|
||||
* Get the authoritative direction.
|
||||
*
|
||||
* @param string $direction The supplied direction
|
||||
* @return string The authoritative direction
|
||||
*/
|
||||
protected function get_direction(string $direction): string {
|
||||
$validdirections = [
|
||||
'=' => '==',
|
||||
'==' => '==',
|
||||
'===' => '===',
|
||||
|
||||
'>' => '>',
|
||||
'=>' => '=>',
|
||||
'<' => '<',
|
||||
'<=' => '<=',
|
||||
];
|
||||
|
||||
if (!array_key_exists($direction, $validdirections)) {
|
||||
throw new InvalidArgumentException("Invalid direction specified '{$direction}'.");
|
||||
}
|
||||
|
||||
return $validdirections[$direction];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a value to the filter.
|
||||
*
|
||||
* @param string $value A json-encoded array containing a direction, and comparison value
|
||||
* @return self
|
||||
*/
|
||||
public function add_filter_value($value): parent {
|
||||
if (!is_string($value)) {
|
||||
$type = gettype($value);
|
||||
if ($type === 'object') {
|
||||
$type = get_class($value);
|
||||
}
|
||||
|
||||
throw new TypeError(
|
||||
"The value supplied was of type '{$type}'. A string representing a json-encoded value was expected."
|
||||
);
|
||||
}
|
||||
|
||||
$data = json_decode($value);
|
||||
|
||||
if ($data === null) {
|
||||
throw new InvalidArgumentException(
|
||||
"A json-encoded object containing both a direction, and comparison value was expected."
|
||||
);
|
||||
}
|
||||
|
||||
if (!is_object($data)) {
|
||||
$type = gettype($value);
|
||||
throw new InvalidArgumentException(
|
||||
"The value supplied was a json encoded '{$type}'. " .
|
||||
"An object containing both a direction, and comparison value was expected."
|
||||
);
|
||||
}
|
||||
|
||||
if (!property_exists($data, 'direction')) {
|
||||
throw new InvalidArgumentException("A 'direction' must be provided.");
|
||||
}
|
||||
$direction = $this->get_direction($data->direction);
|
||||
|
||||
if (!property_exists($data, 'value')) {
|
||||
throw new InvalidArgumentException("A 'value' must be provided.");
|
||||
}
|
||||
$value = $data->value;
|
||||
|
||||
if (!is_numeric($value)) {
|
||||
$type = gettype($value);
|
||||
if ($type === 'object') {
|
||||
$type = get_class($value);
|
||||
}
|
||||
|
||||
throw new TypeError("The value supplied was of type '{$type}'. A numeric value was expected.");
|
||||
}
|
||||
|
||||
$fullvalue = (object) [
|
||||
'direction' => $direction,
|
||||
'value' => $value,
|
||||
];
|
||||
|
||||
if (array_search($fullvalue, $this->filtervalues) !== false) {
|
||||
// Remove duplicates.
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->filtervalues[] = $fullvalue;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* String filter.
|
||||
*
|
||||
* @package core
|
||||
* @category table
|
||||
* @copyright 2020 Simey Lameze <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace core_table\local\filter;
|
||||
|
||||
use TypeError;
|
||||
|
||||
/**
|
||||
* Class representing a string filter.
|
||||
*
|
||||
* @package core
|
||||
* @copyright 2020 Simey Lameze <[email protected]>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class string_filter extends filter {
|
||||
/**
|
||||
* Add a value to the filter.
|
||||
*
|
||||
* @param string $values
|
||||
* @return self
|
||||
*/
|
||||
public function add_filter_value($value): parent {
|
||||
if (!is_string($value)) {
|
||||
$type = gettype($value);
|
||||
if ($type === 'object') {
|
||||
$type = get_class($value);
|
||||
}
|
||||
|
||||
throw new TypeError("The value supplied was of type '{$type}'. A string was expected.");
|
||||
}
|
||||
|
||||
if (array_search($value, $this->filtervalues) !== false) {
|
||||
// Remove duplicates.
|
||||
return $this;
|
||||
}
|
||||
|
||||
$this->filtervalues[] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user