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
<?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\Abstracts;
use Maslosoft\Mangan\Document;
use Maslosoft\Mangan\Interfaces\Criteria\DecoratableInterface;
use Maslosoft\Mangan\Interfaces\CriteriaAwareInterface;
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
use Maslosoft\Mangan\Interfaces\ModelAwareInterface;
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface;
use Maslosoft\Mangan\Interfaces\WithCriteriaInterface;
use Maslosoft\Mangan\Traits\ModelAwareTrait;
/**
* Base class for implementing scope managers
*
* @see ScopeManagerInterface
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
abstract class AbstractScopeManager implements ModelAwareInterface
{
use ModelAwareTrait;
/**
*
* @var CriteriaInterface
*/
private $criteria = null;
/**
* Returns the declaration of named scopes.
* A named scope represents a query criteria that can be chained together with
* other named scopes and applied to a query. This method should be overridden
* by child classes to declare named scopes for the particular document classes.
* For example, the following code declares two named scopes: 'recently' and
* 'published'.
* <pre>
* return array(
* 'published'=>array(
* 'conditions'=>array(
* 'status'=>array('==', 1),
* ),
* ),
* 'recently'=>array(
* 'sort'=>array('create_time'=>Criteria::SortDesc),
* 'limit'=>5,
* ),
* );
* </pre>
* If the above scopes are declared in a 'Post' model, we can perform the following
* queries:
* <pre>
* $posts=Post::model()->published()->findAll();
* $posts=Post::model()->published()->recently()->findAll();
* $posts=Post::model()->published()->published()->recently()->find();
* </pre>
*
* @return array the scope definition. The array keys are scope names; the array
* values are the corresponding scope definitions. Each scope definition is represented
* as an array whose keys must be properties of {@link Criteria}.
* @since v1.0
*/
public function scopes()
{
return [];
}
/**
* Returns the default named scope that should be implicitly applied to all queries for this model.
* Note, default scope only applies to SELECT queries. It is ignored for INSERT, UPDATE and DELETE queries.
* The default implementation simply returns an empty array. You may override this method
* if the model needs to be queried with some default criteria (e.g. only active records should be returned).
* @return array the mongo criteria. This will be used as the parameter to the constructor
* of {@link Criteria}.
* @since v1.2.2
*/
public function defaultScope()
{
return [];
}
/**
* Resets all scopes and criteria applied including default scope.
*
* @return Document
* @since v1.0
*/
public function resetScope()
{
$this->criteria = $this->getNewCriteria();
return $this;
}
/**
* Apply scopes to criteria, will create criteria object if not provided and pass it by reference
* @param CriteriaInterface|array|null $criteria
* @return CriteriaInterface
*/
public function apply(&$criteria = null)
{
if (null === $criteria)
{
return $this->getModelCriteria();
}
elseif (is_array($criteria))
{
$criteria = $this->getNewCriteria($criteria);
}
$criteria->mergeWith($this->criteria);
$criteria->mergeWith($this->getModelCriteria());
if ($criteria instanceof DecoratableInterface)
{
$criteria->decorateWith($this->getModel());
}
return $criteria;
}
public function reset()
{
$this->criteria = $this->getNewCriteria();
return $this;
}
protected function getModelCriteria()
{
$criteria = null;
if ($this->model instanceof WithCriteriaInterface)
{
$criteria = $this->model->getDbCriteria();
}
elseif ($this->model instanceof CriteriaAwareInterface)
{
$criteria = $this->model->getCriteria();
}
if (empty($criteria))
{
return $this->getNewCriteria();
}
return $criteria;
}
abstract public function getNewCriteria($criteria = null);
}
API documentation generated by ApiGen