-
Notifications
You must be signed in to change notification settings - Fork 638
/
Copy pathCustomFieldBehavior.php.template
126 lines (112 loc) · 2.74 KB
/
CustomFieldBehavior.php.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @link http://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license http://craftcms.com/license
*/
namespace craft\behaviors;
use yii\base\Behavior;
/**
* Custom field behavior
*
* This class provides attributes for all the unique custom field handles.
*
{METHOD_DOCS}
*/
class CustomFieldBehavior extends Behavior
{
/**
* @var bool Whether the behavior should provide methods based on the field handles.
*/
public bool $hasMethods = false;
/**
* @var bool Whether properties on the class should be settable directly.
*/
public bool $canSetProperties = true;
/**
* @var string[] List of supported field handles.
*/
public static $fieldHandles = [
/* HANDLES */
];
/* PROPERTIES */
/**
* @var array Additional custom field values we don’t know about yet.
*/
private array $_customFieldValues = [];
/**
* @inheritdoc
*/
public function __call($name, $params)
{
if ($this->hasMethods && isset(self::$fieldHandles[$name]) && count($params) === 1) {
$this->$name = $params[0];
return $this->owner;
}
return parent::__call($name, $params);
}
/**
* @inheritdoc
*/
public function hasMethod($name): bool
{
if ($this->hasMethods && isset(self::$fieldHandles[$name])) {
return true;
}
return parent::hasMethod($name);
}
/**
* @inheritdoc
*/
public function __isset($name): bool
{
if (isset(self::$fieldHandles[$name])) {
return true;
}
return parent::__isset($name);
}
/**
* @inheritdoc
*/
public function __get($name)
{
if (isset(self::$fieldHandles[$name])) {
return $this->_customFieldValues[$name] ?? null;
}
return parent::__get($name);
}
/**
* @inheritdoc
*/
public function __set($name, $value)
{
if (isset(self::$fieldHandles[$name])) {
$this->_customFieldValues[$name] = $value;
return;
}
parent::__set($name, $value);
}
/**
* @inheritdoc
*/
public function canGetProperty($name, $checkVars = true): bool
{
if ($checkVars && isset(self::$fieldHandles[$name])) {
return true;
}
return parent::canGetProperty($name, $checkVars);
}
/**
* @inheritdoc
*/
public function canSetProperty($name, $checkVars = true): bool
{
if (!$this->canSetProperties) {
return false;
}
if ($checkVars && isset(self::$fieldHandles[$name])) {
return true;
}
return parent::canSetProperty($name, $checkVars);
}
}