Completed
Pull Request — master (#47)
by ARCANEDEV
06:27
created

MetaCollection::addOne()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php namespace Arcanedev\SeoHelper\Bases;
2
3
use Arcanedev\SeoHelper\Contracts\Entities\MetaCollection as MetaCollectionContract;
4
use Arcanedev\SeoHelper\Contracts\Helpers\Meta as MetaContract;
5
use Arcanedev\SeoHelper\Contracts\Renderable;
6
use Arcanedev\SeoHelper\Helpers\Meta;
7
use Illuminate\Support\Collection;
8
9
/**
10
 * Class     MetaCollection
11
 *
12
 * @package  Arcanedev\SeoHelper\Bases
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
abstract class MetaCollection extends Collection implements MetaCollectionContract
16
{
17
    /* -----------------------------------------------------------------
18
     |  Properties
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Meta tag prefix.
24
     *
25
     * @var string
26
     */
27
    protected $prefix = '';
28
29
    /**
30
     * Meta tag name property.
31
     *
32
     * @var string
33
     */
34
    protected $nameProperty = 'name';
35
36
    /**
37
     * The items contained in the collection.
38
     *
39
     * @var array
40
     */
41
    protected $items = [];
42
43
    /**
44
     * Ignored tags, they have dedicated class.
45
     *
46
     * @var array
47
     */
48
    protected $ignored = [];
49
50
    /* -----------------------------------------------------------------
51
     |  Getters & Setters
52
     | -----------------------------------------------------------------
53
     */
54
55
    /**
56
     * Set meta prefix name.
57
     *
58
     * @param  string  $prefix
59
     *
60
     * @return \Arcanedev\SeoHelper\Bases\MetaCollection
61
     */
62 280
    public function setPrefix($prefix)
63
    {
64 280
        $this->prefix = $prefix;
65
66 280
        return $this->refresh();
67
    }
68
69
    /* -----------------------------------------------------------------
70
     |  Main Functions
71
     | -----------------------------------------------------------------
72
     */
73
74
    /**
75
     * Add many meta tags.
76
     *
77
     * @param  array  $metas
78
     *
79
     * @return \Arcanedev\SeoHelper\Bases\MetaCollection
80
     */
81 348
    public function addMany(array $metas)
82
    {
83 348
        foreach ($metas as $name => $content) {
84 168
            $this->addOne($name, $content);
85
        }
86
87 348
        return $this;
88
    }
89
90
    /**
91
     * Add a meta to collection.
92
     *
93
     * @param  string        $name
94
     * @param  string|array  $content
95
     *
96
     * @return \Arcanedev\SeoHelper\Bases\MetaCollection
97
     */
98 280
    public function addOne($name, $content)
99
    {
100 280
        if (empty($name) || empty($content))
101 192
            return $this;
102
103 280
        return $this->addMeta($name, $content);
104
    }
105
106
    /**
107
     * Make a meta and add it to collection.
108
     *
109
     * @param  string        $name
110
     * @param  string|array  $content
111
     *
112
     * @return \Arcanedev\SeoHelper\Bases\MetaCollection
113
     */
114 280
    protected function addMeta($name, $content)
115
    {
116 280
        $meta = Meta::make($name, $content, $this->nameProperty, $this->prefix);
117
118 280
        return $this->put($meta->key(), $meta);
119
    }
120
121
    /**
122
     * Remove a meta from the collection by key.
123
     *
124
     * @param  array|string  $names
125
     *
126
     * @return \Arcanedev\SeoHelper\Bases\MetaCollection
127
     */
128 12
    public function remove($names)
129
    {
130 12
        return $this->forget(
131 12
            $this->prepareName($names)
132
        );
133
    }
134
135
    /**
136
     * Render the tag.
137
     *
138
     * @return string
139
     */
140 312
    public function render()
141
    {
142
        return $this->map(function (Renderable $meta) {
143 308
                return $meta->render();
144 312
            })
145 312
            ->filter()
146 312
            ->implode(PHP_EOL);
147
    }
148
149
    /**
150
     * Reset the collection.
151
     *
152
     * @return \Arcanedev\SeoHelper\Bases\MetaCollection
153
     */
154 16
    public function reset()
155
    {
156 16
        $this->items = [];
157
158 16
        return $this;
159
    }
160
161
    /**
162
     * Render the tag.
163
     *
164
     * @return string
165
     */
166 4
    public function __toString()
167
    {
168 4
        return $this->render();
169
    }
170
171
    /* -----------------------------------------------------------------
172
     |  Check Functions
173
     | -----------------------------------------------------------------
174
     */
175
176
    /**
177
     * Check if meta is ignored.
178
     *
179
     * @param  string  $name
180
     *
181
     * @return bool
182
     */
183 184
    protected function isIgnored($name)
184
    {
185 184
        return in_array($name, $this->ignored);
186
    }
187
188
    /* -----------------------------------------------------------------
189
     |  Other Functions
190
     | -----------------------------------------------------------------
191
     */
192
193
    /**
194
     * Remove an item from the collection by key.
195
     *
196
     * @param  string|array  $keys
197
     *
198
     * @return \Arcanedev\SeoHelper\Bases\MetaCollection
199
     */
200 12
    public function forget($keys)
201
    {
202 12
        foreach ((array) $keys as $key) {
203 12
            $this->offsetUnset($key);
204
        }
205
206 12
        return $this;
207
    }
208
209
    /**
210
     * Refresh meta collection items.
211
     *
212
     * @return \Arcanedev\SeoHelper\Bases\MetaCollection
213
     */
214 280
    private function refresh()
215
    {
216
        return $this->map(function (MetaContract $meta) {
217 4
            return $meta->setPrefix($this->prefix);
218 280
        });
219
    }
220
221
    /**
222
     * Prepare names.
223
     *
224
     * @param  array|string  $names
225
     *
226
     * @return array
227
     */
228 12
    protected function prepareName($names)
229
    {
230
        return array_map(function ($name) {
231 12
            return strtolower(trim($name));
232 12
        }, (array) $names);
233
    }
234
}
235