|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silk\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
|
|
7
|
|
|
class Meta |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Meta type |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $type; |
|
14
|
|
|
|
|
15
|
|
|
protected $object_id; |
|
16
|
|
|
|
|
17
|
|
|
protected $key; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $type Meta type |
|
21
|
|
|
* @param int|string $object_id ID of the object metadata is for |
|
22
|
|
|
* @param string $key [description] |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct($type, $object_id, $key) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->type = $type; |
|
27
|
|
|
$this->object_id = $object_id; |
|
28
|
|
|
$this->key = $key; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Get the single meta data |
|
33
|
|
|
* |
|
34
|
|
|
* @return mixed |
|
35
|
|
|
*/ |
|
36
|
|
|
public function get($key = '') |
|
37
|
|
|
{ |
|
38
|
|
|
if (! $this->key && $key) { |
|
39
|
|
|
return new static($this->type, $this->object_id, $key); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return get_metadata($this->type, $this->object_id, $this->key, true); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get all meta data |
|
47
|
|
|
* |
|
48
|
|
|
* @return Collection |
|
49
|
|
|
*/ |
|
50
|
|
|
public function all() |
|
51
|
|
|
{ |
|
52
|
|
|
return Collection::make(get_metadata($this->type, $this->object_id, $this->key, false)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Set the meta value |
|
57
|
|
|
* |
|
58
|
|
|
* @param mixed $value |
|
59
|
|
|
* @param string $prev_value [description] |
|
60
|
|
|
* @return bool True on success, false on failure |
|
61
|
|
|
*/ |
|
62
|
|
|
public function set($value, $prev_value = '') |
|
63
|
|
|
{ |
|
64
|
|
|
return update_metadata($this->type, $this->object_id, $this->key, $value, $prev_value); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Add metadata for the specified object |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $value The value to add |
|
71
|
|
|
* @param [type] $unique [description] |
|
|
|
|
|
|
72
|
|
|
* |
|
73
|
|
|
* @return int|false The meta ID on success, false on failure. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function add($value, $unique = false) |
|
76
|
|
|
{ |
|
77
|
|
|
return add_metadata($this->type, $this->object_id, $this->key, $value, $unique); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Delete the meta data |
|
82
|
|
|
* |
|
83
|
|
|
* Deletes all meta data for the key, if provided, optionally filtered by |
|
84
|
|
|
* a previous value. |
|
85
|
|
|
* If no key was provided, all meta data for the object is deleted. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $value The old value to delete. |
|
88
|
|
|
* This is only necessary when deleting a specific value |
|
89
|
|
|
* from an object which has multiple values for the key. |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool True on success, false on failure |
|
92
|
|
|
*/ |
|
93
|
|
|
public function delete($value = '') |
|
94
|
|
|
{ |
|
95
|
|
|
return delete_metadata($this->type, $this->object_id, $this->key, $value); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Determine if a meta key is set for a given object |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool True of the key is set, false if not. |
|
102
|
|
|
*/ |
|
103
|
|
|
public function exists() |
|
104
|
|
|
{ |
|
105
|
|
|
return metadata_exists($this->type, $this->object_id, $this->key); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the object_id |
|
110
|
|
|
* |
|
111
|
|
|
* @return int|string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getObjectId() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->object_id; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* |
|
120
|
|
|
*/ |
|
121
|
|
|
public function __get($property) |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->get($property); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function __toString() |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->get(); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.