|
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; |
|
10
|
|
|
|
|
11
|
|
|
use yii\db\ActiveRecord as Record; |
|
12
|
|
|
use flipbox\ember\exceptions\RecordNotFoundException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Flipbox Factory <[email protected]> |
|
16
|
|
|
* @since 1.0.0 |
|
17
|
|
|
*/ |
|
18
|
|
|
trait ActiveRecordByString |
|
19
|
|
|
{ |
|
20
|
|
|
use ActiveRecord { |
|
21
|
|
|
find as parentFind; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return string |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract protected static function stringProperty(): string; |
|
28
|
|
|
|
|
29
|
|
|
/******************************************* |
|
30
|
|
|
* OVERRIDE |
|
31
|
|
|
*******************************************/ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param $identifier |
|
35
|
|
|
* @param string|null $toScenario |
|
36
|
|
|
* @return Record|null |
|
37
|
|
|
*/ |
|
38
|
|
|
public function find($identifier, string $toScenario = null) |
|
39
|
|
|
{ |
|
40
|
|
|
if (!is_numeric($identifier) && is_string($identifier)) { |
|
41
|
|
|
return $this->findByString($identifier, $toScenario); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $this->parentFind($identifier, $toScenario); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/******************************************* |
|
48
|
|
|
* FIND STRING |
|
49
|
|
|
*******************************************/ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $string |
|
53
|
|
|
* @param string|null $toScenario |
|
54
|
|
|
* @return Record|null |
|
55
|
|
|
*/ |
|
56
|
|
|
public function findByString(string $string, string $toScenario = null) |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->findByCondition( |
|
59
|
|
|
[self::stringProperty() => $string], |
|
60
|
|
|
$toScenario |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $string |
|
66
|
|
|
* @param string|null $toScenario |
|
67
|
|
|
* @throws RecordNotFoundException |
|
68
|
|
|
* @return Record|null |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getByString(string $string, string $toScenario = null) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$record = $this->findByString($string, $toScenario)) { |
|
73
|
|
|
$this->notFoundByStringException($string); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $record; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string|null $string |
|
81
|
|
|
* @throws RecordNotFoundException |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function notFoundByStringException(string $string = null) |
|
84
|
|
|
{ |
|
85
|
|
|
throw new RecordNotFoundException( |
|
86
|
|
|
sprintf( |
|
87
|
|
|
'Record does not exist with the string "%s".', |
|
88
|
|
|
(string)$string |
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.