-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathAttributableTrait.php
More file actions
79 lines (66 loc) · 1.4 KB
/
Copy pathAttributableTrait.php
File metadata and controls
79 lines (66 loc) · 1.4 KB
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
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
trait AttributableTrait {
/**
* @ORM\Column(type="json", nullable=false)
*
* @var array
*/
protected $attributes = [];
/**
* @return array
*/
public function getAttributes(): array {
return $this->attributes;
}
/**
* @param string $attribute
* @param mixed $def
*
* @return mixed|null
*/
public function getAttribute(string $attribute, $def = null) {
return $this->attributes[$attribute] ?? $def;
}
/**
* @param array $attributes
*
* @return $this
*/
public function setAttributes(array $attributes) {
$this->attributes = [];
return $this->addAttributes($attributes);
}
/**
* @param array $attributes
*
* @return $this
*/
public function addAttributes(array $attributes) {
foreach ($attributes as $key => $value)
$this->setAttribute($key, $value);
return $this;
}
/**
* @param string $attribute
* @param mixed|null $value
*
* @return $this
*/
public function setAttribute(string $attribute, $value) {
if ($value === null)
return $this->removeAttribute($attribute);
$this->attributes[$attribute] = $value;
return $this;
}
/**
* @param string $attribute
*
* @return $this
*/
public function removeAttribute(string $attribute) {
unset($this->attributes[$attribute]);
return $this;
}
}