1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\ember\services\traits\objects; |
10
|
|
|
|
11
|
|
|
use flipbox\ember\exceptions\ObjectNotFoundException; |
12
|
|
|
use yii\base\BaseObject; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Similar to the inherited Accessor, you can also specific a unique string (typically a handle) to |
16
|
|
|
* retrieve an object. |
17
|
|
|
* |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
trait AccessorByString |
22
|
|
|
{ |
23
|
|
|
use Accessor { |
24
|
|
|
find as parentFind; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
abstract protected static function stringProperty(): string; |
31
|
|
|
|
32
|
|
|
/******************************************* |
33
|
|
|
* OVERRIDE |
34
|
|
|
*******************************************/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param $identifier |
38
|
|
|
* @param string|null $toScenario |
39
|
|
|
* @return BaseObject|null |
40
|
|
|
*/ |
41
|
|
|
public function find($identifier, string $toScenario = null) |
42
|
|
|
{ |
43
|
|
|
if (!is_numeric($identifier) && is_string($identifier)) { |
44
|
|
|
return $this->findByString($identifier, $toScenario); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $this->parentFind($identifier, $toScenario); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/******************************************* |
51
|
|
|
* FIND STRING |
52
|
|
|
*******************************************/ |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $string |
56
|
|
|
* @return BaseObject|null |
57
|
|
|
*/ |
58
|
|
|
public function findByString(string $string) |
59
|
|
|
{ |
60
|
|
|
return $this->findByCondition( |
61
|
|
|
[self::stringProperty() => $string] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $string |
67
|
|
|
* @throws ObjectNotFoundException |
68
|
|
|
* @return BaseObject|null |
69
|
|
|
*/ |
70
|
|
|
public function getByString(string $string) |
71
|
|
|
{ |
72
|
|
|
if (null === ($record = $this->findByString($string))) { |
73
|
|
|
$this->notFoundByStringException($string); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $record; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param string|null $string |
81
|
|
|
* @throws ObjectNotFoundException |
82
|
|
|
*/ |
83
|
|
|
protected function notFoundByStringException(string $string = null) |
84
|
|
|
{ |
85
|
|
|
throw new ObjectNotFoundException( |
86
|
|
|
sprintf( |
87
|
|
|
'Object does not exist with the string "%s".', |
88
|
|
|
(string)$string |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.