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
<?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\Traits;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Cursor;
use Maslosoft\Mangan\Finder;
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
use Maslosoft\Mangan\Interfaces\FinderInterface;
/**
* Finder trait contains implementation of `FinderInterface`, and it forwards
* them to concrete Finder class. Finder used by this trait
* can be defined via Finder annotation.
*
* This trait could be used as a part of active document class.
*
* @see FinderInterface
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
trait FinderTrait
{
/**
* Finder
* @var Finder
*/
private $_finder = null;
/**
* Finds a single Document with the specified condition.
*
* @param array|CriteriaInterface $criteria query criteria.
* @return AnnotatedInterface|static|null
* @Ignored
*/
public function find($criteria = null)
{
return $this->_getFinder()->find($criteria);
}
/**
* Finds document with the specified primary key. Primary key by default
* is defined by `_id` field. But could be any other. For simple (one column)
* keys use it's value.
*
* For composite use key-value with column names as keys
* and values for values.
*
* Example for simple pk:
* ```php
* $pk = '51b616fcc0986e30026d0748'
* ```
*
* Composite pk:
* ```php
* $pk = [
* 'mainPk' => 1,
* 'secondaryPk' => 2
* ];
* ```
*
* @param mixed $pk primary key value. Use array for composite key.
* @param array|CriteriaInterface $criteria
* @return AnnotatedInterface|static|null
* @Ignored
*/
public function findByPk($pk, $criteria = null)
{
return $this->_getFinder()->findByPk($pk, $criteria);
}
/**
* Finds document with the specified attributes.
* Attributes should be specified as key-value pairs.
* This allows easier syntax for simple queries.
*
* Example:
* ```php
* $attributes = [
* 'name' => 'John',
* 'title' => 'dr'
* ];
* ```
*
* @param mixed[] Array of stributes and values in form of ['attributeName' => 'value']
* @return AnnotatedInterface|static|null
* @Ignored
*/
public function findByAttributes(array $attributes)
{
return $this->_getFinder()->findByAttributes($attributes);
}
/**
* Finds all documents satisfying the specified condition.
*
* @param array|CriteriaInterface $criteria query criteria.
* @return AnnotatedInterface[]|static[]|Cursor
* @Ignored
*/
public function findAll($criteria = null)
{
return $this->_getFinder()->findAll($criteria);
}
/**
* Finds all documents with the specified attributes.
*
* @param mixed[] Array of stributes and values in form of ['attributeName' => 'value']
* @return AnnotatedInterface[]|static[]|Cursor
* @Ignored
*/
public function findAllByAttributes(array $attributes)
{
return $this->_getFinder()->findAllByAttributes($attributes);
}
/**
* Finds all documents with the specified primary keys.
* In MongoDB world every document has '_id' unique field, so with this method that
* field is in use as PK by default.
* See {@link find()} for detailed explanation about $condition.
*
* @param mixed $pk primary key value(s). Use array for multiple primary keys. For composite key, each key value must be an array (column name=>column value).
* @param array|CriteriaInterface $criteria query criteria.
* @return AnnotatedInterface[]|static[]|Cursor
* @Ignored
*/
public function findAllByPk($pk, $criteria = null)
{
return $this->_getFinder()->findAllByPk($pk, $criteria);
}
/**
* Counts all documents satisfying the specified condition.
*
* @param array|CriteriaInterface $criteria query criteria.
* @return int
* @Ignored
*/
public function count($criteria = null)
{
return $this->_getFinder()->count($criteria);
}
/**
* Counts all documents found by attribute values.
*
* Example:
* ```php
* $attributes = [
* 'name' => 'John',
* 'title' => 'dr'
* ];
* ```
*
* @param mixed[] Array of attributes and values in form of ['attributeName' => 'value']
* @return int
* @Ignored
*/
public function countByAttributes(array $attributes)
{
return $this->_getFinder()->countByAttributes($attributes);
}
/**
* Checks whether there is document satisfying the specified condition.
*
* @param CriteriaInterface $criteria
* @return bool
* @Ignored
*/
public function exists(CriteriaInterface $criteria = null)
{
return $this->_getFinder()->exists($criteria);
}
/**
* Whenever to use cursor
*
* @param boolean $useCursor
* @return FinderInterface
* @Ignored
*/
public function withCursor($useCursor = true)
{
return $this->_getFinder()->withCursor($useCursor);
}
/**
* Get finder instance
* @return FinderInterface
*/
private function _getFinder()
{
if (null === $this->_finder)
{
assert($this instanceof AnnotatedInterface, sprintf('Trait `%s` can be used only on `%s` instances', __CLASS__, AnnotatedInterface::class));
$this->_finder = Finder::create($this);
}
return $this->_finder;
}
}
API documentation generated by ApiGen