Completed
Push — master ( 38ffd4...ed5eea )
by Evan
03:02
created

Meta::replace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silk\Meta;
4
5
use Illuminate\Support\Collection;
6
7
class Meta
8
{
9
    /**
10
     * Meta type
11
     * @var string
12
     */
13
    protected $type;
14
15
    /**
16
     * The object ID this metadata is for
17
     * @var int
18
     */
19
    protected $object_id;
20
21
    /**
22
     * The key the metadata is for
23
     * @var string
24
     */
25
    protected $key;
26
27
    /**
28
     * Meta Constructor.
29
     *
30
     * @param string     $type      Meta type
31
     * @param int|string $object_id ID of the object metadata is for
32
     * @param string     $key       Meta key
33
     */
34
    public function __construct($type, $object_id, $key)
35
    {
36
        $this->type      = $type;
37
        $this->object_id = (int) $object_id;
38
        $this->key       = $key;
39
    }
40
41
    /**
42
     * Get the single meta data.
43
     *
44
     * @return mixed
45
     */
46
    public function get()
47
    {
48
        return get_metadata($this->type, $this->object_id, $this->key, true);
49
    }
50
51
    /**
52
     * Get all meta data as a Collection.
53
     *
54
     * @return Collection
55
     */
56
    public function collect()
57
    {
58
        return Collection::make($this->all());
59
    }
60
61
    /**
62
     * Get all meta data as an array.
63
     *
64
     * @return array
65
     */
66
    public function all()
67
    {
68
        return (array) get_metadata($this->type, $this->object_id, $this->key, false);
69
    }
70
71
    /**
72
     * Set the new value.
73
     *
74
     * @param mixed  $value
75
     * @param string $prev_value
76
     *
77
     * @return $this
78
     */
79
    public function set($value, $prev_value = '')
80
    {
81
        update_metadata($this->type, $this->object_id, $this->key, $value, $prev_value);
82
83
        return $this;
84
    }
85
86
    /**
87
     * Replace a single value.
88
     *
89
     * @param  mixed $old  Previous value to update
90
     * @param  mixed $new  New value to set the previous value to
91
     *
92
     * @return $this
93
     */
94
    public function replace($old, $new)
95
    {
96
        return $this->set($new, $old);
97
    }
98
99
    /**
100
     * Add metadata for the specified object.
101
     *
102
     * @param mixed  $value   The value to add
103
     * @param bool   $unique  Whether the specified metadata key should be unique
104
     *                        for the object.  If true, and the object already has
105
     *                        a value for the specified metadata key, no change will be made.
106
     *
107
     * @return $this
108
     */
109
    public function add($value, $unique = false)
110
    {
111
        add_metadata($this->type, $this->object_id, $this->key, $value, $unique);
112
113
        return $this;
114
    }
115
116
    /**
117
     * Delete the meta data
118
     *
119
     * Deletes all meta data for the key, if provided, optionally filtered by
120
     * a previous value.
121
     * If no key was provided, all meta data for the object is deleted.
122
     *
123
     * @param  string $value The old value to delete.
124
     *                       This is only necessary when deleting a specific value
125
     *                       from an object which has multiple values for the key.
126
     *
127
     * @return $this
128
     */
129
    public function delete($value = '')
130
    {
131
        delete_metadata($this->type, $this->object_id, $this->key, $value);
132
133
        return $this;
134
    }
135
136
    /**
137
     * Determine if a meta key is set for a given object.
138
     *
139
     * @return bool     True of the key is set, false if not.
140
     */
141
    public function exists()
142
    {
143
        return metadata_exists($this->type, $this->object_id, $this->key);
144
    }
145
146
    /**
147
     * Get the meta data as a string.
148
     *
149
     * @return string  The meta value
150
     */
151
    public function __toString()
152
    {
153
        return $this->get();
154
    }
155
}
156