1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/mangan |
7
|
|
|
* @licence AGPL or Commercial |
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
9
|
|
|
* @copyright Copyright (c) Maslosoft |
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
11
|
|
|
* @link http://maslosoft.com/mangan/ |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan; |
15
|
|
|
|
16
|
|
|
use Iterator; |
17
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
18
|
|
|
use Maslosoft\Mangan\Abstracts\AbstractFinder; |
19
|
|
|
use Maslosoft\Mangan\Adapters\Finder\MongoAdapter; |
20
|
|
|
use Maslosoft\Mangan\Exceptions\ManganException; |
21
|
|
|
use Maslosoft\Mangan\Helpers\FinderEvents; |
22
|
|
|
use Maslosoft\Mangan\Helpers\PkManager; |
23
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
24
|
|
|
use Maslosoft\Mangan\Interfaces\EntityManagerInterface; |
25
|
|
|
use Maslosoft\Mangan\Interfaces\FinderInterface; |
26
|
|
|
use Maslosoft\Mangan\Interfaces\ScenariosInterface; |
27
|
|
|
use Maslosoft\Mangan\Meta\ManganMeta; |
28
|
|
|
use Maslosoft\Mangan\Traits\Finder\FinderHelpers; |
29
|
|
|
use Maslosoft\Mangan\Transformers\RawArray; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Basic Finder implementation |
33
|
|
|
* |
34
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
35
|
|
|
*/ |
36
|
|
|
class Finder extends AbstractFinder implements FinderInterface |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
use FinderHelpers; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
* |
44
|
|
|
* @param object $model Model instance |
45
|
|
|
* @param EntityManagerInterface $em |
46
|
|
|
* @param Mangan $mangan |
47
|
|
|
*/ |
48
|
69 |
|
public function __construct($model, $em = null, $mangan = null) |
49
|
|
|
{ |
50
|
69 |
|
$this->setModel($model); |
51
|
69 |
|
$this->setScopeManager(new ScopeManager($model)); |
52
|
69 |
|
if (null === $mangan) |
53
|
|
|
{ |
54
|
69 |
|
$mangan = Mangan::fromModel($model); |
55
|
|
|
} |
56
|
69 |
|
$this->setAdapter(new MongoAdapter($model, $mangan, $em)); |
57
|
|
|
|
58
|
69 |
|
$this->setProfiler($mangan->getProfiler()); |
59
|
69 |
|
$this->setFinderEvents(new FinderEvents); |
60
|
69 |
|
$this->withCursor($mangan->useCursor); |
61
|
69 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Create model related finder. |
65
|
|
|
* This will create customized finder if defined in model with Finder annotation. |
66
|
|
|
* If no custom finder is defined this will return default Finder. |
67
|
|
|
* |
68
|
|
|
* @param AnnotatedInterface $model |
69
|
|
|
* @param EntityManagerInterface $em |
70
|
|
|
* @param Mangan $mangan |
71
|
|
|
* @return FinderInterface |
72
|
|
|
*/ |
73
|
9 |
|
public static function create(AnnotatedInterface $model, $em = null, Mangan $mangan = null) |
74
|
|
|
{ |
75
|
9 |
|
$finderClass = ManganMeta::create($model)->type()->finder ?: static::class; |
76
|
9 |
|
return new $finderClass($model, $em, $mangan); |
77
|
|
|
} |
78
|
|
|
|
79
|
58 |
|
protected function createModel($data) |
80
|
|
|
{ |
81
|
58 |
|
if (!empty($data['$err'])) |
82
|
|
|
{ |
83
|
|
|
throw new ManganException(sprintf("There is an error in query: %s", $data['$err'])); |
84
|
|
|
} |
85
|
58 |
|
return RawArray::toModel($data, $this->getModel()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|