1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Model; |
13
|
|
|
|
14
|
|
|
use Phalcon\Mvc\Model\Resultset\Simple; |
15
|
|
|
use Phalcon\Mvc\Model\ResultsetInterface; |
16
|
|
|
use Phalcon\Mvc\ModelInterface; |
17
|
|
|
use Zemit\Support\Helper; |
18
|
|
|
|
19
|
|
|
trait Events |
20
|
|
|
{ |
21
|
|
|
abstract public function fireEventCancel(string $eventName): bool; |
22
|
|
|
|
23
|
|
|
public static function find($parameters = null): ResultsetInterface |
24
|
|
|
{ |
25
|
|
|
$ret = self::fireEventCancelCall(__FUNCTION__, func_get_args()); |
26
|
|
|
|
27
|
|
|
if (!$ret) { |
28
|
|
|
$that = new self(); |
29
|
|
|
assert($that instanceof ModelInterface); |
30
|
|
|
$columnMap = $that->getModelsMetaData()->getColumnMap($that); |
31
|
|
|
return new Simple($columnMap, $that, false); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $ret; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function findFirst($parameters = null): ?ModelInterface |
38
|
|
|
{ |
39
|
|
|
$ret = self::fireEventCancelCall(__FUNCTION__, func_get_args()); |
40
|
|
|
return $ret ?: null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function count($parameters = null) |
44
|
|
|
{ |
45
|
|
|
return self::fireEventCancelCall(__FUNCTION__, func_get_args()); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public static function sum($parameters = null) |
49
|
|
|
{ |
50
|
|
|
return self::fireEventCancelCall(__FUNCTION__, func_get_args()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function average($parameters = null) |
54
|
|
|
{ |
55
|
|
|
return self::fireEventCancelCall(__FUNCTION__, func_get_args()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function fireEventCancelCall(string $method, array $arguments = []) |
59
|
|
|
{ |
60
|
|
|
$class = get_called_class(); |
61
|
|
|
$that = new $class(); |
62
|
|
|
$event = ucfirst(Helper::camelize($method)); |
63
|
|
|
|
64
|
|
|
if ($that->fireEventCancel('before' . $event) === false) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$ret = @parent::$method(...$arguments); // @todo fix this for php 83+ |
69
|
|
|
|
70
|
|
|
$that->fireEvent('after' . $event); |
71
|
|
|
|
72
|
|
|
return $ret; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|