Completed
Push — master ( 086d51...184cf7 )
by ARCANEDEV
12s
created

Url::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php namespace Arcanedev\LaravelSitemap\Entities;
2
3
use Arcanedev\LaravelSitemap\Contracts\Entities\Url as UrlContract;
4
use Arcanedev\LaravelSitemap\Exceptions\SitemapException;
5
use DateTime;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Fluent;
8
9
/**
10
 * Class     Url
11
 *
12
 * @package  Arcanedev\LaravelSitemap\Entities
13
 * @author   ARCANEDEV <[email protected]>
14
 */
15
class Url extends Fluent implements UrlContract
16
{
17
    /* -----------------------------------------------------------------
18
     |  Constructor
19
     | -----------------------------------------------------------------
20
     */
21
22
    /**
23
     * Url constructor.
24
     *
25
     * @param  array|string  $attributes
26
     */
27 80
    public function __construct($attributes = [])
28
    {
29 80
        if (is_string($attributes))
30 32
            $attributes = ['loc' => $attributes];
31
32 80
        parent::__construct($attributes);
33
34 80
        $this->setLoc(Arr::get($attributes, 'loc'));
35 80
        $this->setLastMod(Arr::get($attributes, 'lastmod', new DateTime));
36 80
        $this->setChangeFreq(Arr::get($attributes, 'changefreq', ChangeFrequency::DAILY));
37 80
        $this->setPriority(Arr::get($attributes, 'priority', 0.8));
38 80
        $this->setTitle(Arr::get($attributes, 'title'));
39 80
    }
40
41
    /* -----------------------------------------------------------------
42
     |  Getters & Setters
43
     | -----------------------------------------------------------------
44
     */
45
46
    /**
47
     * Get the url location.
48
     *
49
     * @return string
50
     */
51 54
    public function getLoc()
52
    {
53 54
        return $this->escape($this->get('loc'));
54
    }
55
56
    /**
57
     * Set the url location.
58
     *
59
     * @param  string  $loc
60
     *
61
     * @return self
62
     *
63
     * @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException
64
     */
65 80
    public function setLoc($loc)
66
    {
67 80
        return $this->set('loc', $this->checkLoc($loc));
68
    }
69
70
    /**
71
     * Get the last modification date.
72
     *
73
     * @return \DateTimeInterface
74
     */
75 38
    public function getLastMod()
76
    {
77 38
        return $this->get('lastmod');
78
    }
79
80
    /**
81
     * Format the url last modification.
82
     *
83
     * @param  string  $format
84
     *
85
     * @return string
86
     */
87 32
    public function formatLastMod($format = DateTime::ATOM)
88
    {
89 32
        return $this->getLastMod()->format($format);
90
    }
91
92
    /**
93
     * Set the last modification date.
94
     *
95
     * @param  string|\DateTimeInterface  $lastModDate
96
     * @param  string                     $format
97
     *
98
     * @return self
99
     */
100 80
    public function setLastMod($lastModDate, $format = 'Y-m-d H:i:s')
101
    {
102 80
        if (is_string($lastModDate))
103 52
            $lastModDate = DateTime::createFromFormat($format, $lastModDate);
104
105 80
        return $this->set('lastmod', $lastModDate);
106
    }
107
108
    /**
109
     * Get the change frequency.
110
     *
111
     * @return string
112
     */
113 8
    public function getChangeFreq()
114
    {
115 8
        return $this->get('changefreq');
116
    }
117
118
    /**
119
     * Set the change frequency.
120
     *
121
     * @param  string  $changeFreq
122
     *
123
     * @return self
124
     */
125 80
    public function setChangeFreq($changeFreq)
126
    {
127 80
        return $this->set('changefreq', strtolower(trim($changeFreq)));
128
    }
129
130
    /**
131
     * Get the priority.
132
     *
133
     * @return float
134
     */
135 8
    public function getPriority()
136
    {
137 8
        return $this->get('priority');
138
    }
139
140
    /**
141
     * Set the priority.
142
     *
143
     * @param  float  $priority
144
     *
145
     * @return self
146
     *
147
     * @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException
148
     */
149 80
    public function setPriority($priority)
150
    {
151 80
        $priority = $this->checkPriority($priority);
152
153 80
        return $this->set('priority', $priority);
154
    }
155
156
    /**
157
     * Get the title.
158
     *
159
     * @return string|null
160
     */
161 10
    public function getTitle()
162
    {
163 10
        return $this->escape($this->get('title'));
164
    }
165
166
    /**
167
     * Get the title.
168
     *
169
     * @param  string  $title
170
     *
171
     * @return self
172
     */
173 80
    public function setTitle($title)
174
    {
175 80
        return $this->set('title', $title);
176
    }
177
178
    /* -----------------------------------------------------------------
179
     |  Main Methods
180
     | -----------------------------------------------------------------
181
     */
182
183
    /**
184
     * Create a sitemap url instance.
185
     *
186
     * @param  string  $loc
187
     *
188
     * @return \Arcanedev\LaravelSitemap\Entities\Url
189
     */
190 60
    public static function make($loc)
191
    {
192 60
        return new static(compact('loc'));
193
    }
194
195
    /**
196
     * Make a URL instance with attributes.
197
     *
198
     * @param  array  $attributes
199
     *
200
     * @return \Arcanedev\LaravelSitemap\Entities\Url
201
     */
202 6
    public static function makeFromArray(array $attributes)
203
    {
204 6
        return new static($attributes);
205
    }
206
207
    /**
208
     * Check if has an attribute.
209
     *
210
     * @param  string  $key
211
     *
212
     * @return bool
213
     */
214 12
    public function has($key)
215
    {
216 12
        return ! is_null($this->get($key));
217
    }
218
219
    /**
220
     * Convert the Fluent instance to an array.
221
     *
222
     * @return array
223
     */
224 20
    public function toArray()
225
    {
226 20
        return array_merge(parent::toArray(), [
227 20
            'lastmod' => $this->formatLastMod(),
228
        ]);
229
    }
230
231
    /* -----------------------------------------------------------------
232
     |  Other Methods
233
     | -----------------------------------------------------------------
234
     */
235
236
    /**
237
     * Set an attribute.
238
     *
239
     * @param  string  $key
240
     * @param  mixed   $value
241
     *
242
     * @return self
243
     */
244 80
    protected function set($key, $value)
245
    {
246 80
        $this->attributes[$key] = $value;
247
248 80
        return $this;
249
    }
250
251
    /**
252
     * Escape the given value.
253
     *
254
     * @param  string  $value
255
     *
256
     * @return string
257
     */
258 56
    protected function escape($value)
259
    {
260 56
        if (is_null($value))
261 2
            return $value;
262
263 56
        return config('sitemap.escaping', true)
264 56
            ? htmlentities($value, ENT_XML1, 'UTF-8')
265 56
            : $value;
266
    }
267
268
    /**
269
     * Check the loc value.
270
     *
271
     * @param  string  $loc
272
     *
273
     * @return string
274
     *
275
     * @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException
276
     */
277 80
    protected function checkLoc($loc)
278
    {
279 80
        if (is_null($loc) || ! is_string($loc))
280 4
            throw new SitemapException('The [loc] attribute is required and must be string value.');
281
282 80
        return $loc;
283
    }
284
285
    /**
286
     * Check the priority value.
287
     *
288
     * @param  float  $priority
289
     *
290
     * @return float
291
     *
292
     * @throws \Arcanedev\LaravelSitemap\Exceptions\SitemapException
293
     */
294 80
    protected function checkPriority($priority)
295
    {
296 80
        if ( ! is_numeric($priority))
297 2
            throw new SitemapException("The [priority] value must be numeric.");
298
299 80
        $priority = round($priority, 1);
300
301 80
        if ($priority > 1 || $priority < 0)
302 2
            throw new SitemapException("The [priority] value must be between `0.0` and `1.0`, `{$priority}` was given.");
303
304 80
        return $priority;
305
    }
306
}
307