|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maslosoft\Mangan\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
6
|
|
|
use Maslosoft\Mangan\Events\Event; |
|
7
|
|
|
use Maslosoft\Mangan\Interfaces\FinderEventsInterface; |
|
8
|
|
|
use Maslosoft\Mangan\Interfaces\FinderInterface; |
|
9
|
|
|
use Maslosoft\Mangan\Interfaces\ModelAwareInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* FinderEvents |
|
13
|
|
|
* |
|
14
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
15
|
|
|
*/ |
|
16
|
|
|
class FinderEvents implements FinderEventsInterface |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
19 |
|
public function afterCount(FinderInterface $finder) |
|
20
|
|
|
{ |
|
21
|
19 |
|
$this->trigger($finder, FinderInterface::EventAfterCount); |
|
22
|
19 |
|
} |
|
23
|
|
|
|
|
24
|
9 |
|
public function afterExists(FinderInterface $finder) |
|
25
|
|
|
{ |
|
26
|
9 |
|
$this->trigger($finder, FinderInterface::EventAfterExists); |
|
27
|
9 |
|
} |
|
28
|
|
|
|
|
29
|
58 |
|
public function afterFind(FinderInterface $finder, AnnotatedInterface $model) |
|
30
|
|
|
{ |
|
31
|
58 |
|
Event::trigger($model, FinderInterface::EventAfterFind); |
|
32
|
58 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Trigger before count event |
|
36
|
|
|
* @return boolean |
|
37
|
|
|
*/ |
|
38
|
19 |
|
public function beforeCount(FinderInterface $finder) |
|
39
|
|
|
{ |
|
40
|
19 |
|
return $this->handle($finder, FinderInterface::EventBeforeCount); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Trigger before exists event |
|
45
|
|
|
* @return boolean |
|
46
|
|
|
*/ |
|
47
|
9 |
|
public function beforeExists(FinderInterface $finder) |
|
48
|
|
|
{ |
|
49
|
9 |
|
return $this->handle($finder, FinderInterface::EventBeforeExists); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Trigger before find event |
|
54
|
|
|
* @return boolean |
|
55
|
|
|
*/ |
|
56
|
60 |
|
public function beforeFind(FinderInterface $finder) |
|
57
|
|
|
{ |
|
58
|
60 |
|
return $this->handle($finder, FinderInterface::EventBeforeFind); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
24 |
|
protected function trigger($finder, $event) |
|
62
|
|
|
{ |
|
63
|
24 |
|
assert($finder instanceof ModelAwareInterface); |
|
64
|
24 |
|
Event::trigger($finder->getModel(), $event); |
|
65
|
24 |
|
} |
|
66
|
|
|
|
|
67
|
69 |
|
protected function handle($finder, $event) |
|
68
|
|
|
{ |
|
69
|
69 |
|
assert($finder instanceof ModelAwareInterface); |
|
70
|
69 |
|
if (!Event::hasHandler($finder->getModel(), $event)) |
|
71
|
|
|
{ |
|
72
|
69 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
return Event::handled($finder->getModel(), $event); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|