|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
|
5
|
|
|
* @license https://github.com/flipbox/spark/blob/master/LICENSE |
|
6
|
|
|
* @link https://github.com/flipbox/spark |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace flipbox\spark\helpers; |
|
10
|
|
|
|
|
11
|
|
|
use craft\base\Element; |
|
12
|
|
|
use craft\base\ElementInterface; |
|
13
|
|
|
use craft\helpers\ElementHelper as BaseElementHelper; |
|
14
|
|
|
use yii\base\InvalidConfigException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Flipbox Factory <[email protected]> |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
class ElementHelper extends BaseElementHelper |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The default scenario |
|
25
|
|
|
*/ |
|
26
|
|
|
const DEFAULT_SCENARIO = self::SCENARIO_DEFAULT; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The scenario used by default |
|
30
|
|
|
*/ |
|
31
|
|
|
const SCENARIO_DEFAULT = 'default'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The scenario used to populate an element |
|
35
|
|
|
*/ |
|
36
|
|
|
const SCENARIO_POPULATE = 'populate'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The scenario used to insert an element |
|
40
|
|
|
*/ |
|
41
|
|
|
const SCENARIO_INSERT = 'insert'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The scenario used to update an element |
|
45
|
|
|
*/ |
|
46
|
|
|
const SCENARIO_UPDATE = 'update'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The scenario used to save an element |
|
50
|
|
|
*/ |
|
51
|
|
|
const SCENARIO_SAVE = 'save'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $config |
|
55
|
|
|
* @param null $instanceOf |
|
56
|
|
|
* @param string|null $toScenario |
|
57
|
|
|
* @throws InvalidConfigException |
|
58
|
|
|
* @return ElementInterface |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function create( |
|
61
|
|
|
$config, |
|
62
|
|
|
$instanceOf = null, |
|
63
|
|
|
string $toScenario = null |
|
64
|
|
|
): ElementInterface { |
|
65
|
|
|
|
|
66
|
|
|
// Get class from config |
|
67
|
|
|
$class = ObjectHelper::checkConfig($config, $instanceOf); |
|
68
|
|
|
|
|
69
|
|
|
// New model |
|
70
|
|
|
$model = new $class(); |
|
71
|
|
|
|
|
72
|
|
|
return static::populate($model, $config, $toScenario); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param ElementInterface $element |
|
77
|
|
|
* @param array $attributes |
|
78
|
|
|
* @param string|null $toScenario |
|
79
|
|
|
* @return ElementInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function populate( |
|
82
|
|
|
ElementInterface $element, |
|
83
|
|
|
$attributes = [], |
|
84
|
|
|
string $toScenario = null |
|
85
|
|
|
): ElementInterface { |
|
86
|
|
|
|
|
87
|
|
|
/** @var Element $element */ |
|
88
|
|
|
|
|
89
|
|
|
// Set scenario |
|
90
|
|
|
if (null !== $toScenario) { |
|
91
|
|
|
$element->setScenario($toScenario); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// Populate model attributes |
|
95
|
|
|
$element->setAttributes($attributes); |
|
96
|
|
|
|
|
97
|
|
|
return $element; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|