|
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
|
|
|
* @method BaseObject parentFind($identifier) |
|
22
|
|
|
*/ |
|
23
|
|
|
trait AccessorByString |
|
24
|
|
|
{ |
|
25
|
|
|
use Accessor { |
|
26
|
|
|
find as parentFind; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract protected static function stringProperty(): string; |
|
33
|
|
|
|
|
34
|
|
|
/******************************************* |
|
35
|
|
|
* OVERRIDE |
|
36
|
|
|
*******************************************/ |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param $identifier |
|
40
|
|
|
* @return BaseObject|null |
|
41
|
|
|
*/ |
|
42
|
|
|
public function find($identifier) |
|
43
|
|
|
{ |
|
44
|
|
|
if (!is_numeric($identifier) && is_string($identifier)) { |
|
45
|
|
|
return $this->findByString($identifier); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->parentFind($identifier); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/******************************************* |
|
52
|
|
|
* FIND STRING |
|
53
|
|
|
*******************************************/ |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $string |
|
57
|
|
|
* @return BaseObject|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public function findByString(string $string) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->findByCondition( |
|
62
|
|
|
[self::stringProperty() => $string] |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $string |
|
68
|
|
|
* @throws ObjectNotFoundException |
|
69
|
|
|
* @return BaseObject|null |
|
70
|
|
|
*/ |
|
71
|
|
|
public function getByString(string $string) |
|
72
|
|
|
{ |
|
73
|
|
|
if (null === ($record = $this->findByString($string))) { |
|
74
|
|
|
$this->notFoundByStringException($string); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $record; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string|null $string |
|
82
|
|
|
* @throws ObjectNotFoundException |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function notFoundByStringException(string $string = null) |
|
85
|
|
|
{ |
|
86
|
|
|
throw new ObjectNotFoundException( |
|
87
|
|
|
sprintf( |
|
88
|
|
|
'Object does not exist with the string "%s".', |
|
89
|
|
|
(string)$string |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|