1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package WPEmerge |
4
|
|
|
* @author Atanas Angelov <[email protected]> |
5
|
|
|
* @copyright 2017-2019 Atanas Angelov |
6
|
|
|
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0 |
7
|
|
|
* @link https://wpemerge.com/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WPEmerge\Helpers; |
11
|
|
|
|
12
|
|
|
use WPEmerge\Support\Arr; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Represent an object which has an array of attributes. |
16
|
|
|
*/ |
17
|
|
|
trait HasAttributesTrait { |
18
|
|
|
/** |
19
|
|
|
* Attributes. |
20
|
|
|
* |
21
|
|
|
* @var array<string, mixed> |
22
|
|
|
*/ |
23
|
|
|
protected $attributes = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Get attribute. |
27
|
|
|
* |
28
|
|
|
* @param string $attribute |
29
|
|
|
* @param mixed $default |
30
|
|
|
* @return mixed |
31
|
|
|
*/ |
32
|
1 |
|
public function getAttribute( $attribute, $default = '' ) { |
33
|
1 |
|
return Arr::get( $this->getAttributes(), $attribute, $default ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get all attributes. |
38
|
|
|
* |
39
|
|
|
* @return array<string, mixed> |
40
|
|
|
*/ |
41
|
1 |
|
public function getAttributes() { |
42
|
1 |
|
return $this->attributes; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set attribute. |
47
|
|
|
* |
48
|
|
|
* @param string $attribute |
49
|
|
|
* @param mixed $value |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
1 |
|
public function setAttribute( $attribute, $value ) { |
53
|
1 |
|
$this->setAttributes( array_merge( |
54
|
1 |
|
$this->getAttributes(), |
55
|
1 |
|
[$attribute => $value] |
56
|
|
|
) ); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Fluent alias for setAttribute(). |
61
|
|
|
* |
62
|
|
|
* @codeCoverageIgnore |
63
|
|
|
* @param string $attribute |
64
|
|
|
* @param mixed $value |
65
|
|
|
* @return static $this |
66
|
|
|
*/ |
67
|
|
|
public function attribute( $attribute, $value ) { |
68
|
|
|
$this->setAttribute( $attribute, $value ); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set all attributes. |
75
|
|
|
* No attempt to merge attributes is done - this is a direct overwrite operation. |
76
|
|
|
* |
77
|
|
|
* @param array<string, mixed> $attributes |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
1 |
|
public function setAttributes( $attributes ) { |
81
|
1 |
|
$this->attributes = $attributes; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Fluent alias for setAttributes(). |
86
|
|
|
* |
87
|
|
|
* @codeCoverageIgnore |
88
|
|
|
* @param array<string, mixed> $attributes |
89
|
|
|
* @return static $this |
90
|
|
|
*/ |
91
|
|
|
public function attributes( $attributes ) { |
92
|
|
|
$this->setAttributes( $attributes ); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|