11 abstract public static function evaluate($database, $field, $criteria);
28 protected static function fieldExtract(array $database, $field): ?int
35 $fieldNames = array_map(
'strtoupper', array_shift($database));
36 if (is_numeric($field)) {
37 return ((
int) $field) - 1;
39 $key = array_search($field, array_values($fieldNames),
true);
41 return (
$key !==
false) ? (int)
$key : null;
62 protected static function filter(array $database, array $criteria): array
64 $fieldNames = array_shift($database);
65 $criteriaNames = array_shift($criteria);
68 $query = self::buildQuery($criteriaNames, $criteria);
71 return self::executeQuery($database,
$query, $criteriaNames, $fieldNames);
74 protected static function getFilteredColumn(array $database, ?
int $field, array $criteria): array
77 $database = self::filter($database, $criteria);
78 $defaultReturnColumnValue = ($field === null) ? 1 : null;
82 foreach ($database as $rowKey =>
$row) {
85 $columnKey =
$key ??
'A';
86 $columnData[$rowKey][$columnKey] =
$row[
$key] ?? $defaultReturnColumnValue;
92 private static function buildQuery(array $criteriaNames, array $criteria): string
95 foreach ($criteria as
$key => $criterion) {
96 foreach ($criterion as $field => $value) {
97 $criterionName = $criteriaNames[$field];
98 if ($value !== null && $value !==
'') {
99 $condition = self::buildCondition($value, $criterionName);
100 $baseQuery[
$key][] = $condition;
105 $rowQuery = array_map(
106 function ($rowValue) {
107 return (count($rowValue) > 1) ?
'AND(' . implode(
',', $rowValue) .
')' : $rowValue[0];
112 return (count($rowQuery) > 1) ?
'OR(' . implode(
',', $rowQuery) .
')' : $rowQuery[0];
115 private static function buildCondition($criterion,
string $criterionName): string
120 $result = preg_match(
'/(?<operator>[^"]*)(?<operand>".*[*?].*")/ui', $ifCondition, $matches);
122 return "[:{$criterionName}]{$ifCondition}";
125 $trueFalse = ($matches[
'operator'] !==
'<>');
126 $wildcard = WildcardMatch::wildcard($matches[
'operand']);
127 $condition =
"WILDCARDMATCH([:{$criterionName}],{$wildcard})";
128 if ($trueFalse ===
false) {
129 $condition =
"NOT({$condition})";
135 private static function executeQuery(array $database,
string $query, array $criteria, array $fields): array
137 foreach ($database as $dataRow => $dataValues) {
140 foreach ($criteria as $criterion) {
141 $conditions = self::processCondition($criterion, $fields, $dataValues, $conditions);
149 unset($database[$dataRow]);
156 private static function processCondition(
string $criterion, array $fields, array $dataValues,
string $conditions)
158 $key = array_search($criterion, $fields,
true);
161 if (is_bool($dataValues[
$key])) {
162 $dataValue = ($dataValues[
$key]) ?
'TRUE' :
'FALSE';
163 } elseif ($dataValues[$key] !== null) {
164 $dataValue = $dataValues[
$key];
166 if (is_string($dataValue) && strpos($dataValue,
'"') !==
false) {
167 $dataValue = str_replace(
'"',
'""', $dataValue);
172 return str_replace(
'[:' . $criterion .
']', $dataValue, $conditions);
static filter(array $database, array $criteria)
filter.
static wrapResult($value)
Wrap string values in quotes.
static ifCondition($condition)
static buildCondition($criterion, string $criterionName)
static processCondition(string $criterion, array $fields, array $dataValues, string $conditions)
static getInstance(?Spreadsheet $spreadsheet=null)
Get an instance of this class.
static evaluate($database, $field, $criteria)
static executeQuery(array $database, string $query, array $criteria, array $fields)
static getFilteredColumn(array $database, ?int $field, array $criteria)
static fieldExtract(array $database, $field)
fieldExtract.
static flattenSingleValue($value='')
Convert an array to a single scalar value by extracting the first element.
static buildQuery(array $criteriaNames, array $criteria)