Completed
Push — master ( e1987d...bf6604 )
by ARCANEDEV
07:44
created

MetaCollection::refresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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