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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
<?php
/**
* TfishCriteria class file.
*
* @copyright Simon Wilkinson 2013-2017 (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.0
* @package database
*/
// Enable strict type declaration.
declare(strict_types=1);
if (!defined("TFISH_ROOT_PATH")) die("TFISH_ERROR_ROOT_PATH_NOT_DEFINED");
/**
* Query composer class for SQLite database.
*
* Use this class to set parameters on database-related actions. Individual conditions are held
* within the item property, as TfishCriteriaItem objects. Criteria holds the basic query parameters
* and controls how TfishCriteriaItem are chained together (eg. with "AND", "OR").
*
* See the Tuskfish Developer Guide for a full explanation and examples.
*
* @copyright Simon Wilkinson 2013-2017 (https://tuskfish.biz)
* @license https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html GNU General Public License (GPL) V2
* @author Simon Wilkinson <[email protected]>
* @version Release: 1.0
* @since 1.0
* @package database
* @property array $item Array of TfishCriteriaItem
* @property array $condition Array of conditions used to join TfishCriteriaItem (AND, OR)
* @property string $groupby Column to group results by
* @property int $limit Number of records to retrieve
* @property int $offset Starting point for retrieving records
* @property string $order Sort order
* @property string $ordertype Sort ascending (ASC) or descending(DESC)
* @property array $tag Array of tag IDs
*/
class TfishCriteria
{
/** @var array $__data Holds the values of the object's properties for access by magic methods. **/
protected $__data = array(
'item' => array(),
'condition' => array(),
'groupby' => false,
'limit' => 0,
'offset' => 0,
'order' => false,
'ordertype' => "DESC",
'tag' => array()
);
/**
* Add conditions (TfishCriteriaItem) to a query.
*
* @param object $criteria_item TfishCriteriaItem object.
* @param string $condition Condition used to chain TfishCriteriaItems, "AND" or "OR" only.
*/
public function add(TfishCriteriaItem $criteria_item, string $condition = "AND")
{
self::__set('item', $criteria_item);
self::__set('condition', $condition);
}
/**
* Get the value of a property.
*
* Intercepts direct calls to access an object property. This method can be modified to impose
* processing logic to the value before returning it.
*
* @param string $property Name of property.
* @return mixed|null $property Value of property if it is set; otherwise null.
*/
public function __get(string $property)
{
$clean_property = TfishFilter::trimString($property);
if (isset($this->__data[$clean_property])) {
return $this->__data[$clean_property];
} else {
return null;
}
}
/**
* Check if a property is set.
*
* Intercepts isset() calls to correctly read object properties. Can be modified to add
* processing logic for specific properties.
*
* @param string $property Name of property.
* @return bool True if set, otherwise false.
*/
public function __isset(string $property)
{
$clean_property = TfishFilter::trimString($property);
if (isset($this->__data[$clean_property])) {
return true;
} else {
return false;
}
}
/**
* Unset existing type criteria.
*
* Used by content object handler subclasses to remove any existing type filter when they may
* need to set or reset it to a specific type.
*
* @param int $key Key of the item array containing the type filter.
*/
public function killType(int $key)
{
if (isset($this->__data['item'][$key])) {
unset($this->__data['item'][$key]);
unset($this->__data['condition'][$key]);
}
// Reindex the arrays.
$this->__data['item'] = array_values($this->__data['item']);
$this->__data['condition'] = array_values($this->__data['condition']);
}
/**
* Set the value of a property and will not allow non-whitelisted properties to be set.
*
* Intercepts direct calls to set the value of an object property. This method can be modified
* to impose data type restrictions and range checks before allowing the property to be set.
*
* @param string $property Name of property.
* @param mixed $value Value of property.
*/
public function __set(string $property, $value)
{
$clean_property = TfishFilter::trimString($property);
// Check that property is whitelisted.
if (!isset($this->__data[$clean_property])) {
trigger_error(TFISH_ERROR_NO_SUCH_PROPERTY, E_USER_ERROR);
}
switch ($clean_property) {
case "item": // Array of TfishCriteriaItem objects
if (is_a($value, 'TfishCriteriaItem')) {
$this->__data['item'][] = $value;
} else {
trigger_error(TFISH_ERROR_NOT_CRITERIA_ITEM_OBJECT, E_USER_ERROR);
}
break;
case "limit": // int
case "offset": // int
if (TfishFilter::isInt($value, 0)) {
$this->__data[$clean_property] = (int) $value;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
break;
case "condition":
if ($value === "AND" || $value === "OR") {
$this->__data['condition'][] = TfishFilter::trimString($value);
} else {
trigger_error(TFISH_ERROR_ILLEGAL_VALUE, E_USER_ERROR);
}
break;
case "order": // string; any property of target object
case "groupby": // string; any property of target object
$value = TfishFilter::trimString($value);
if (TfishFilter::isAlnumUnderscore($value)) {
$this->__data[$clean_property] = $value;
} else {
trigger_error(TFISH_ERROR_NOT_ALNUMUNDER, E_USER_ERROR);
}
break;
case "ordertype": // ASC or DESC
if ($value === "ASC") {
$this->__data['ordertype'] = "ASC";
} else {
$this->__data['ordertype'] = "DESC";
}
break;
case "tag":
if (TfishFilter::isArray($value)) {
$clean_tags = array();
foreach ($value as $val) {
if (TfishFilter::isInt($val, 1)) {
$clean_tags[] = (int) $val;
} else {
trigger_error(TFISH_ERROR_NOT_INT, E_USER_ERROR);
}
unset($val);
}
$this->__data['tag'] = $clean_tags;
} else {
trigger_error(TFISH_ERROR_NOT_ARRAY, E_USER_ERROR);
}
break;
}
return true;
}
/**
* Generate an SQL WHERE clause with PDO placeholders based on criteria items.
*
* Loop through the criteria items building a list of PDO placeholders together
* with the SQL. These will be used to bind the values in the statement to prevent
* SQL injection. Note that values are NOT inserted into the SQL directly.
*
* Enclose column identifiers in backticks to escape them. Link criteria items with AND/OR
* except on the last iteration ($count-1).
*
* @return string $sql SQL query fragment.
*/
public function renderSQL()
{
$sql = '';
$count = count($this->item);
if ($count) {
$sql = "(";
for ($i = 0; $i < $count; $i++) {
$sql .= "`" . TfishDatabase::escapeIdentifier($this->item[$i]->column) . "` "
. $this->item[$i]->operator . " :placeholder" . (string) $i;
if ($i < ($count - 1)) {
$sql .= " " . $this->condition[$i] . " ";
}
}
$sql .= ") ";
}
return $sql;
}
/**
* Generate an array of PDO placeholders based on criteria items.
*
* Use this function to get a list of placeholders generated by renderSQL(). The two functions
* should be used together; use renderSQL() to create a WHERE clause with named placeholders,
* and renderPDO() to get a list of the named placeholders so that you can bind values to them.
*
* @return array $pdo_placeholders Array of PDO placeholders used for building SQL query.
*/
public function renderPDO()
{
$pdo_placeholders = array();
$count = count($this->item);
for ($i = 0; $i < $count; $i++) {
$pdo_placeholders[":placeholder" . (string) $i] = $this->item[$i]->value;
}
return $pdo_placeholders;
}
/**
* Generate an SQL WHERE clause with PDO placeholders based on the tag property.
*
* Loop through the criteria->tags building a list of PDO placeholders together
* with the SQL. These will be used to bind the values in the statement to prevent
* SQL injection. Note that values are NOT inserted into the SQL directly.
*
* @return string $sql SQL query fragment.
*/
public function renderTagSQL()
{
$sql = '';
$count = count($this->tag);
if ($count === 1) {
$sql .= "`taglink`.`tag_id` = :tag0 ";
} elseif ($count > 1) {
$sql .= "`taglink`.`tag_id` IN (";
for ($i = 0; $i < count($this->tag); $i++) {
$sql .= ':tag' . (string) $i . ',';
}
$sql = rtrim($sql, ',');
$sql .= ") ";
}
return $sql;
}
/**
* Generate an array of PDO placeholders based on the tag property.
*
* Use this function to get a list of placeholders generated by renderTagSQL(). The two
* functions should be used together; use renderTagSQL() to create a WHERE clause with named
* placeholders, and renderTagPDO() to get a list of the named placeholders so that you can
* bind values to them.
*
* @return array $tag_placeholders Array of PDO placeholders used for building SQL query.
*/
public function renderTagPDO()
{
$tag_placeholders = array();
for ($i = 0; $i < count($this->tag); $i++) {
$tag_placeholders[":tag" . (string) $i] = (int) $this->tag[$i];
}
return $tag_placeholders;
}
/**
* Unsets a property.
*
* Intercepts unset() calls to correctly unset object properties. Can be modified to add
* processing logic for specific properties.
*
* @param string $property Name of property.
* @return bool True on success false on failure.
*/
public function __unset(string $property)
{
$clean_property = TfishFilter::trimString($property);
if (isset($this->__data[$clean_property])) {
unset($this->__data[$clean_property]);
return true;
} else {
return false;
}
}
}