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
<?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 Iterator;
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
use Maslosoft\Mangan\Adapters\Finder\MongoAdapter;
use Maslosoft\Mangan\Cursor;
use Maslosoft\Mangan\Finder;
use Maslosoft\Mangan\Helpers\PkManager;
use Maslosoft\Mangan\Interfaces\Adapters\FinderAdapterInterface;
use Maslosoft\Mangan\Interfaces\Adapters\FinderCursorInterface;
use Maslosoft\Mangan\Interfaces\CriteriaInterface;
use Maslosoft\Mangan\Interfaces\FinderEventsInterface;
use Maslosoft\Mangan\Interfaces\FinderInterface;
use Maslosoft\Mangan\Interfaces\ModelAwareInterface;
use Maslosoft\Mangan\Interfaces\ProfilerInterface;
use Maslosoft\Mangan\Interfaces\ScenariosInterface;
use Maslosoft\Mangan\Interfaces\ScopeManagerInterface;
use Maslosoft\Mangan\ScenarioManager;
use Maslosoft\Mangan\Traits\Finder\FinderHelpers;
use Maslosoft\Mangan\Traits\ModelAwareTrait;
use MongoCursor;
use UnexpectedValueException;
/**
* AbstractFinder
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
abstract class AbstractFinder implements FinderInterface, ModelAwareInterface
{
use ModelAwareTrait;
/**
* Whenever to use cursors
* @var bool
*/
private $useCursor = false;
// <editor-fold defaultstate="collapsed" desc="Required getters/setters">
/**
* @see FinderHelpers
* @return FinderAdapterInterface|MongoAdapter
*/
abstract public function getAdapter();
/**
* @see FinderHelpers
* @return ScopeManagerInterface
*/
abstract public function getScopeManager();
/**
* @see FinderHelpers
* @return FinderEventsInterface
*/
abstract public function getFinderEvents();
/**
* @see FinderHelpers
* @return ProfilerInterface
*/
abstract public function getProfiler();
/**
* @see FinderHelpers
* @return static
*/
abstract public function setAdapter(FinderAdapterInterface $adapter);
/**
* @see FinderHelpers
* @return static
*/
abstract public function setScopeManager(ScopeManagerInterface $scopeManager);
/**
* @see FinderHelpers
* @return static
*/
abstract public function setFinderEvents(FinderEventsInterface $finderEvents);
/**
* @see FinderHelpers
* @return static
*/
abstract public function setProfiler(ProfilerInterface $profiler);
// </editor-fold>
/**
* Finds a single Document with the specified condition.
*
* @param array|CriteriaInterface $criteria query criteria.
* @return AnnotatedInterface
* @Ignored
*/
public function find($criteria = null)
{
if ($this->getFinderEvents()->beforeFind($this))
{
$criteria = $this->getScopeManager()->apply($criteria);
$data = $this->getAdapter()->findOne($criteria, $criteria->getSelect());
return $this->populateRecord($data);
}
return null;
}
/**
* 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 $pkValue primary key value. Use array for composite key.
* @param array|CriteriaInterface $criteria
* @return AnnotatedInterface|null
* @Ignored
*/
public function findByPk($pkValue, $criteria = null)
{
$pkCriteria = $this->getScopeManager()->getNewCriteria($criteria);
$pkCriteria->mergeWith(PkManager::prepare($this->getModel(), $pkValue));
return $this->find($pkCriteria);
}
/**
* 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|null
*/
public function findByAttributes(array $attributes)
{
$criteria = $this->getScopeManager()->getNewCriteria();
foreach ($attributes as $name => $value)
{
$criteria->addCond($name, '==', $value);
}
return $this->find($criteria);
}
/**
* Finds all documents satisfying the specified condition.
* See {@link find()} for detailed explanation about $condition and $params.
*
* @param array|CriteriaInterface $criteria query criteria.
* @return AnnotatedInterface[]|Cursor
*/
public function findAll($criteria = null)
{
if ($this->getFinderEvents()->beforeFind($this))
{
$criteria = $this->getScopeManager()->apply($criteria);
$cursor = $this->getAdapter()->findMany($criteria);
assert(is_object($cursor), sprintf('Expected cursor to be compatible object, got %s', gettype($cursor)));
assert($cursor instanceof FinderCursorInterface || $cursor instanceof MongoCursor, new UnexpectedValueException(sprintf('Expected `%s` or `%s` got `%s`', FinderCursorInterface::class, MongoCursor::class, get_class($cursor))));
if ($criteria->getSort() !== null)
{
$cursor->sort($criteria->getSort());
}
if ($criteria->getLimit() !== null)
{
$cursor->limit($criteria->getLimit());
}
if ($criteria->getOffset() !== null)
{
$cursor->skip($criteria->getOffset());
}
if ($criteria->getSelect())
{
$cursor->fields($criteria->getSelect());
}
$this->getProfiler()->cursor($cursor);
if ($this->isWithCursor())
{
return new Cursor($cursor, $this->getModel());
}
else
{
return $this->populateRecords($cursor);
}
}
return [];
}
/**
* Finds all documents with the specified attributes.
*
* @param mixed[] Array of stributes and values in form of ['attributeName' => 'value']
* @return AnnotatedInterface[]|Cursor - Array or cursor of Documents
* @since v1.0
*/
public function findAllByAttributes(array $attributes)
{
$criteria = $this->getScopeManager()->getNewCriteria();
foreach ($attributes as $name => $value)
{
$criteria->$name('==', $value);
}
return $this->findAll($criteria);
}
/**
* 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 $pkValues 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[]|Cursor - Array or cursor of Documents
* @since v1.0
*/
public function findAllByPk($pkValues, $criteria = null)
{
$pkCriteria = $this->getScopeManager()->getNewCriteria($criteria);
PkManager::prepareAll($this->getModel(), $pkValues, $pkCriteria);
return $this->findAll($pkCriteria);
}
/**
* Counts all documents satisfying the specified condition.
* See {@link find()} for detailed explanation about $condition and $params.
* @param array|CriteriaInterface $criteria query criteria.
* @return integer Count of all documents satisfying the specified condition.
* @since v1.0
*/
public function count($criteria = null)
{
if ($this->getFinderEvents()->beforeCount($this))
{
$criteria = $this->getScopeManager()->apply($criteria);
$count = $this->getAdapter()->count($criteria);
$this->getFinderEvents()->afterCount($this);
return $count;
}
return 0;
}
/**
* 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
* @since v1.2.2
* @Ignored
*/
public function countByAttributes(array $attributes)
{
if ($this->getFinderEvents()->beforeCount($this))
{
$criteria = $this->getScopeManager()->getNewCriteria();
foreach ($attributes as $name => $value)
{
$criteria->$name = $value;
}
$scopedCriteria = $this->getScopeManager()->apply($criteria);
$count = $this->getAdapter()->count($scopedCriteria);
$this->getFinderEvents()->afterCount($this);
return $count;
}
return 0;
}
/**
* Checks whether there is document satisfying the specified condition.
*
* @param CriteriaInterface $criteria
* @return bool
*/
public function exists(CriteriaInterface $criteria = null)
{
if ($this->getFinderEvents()->beforeExists($this))
{
$criteria = $this->getScopeManager()->apply($criteria);
//Select only Pk Fields to not fetch possibly large document
$pkKeys = PkManager::getPkKeys($this->getModel());
if (is_string($pkKeys))
{
$pkKeys = [$pkKeys];
}
$cursor = $this->getAdapter()->findMany($criteria, $pkKeys);
$cursor->limit(1);
// NOTE: Cannot use count(true) here because of hhvm mongofill compatibility, see:
// https://github.com/mongofill/mongofill/issues/86
$exists = ($cursor->count() !== 0);
$this->getFinderEvents()->afterExists($this);
return $exists;
}
return false;
}
/**
* Resets all scopes and criteria applied including default scope.
*
* @return Finder
* @since v1.0
*/
public function resetScope()
{
$this->getScopeManager()->reset();
return $this;
}
/**
* Whenever to use cursor
* @param bool $useCursor
* @return FinderInterface
*/
public function withCursor($useCursor = true)
{
$this->useCursor = $useCursor;
return $this;
}
public function isWithCursor()
{
return $this->useCursor;
}
/**
* Creates an model with the given attributes.
* This method is internally used by the find methods.
* @param mixed[] $data attribute values (column name=>column value)
* @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class.
* Null is returned if the input data is false.
* @since v1.0
*/
protected function populateRecord($data)
{
if ($data !== null)
{
$model = $this->createModel($data);
ScenarioManager::setScenario($model, ScenariosInterface::Update);
$this->getFinderEvents()->afterFind($this, $model);
return $model;
}
else
{
return null;
}
}
/**
* Creates a list of documents based on the input data.
* This method is internally used by the find methods.
* @param Iterator|array $cursor Results found to populate active records.
* @return AnnotatedInterface[] array list of active records.
* @since v1.0
*/
private function populateRecords($cursor)
{
$records = array();
foreach ($cursor as $data)
{
$records[] = $this->populateRecord($data);
}
return $records;
}
}
API documentation generated by ApiGen