1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
<?php
/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ICanBoogie\Facets;
use ICanBoogie\ActiveRecord\Query;
/**
* Trait for a generic criterion.
*/
trait CriterionTrait
{
/**
* The identifier of the criterion.
*
* @var string
*/
protected $id;
/**
* Returns the criterion's identifier.
*/
protected function get_id()
{
return $this->id;
}
/**
* The column name of the criterion, as in a SQL table.
*
* @var string
*/
protected $column_name;
/**
* Returns the criterion's column name, or if it is empty the criterion's identifier.
*/
protected function get_column_name()
{
return $this->column_name ?: $this->id;
}
/**
* Parses the query string and marks words matched by the criterion.
*
* @param QueryString $q
*
* @return QueryString
*/
public function parse_query_string(QueryString $q)
{
return $q;
}
/**
* Parses the criterion value using {@link CriterionValue::from()}.
*
* @param mixed $value
*
* @return mixed
*/
public function parse_value($value)
{
return CriterionValue::from($value);
}
/**
* Unset the condition if the modifier is `null` or an empty string.
*
* @param array $conditions
* @param array $modifiers
*/
public function alter_conditions(array &$conditions, array $modifiers)
{
if (!isset($modifiers[$this->id]) || $modifiers[$this->id] === '')
{
unset($conditions[$this->id]);
return;
}
$conditions[$this->id] = $modifiers[$this->id];
}
public function alter_query(Query $query)
{
return $query;
}
/**
* Alters the query according to the specified value.
*
* The method handles {@link IntervalCriterionValue} and {@link SetCriterionValue} instances as well
* as plain values, for which a simple `{$this->id} = {$value}` is done.
*
* Subclasses might want to override the method according to the kind of value they provide.
*
* @param Query $query
* @param mixed $value The criterion value. Special care is taken if the param is an
* instance of {@link IntervalCriterionValue} or {@link SetCriterionValue}.
*
* @return Query
*/
public function alter_query_with_value(Query $query, $value)
{
if ($value instanceof IntervalCriterionValue)
{
if ($value->min === null)
{
return $query->and("`$this->column_name` <= ?", $value->max);
}
if ($value->max === null)
{
return $query->and("`$this->column_name` >= ?", $value->min);
}
return $query->and("`$this->column_name` BETWEEN ? AND ?", $value->min, $value->max);
}
if ($value instanceof SetCriterionValue)
{
$value = $value->to_array();
}
return $query->and([ $this->column_name => $value ]);
}
/**
* Alters the query with an order.
*
* The {@link $column_name} property is used.
*
* @param Query $query
* @param int $order_direction "DESC" if inferior to 0, "ASC" otherwise.
*
* @return Query
*/
public function alter_query_with_order(Query $query, $order_direction)
{
return $query->order("`$this->column_name` " . ($order_direction < 0 ? 'DESC' : 'ASC'));
}
public function alter_records(array &$records)
{
}
/**
* Returns a human readable value.
*
* @param mixed $value
*
* @return string|IntervalCriterionValue
*/
public function humanize($value)
{
if ($value instanceof IntervalCriterionValue)
{
return "$value->min – $value->max";
}
return $value;
}
/**
* Formats a humanized value, or array of values, into a string.
*
* @param mixed $humanized_value
*
* @return string
*/
public function format_humanized_value($humanized_value)
{
if (is_array($humanized_value))
{
return implode(', ', $humanized_value);
}
return $humanized_value;
}
}