Maslosoft Mangan API
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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/mangan
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @copyright Copyright (c) Others as mentioned in code
* @link https://maslosoft.com/mangan/
*/
namespace Maslosoft\Mangan;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Criteria\ConditionDecorator;
use Maslosoft\Mangan\Criteria\Conditions;
use Maslosoft\Mangan\Interfaces\Criteria\LimitableInterface;
use Maslosoft\Mangan\Interfaces\Criteria\MergeableInterface;
use Maslosoft\Mangan\Interfaces\Criteria\SelectableInterface;
use Maslosoft\Mangan\Interfaces\Criteria\SortableInterface;
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
use Maslosoft\Mangan\Interfaces\ModelAwareInterface;
use Maslosoft\Mangan\Interfaces\SortInterface;
use Maslosoft\Mangan\Traits\Criteria\CursorAwareTrait;
use Maslosoft\Mangan\Traits\Criteria\DecoratableTrait;
use Maslosoft\Mangan\Traits\Criteria\LimitableTrait;
use Maslosoft\Mangan\Traits\Criteria\SelectableTrait;
use Maslosoft\Mangan\Traits\Criteria\SortableTrait;
use Maslosoft\Mangan\Traits\ModelAwareTrait;
use UnexpectedValueException;
/**
* Criteria
*
* This class is a helper for building MongoDB query arrays, it support three syntaxes for adding conditions:
*
* 1. 'equals' syntax:
* $criteriaObject->fieldName = $value; // this will produce fieldName == value query
* 2. fieldName call syntax
* $criteriaObject->fieldName($operator, $value); // this will produce fieldName <operator> value
* 3. addCond method
* $criteriaObject->addCond($fieldName, $operator, $vale); // this will produce fieldName <operator> value
*
* For operators list {@see Criteria::$operators}
*
* @author Ianaré Sévi
* @author Dariusz Górecki <darek.krk@gmail.com>
* @author Invenzzia Group, open-source division of CleverIT company http://www.invenzzia.org
* @copyright 2011 CleverIT http://www.cleverit.com.pl
* @license New BSD license
*/
class Criteria implements CriteriaInterface,
ModelAwareInterface
{
use CursorAwareTrait,
DecoratableTrait,
LimitableTrait,
ModelAwareTrait,
SelectableTrait,
SortableTrait;
/**
* @since v1.0
* @var array $operators supported operators lists
*/
public static $operators = [
// Comparison
// Matches values that are equal to a specified value.
'eq' => '$eq',
'equals' => '$eq',
'==' => '$eq',
// Matches values that are greater than a specified value.
'gt' => '$gt',
'greater' => '$gt',
'>' => '$gt',
// Matches values that are greater than or equal to a specified value.
'gte' => '$gte',
'greatereq' => '$gte',
'>=' => '$gte',
// Matches values that are less than a specified value.
'lt' => '$lt',
'less' => '$lt',
'<' => '$lt',
// Matches values that are less than or equal to a specified value.
'lte' => '$lte',
'lesseq' => '$lte',
'<=' => '$lte',
// Matches all values that are not equal to a specified value.
'ne' => '$ne',
'noteq' => '$ne',
'!=' => '$ne',
'<>' => '$ne',
// Matches any of the values specified in an array.
'in' => '$in',
// Matches none of the values specified in an array.
'notin' => '$nin',
// Logical
// Joins query clauses with a logical OR returns all documents that match the conditions of either clause.
'or' => '$or',
// Joins query clauses with a logical AND returns all documents that match the conditions of both clauses.
'and' => '$and',
// Inverts the effect of a query expression and returns documents that do not match the query expression.
'not' => '$not',
// Joins query clauses with a logical NOR returns all documents that fail to match both clauses.
'nor' => '$nor',
// Element
// Matches documents that have the specified field.
'exists' => '$exists',
'notexists' => '$exists',
// Selects documents if a field is of the specified type.
'type' => '$type',
// Evaluation
// Performs a modulo operation on the value of a field and selects documents with a specified result.
'mod' => '$mod',
'%' => '$mod',
// Selects documents where values match a specified regular expression.
'regex' => '$regex',
// Performs text search.
'text' => '$text',
// Matches documents that satisfy a JavaScript expression.
'where' => '$where',
// Geospatial
// Selects geometries within a bounding GeoJSON geometry. The `2dsphere` and `2d` indexes support $geoWithin.
'geoWithin' => '$geoWithin',
// Selects geometries that intersect with a GeoJSON geometry. The `2dsphere` index supports $geoIntersects.
'geoIntersects' => '$geoIntersects',
// Returns geospatial objects in proximity to a point. Requires a geospatial index. The `2dsphere` and `2d` indexes support $near.
'near' => '$near',
// Returns geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The `2dsphere` and `2d` indexes support $nearSphere.
'nearSphere' => '$nearSphere',
// Array
// Matches arrays that contain all elements specified in the query.
'all' => '$all',
// Selects documents if element in the array field matches all the specified $elemMatch conditions.
'elemmatch' => '$elemMatch',
// Selects documents if the array field is a specified size.
'size' => '$size',
// Comments
'comment' => '$comment'
];
/**
* Sort Ascending
*/
const SortAsc = SortInterface::SortAsc;
/**
* Sort Descending
*/
const SortDesc = SortInterface::SortDesc;
/**
* Sort Ascending
* @deprecated since version 4.0.7
*/
const SORT_ASC = SortInterface::SortAsc;
/**
* Sort Descending
* @deprecated since version 4.0.7
*/
const SORT_DESC = SortInterface::SortDesc;
private $_conditions = [];
/**
* Raw conditions array
* @var mixed[]
*/
private $_rawConds = [];
private $_workingFields = [];
/**
* Constructor
* Example criteria:
*
* <PRE>
* 'criteria' = array(
* 'conditions'=>array(
* 'fieldName1'=>array('greater' => 0),
* 'fieldName2'=>array('>=' => 10),
* 'fieldName3'=>array('<' => 10),
* 'fieldName4'=>array('lessEq' => 10),
* 'fieldName5'=>array('notEq' => 10),
* 'fieldName6'=>array('in' => array(10, 9)),
* 'fieldName7'=>array('notIn' => array(10, 9)),
* 'fieldName8'=>array('all' => array(10, 9)),
* 'fieldName9'=>array('size' => 10),
* 'fieldName10'=>array('exists'),
* 'fieldName11'=>array('notExists'),
* 'fieldName12'=>array('mod' => array(10, 9)),
* 'fieldName13'=>array('==' => 1)
* ),
* 'select'=>array('fieldName', 'fieldName2'),
* 'limit'=>10,
* 'offset'=>20,
* 'sort'=>array('fieldName1'=>Criteria::SortAsc, 'fieldName2'=>Criteria::SortDesc),
* );
* </PRE>
* @param mixed|CriteriaInterface|Conditions $criteria
* @param AnnotatedInterface|null Model to use for criteria decoration
* @since v1.0
*/
public function __construct($criteria = null, AnnotatedInterface $model = null)
{
if (!empty($model))
{
$this->setModel($model);
}
$this->setCd(new ConditionDecorator($model));
if (is_array($criteria))
{
$available = ['conditions', 'select', 'limit', 'offset', 'sort', 'useCursor'];
$diff = array_diff_key($criteria, array_flip($available));
if (!empty($diff))
{
$params = [
'[' . implode(', ', $available) . ']',
'[' . implode(', ', array_keys($criteria)) . ']'
];
$msg = vsprintf('Allowed criteria keys are: %s, however was provided: %s', $params);
throw new UnexpectedValueException($msg);
}
if (isset($criteria['conditions']))
foreach ($criteria['conditions'] as $fieldName => $conditions)
{
$fieldNameArray = explode('.', $fieldName);
if (count($fieldNameArray) === 1)
{
$fieldName = array_shift($fieldNameArray);
}
else
{
$fieldName = array_pop($fieldNameArray);
}
$this->_workingFields = $fieldNameArray;
assert(is_array($conditions), 'Each condition must be array with operator as key and value, ie: ["_id" => ["==" => "123"]]');
foreach ($conditions as $operator => $value)
{
$operator = strtolower($operator);
if(!isset(self::$operators[$operator]))
{
$params = [
$operator,
$fieldName
];
$msg = vsprintf('Unknown Criteria operator `%s` for `%s`', $params);
throw new UnexpectedValueException($msg);
}
$this->addCond($fieldName, $operator, $value);
}
}
if (isset($criteria['select']))
{
$this->select($criteria['select']);
}
if (isset($criteria['limit']))
{
$this->limit($criteria['limit']);
}
if (isset($criteria['offset']))
{
$this->offset($criteria['offset']);
}
if (isset($criteria['sort']))
{
$this->setSort($criteria['sort']);
}
if (isset($criteria['useCursor']))
{
$this->setUseCursor($criteria['useCursor']);
}
}
// NOTE:
//Scrunitizer: $criteria is of type object<Maslosoft\Mangan\...ria\MergeableInterface>, but the function expects a array|object<Maslosoft\M...aces\CriteriaInterface>.
// But for now it should be this way to easyli distinguish from Conditions.
// Future plan: Use CriteriaInterface here, and drop `$criteria instanceof Conditions` if clause. Conditions should implement CriteriaInterface too.
elseif ($criteria instanceof MergeableInterface)
{
assert($criteria instanceof CriteriaInterface);
$this->mergeWith($criteria);
}
elseif ($criteria instanceof Conditions)
{
$this->setConditions($criteria);
}
}
/**
* Merge with other criteria
* - Field list operators will be merged
* - Limit and offet will be overriden
* - Select fields list will be merged
* - Sort fields list will be merged
* @param null|array|CriteriaInterface $criteria
* @return CriteriaInterface
* @since v1.0
*/
public function mergeWith($criteria)
{
if (is_array($criteria))
{
$criteria = new static($criteria, $this->getModel());
}
elseif (empty($criteria))
{
return $this;
}
if ($this instanceof LimitableInterface && $criteria instanceof LimitableInterface && !empty($criteria->getLimit()))
{
$this->setLimit($criteria->getLimit());
}
if ($this instanceof LimitableInterface && $criteria instanceof LimitableInterface && !empty($criteria->getOffset()))
{
$this->setOffset($criteria->getOffset());
}
if ($this instanceof SortableInterface && $criteria instanceof SortableInterface && !empty($criteria->getSort()))
{
$this->setSort($criteria->getSort());
}
if ($this instanceof SelectableInterface && $criteria instanceof SelectableInterface && !empty($criteria->getSelect()))
{
$this->select($criteria->getSelect());
}
$this->_conditions = $this->_mergeConditions($this->_conditions, $criteria->getConditions());
return $this;
}
private function _mergeConditions($source, $conditions)
{
$opTable = array_values(self::$operators);
foreach ($conditions as $fieldName => $conds)
{
if (
is_array($conds) &&
count(array_diff(array_keys($conds), $opTable)) == 0
)
{
if (isset($source[$fieldName]) && is_array($source[$fieldName]))
{
foreach ($source[$fieldName] as $operator => $value)
{
if (!in_array($operator, $opTable))
{
unset($source[$fieldName][$operator]);
}
}
}
else
{
$source[$fieldName] = [];
}
foreach ($conds as $operator => $value)
{
$source[$fieldName][$operator] = $value;
}
}
else
{
$source[$fieldName] = $conds;
}
}
return $source;
}
/**
* If we have operator add it otherwise call parent implementation
* @since v1.0
*/
public function __call($fieldName, $parameters)
{
if (isset($parameters[0]))
{
$operatorName = strtolower($parameters[0]);
}
if (array_key_exists(1, $parameters))
{
$value = $parameters[1];
}
if (is_numeric($operatorName))
{
$operatorName = strtolower(trim($value));
$value = (strtolower(trim($value)) === 'exists') ? true : false;
}
if (in_array($operatorName, array_keys(self::$operators)))
{
array_push($this->_workingFields, $fieldName);
$fieldName = implode('.', $this->_workingFields);
$this->_workingFields = [];
switch ($operatorName)
{
case 'exists':
$this->addCond($fieldName, $operatorName, true);
break;
case 'notexists':
$this->addCond($fieldName, $operatorName, false);
break;
default:
$this->addCond($fieldName, $operatorName, $value);
}
return $this;
}
}
/**
* @since v1.0.2
*/
public function __get($name)
{
array_push($this->_workingFields, $name);
return $this;
}
/**
* @since v1.0.2
*/
public function __set($name, $value)
{
array_push($this->_workingFields, $name);
$fieldList = implode('.', $this->_workingFields);
$this->_workingFields = [];
$this->addCond($fieldList, '==', $value);
}
/**
* Return query array
* @return array query array
* @since v1.0
*/
public function getConditions()
{
$conditions = [];
foreach ($this->_rawConds as $c)
{
$conditions = $this->_makeCond($c[Conditions::FieldName], $c[Conditions::Operator], $c[Conditions::Value], $conditions);
}
return $this->_mergeConditions($this->_conditions, $conditions);
}
/**
* Set conditions
* @param array|Conditions $conditions
* @return Criteria
*/
public function setConditions($conditions)
{
if ($conditions instanceof Conditions)
{
$this->_conditions = $conditions->get();
return $this;
}
$this->_conditions = $conditions;
return $this;
}
/**
* Add condition
* If specified field already has a condition, values will be merged
* duplicates will be overriden by new values!
*
* NOTE: Should NOT be part of interface
*
* @param string $fieldName
* @param string $op operator
* @param mixed $value
* @since v1.0
*/
public function addCond($fieldName, $op, $value)
{
$this->_rawConds[] = [
Conditions::FieldName => $fieldName,
Conditions::Operator => $op,
Conditions::Value => $value
];
return $this;
}
/**
* Get condition
* If specified field already has a condition, values will be merged
* duplicates will be overridden by new values!
* @see getConditions
* @param string $fieldName
* @param string $op operator
* @param mixed $value
* @since v1.0
*/
private function _makeCond($fieldName, $op, $value, $conditions = [])
{
// For array values
$arrayOperators = [
'or',
'in',
'notin'
];
if (in_array($op, $arrayOperators))
{
// Ensure array
if (!is_array($value))
{
$value = [$value];
}
// Decorate each value
$values = [];
foreach ($value as $val)
{
$decorated = $this->getCd()->decorate($fieldName, $val);
$fieldName = key($decorated);
$values[] = current($decorated);
}
$value = $values;
}
else
{
$decorated = $this->getCd()->decorate($fieldName, $value);
$fieldName = key($decorated);
$value = current($decorated);
}
// Apply operators
$op = self::$operators[$op];
if ($op == self::$operators['or'])
{
if (!isset($conditions[$op]))
{
$conditions[$op] = [];
}
$conditions[$op][] = [$fieldName => $value];
}
else
{
if (!isset($conditions[$fieldName]) && $op != self::$operators['equals'])
{
$conditions[$fieldName] = [];
}
if ($op != self::$operators['equals'])
{
if (
!is_array($conditions[$fieldName]) ||
count(array_diff(array_keys($conditions[$fieldName]), array_values(self::$operators))) > 0
)
{
$conditions[$fieldName] = [];
}
$conditions[$fieldName][$op] = $value;
}
else
{
$conditions[$fieldName] = $value;
}
}
return $conditions;
}
}
API documentation generated by ApiGen