Url::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
ccs 9
cts 9
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Wszetko Sitemap.
7
 *
8
 * (c) Paweł Kłopotek-Główczewski <[email protected]>
9
 *
10
 * This source file is subject to the MIT license that is bundled
11
 * with this source code in the file LICENSE.
12
 */
13
14
namespace Wszetko\Sitemap\Items;
15
16
use Wszetko\Sitemap\Sitemap;
17
18
/**
19
 * Class Url.
20
 *
21
 * @package Wszetko\Sitemap\Items
22
 *
23
 * @method setLoc($loc): static
24
 * @method getLoc()
25
 * @method setPriority($priority): static
26
 * @method getPriority()
27
 * @method setChangefreq($changeFreq): static
28
 * @method getChangefreq()
29
 * @method setLastmod($lastMod): static
30
 * @method getLastmod()
31
 */
32
class Url extends AbstractItem
33
{
34
    /**
35
     * Element name.
36
     */
37
    public const ELEMENT_NAME = 'url';
38
39
    /**
40
     * Location (URL).
41
     *
42
     * @var \Wszetko\Sitemap\Items\DataTypes\URLType
43
     */
44
    protected $loc;
45
46
    /**
47
     * Last modified time.
48
     *
49
     * @var \Wszetko\Sitemap\Items\DataTypes\DateTimeType
50
     */
51
    protected $lastmod;
52
53
    /**
54
     * Change frequency of the location.
55
     *
56
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
57
     */
58
    protected $changefreq;
59
60
    /**
61
     * Priority of page importance.
62
     *
63
     * @var \Wszetko\Sitemap\Items\DataTypes\FloatType
64
     */
65
    protected $priority;
66
67
    /**
68
     * Array of used extensions.
69
     *
70
     * @var array
71
     */
72
    private $extensions = [];
73
74
    /**
75
     * Url constructor.
76
     *
77
     * @param string $loc
78
     *
79
     * @throws \ReflectionException
80
     */
81 90
    public function __construct(string $loc)
82
    {
83 90
        parent::__construct();
84
85 90
        $this->loc->setRequired(true);
86 90
        $this->setLoc($loc);
87 90
        $this->changefreq->setAllowedValues(Sitemap::CHANGEFREQ);
88 90
        $this->priority
89 90
            ->setMinValue(0)
90 90
            ->setMaxValue(1)
91 90
            ->setPrecision(1)
92
        ;
93 90
    }
94
95
    /**
96
     * @return array
97
     */
98 14
    public function toArray(): array
99
    {
100 14
        $array = parent::toArray();
101
102 14
        foreach ($this->getExtensions() as $extension => $data) {
103 6
            foreach ($data as $ext) {
104 6
                $ext->setDomain($this->getDomain());
105 6
                $array['url'][$extension][] = $ext->toArray();
106
            }
107
        }
108
109 14
        return $array;
110
    }
111
112
    /**
113
     * @return array
114
     */
115 16
    public function getExtensions(): array
116
    {
117 16
        return $this->extensions;
118
    }
119
120
    /**
121
     * @param Extension $extension
122
     *
123
     * @return self
124
     */
125 8
    public function addExtension(Extension $extension): self
126
    {
127 8
        if (!isset($this->extensions[$extension::NAMESPACE_NAME])) {
128 8
            $this->extensions[$extension::NAMESPACE_NAME] = [];
129
        }
130
131 8
        $this->extensions[$extension::NAMESPACE_NAME][] = $extension;
132
133 8
        return $this;
134
    }
135
}
136