News::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 4
Bugs 1 Features 0
Metric Value
cc 1
eloc 17
c 4
b 1
f 0
nc 1
nop 4
dl 0
loc 27
ccs 18
cts 18
cp 1
crap 1
rs 9.7
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 DateTimeInterface;
17
18
/**
19
 * Class News.
20
 *
21
 * @package Wszetko\Sitemap\Items
22
 *
23
 * @method setPublicationDate($publicationDate)
24
 * @method getPublicationDate()
25
 * @method setPublicationName($publicationName)
26
 * @method getPublicationName()
27
 * @method setPublicationLanguage($publicationLanguage)
28
 * @method getPublicationLanguage()
29
 * @method setAccess($access)
30
 * @method getAccess()
31
 * @method setTitle($title)
32
 * @method getTitle()
33
 * @method setGenres($genres)
34
 * @method addGenres($genres)
35
 * @method getGenres()
36
 * @method setKeywords($keywords)
37
 * @method addKeywords($keywords)
38
 * @method getKeywords()
39
 * @method setStockTickers($stockTickers)
40
 * @method addStockTickers($stockTickers)
41
 * @method getStockTickers()
42
 */
43
class News extends Extension
44
{
45
    /**
46
     * Name of Namescapce.
47
     */
48
    public const NAMESPACE_NAME = 'news';
49
50
    /**
51
     * Namespace URL.
52
     */
53
    public const NAMESPACE_URL = 'http://www.google.com/schemas/sitemap-news/0.9';
54
55
    /**
56
     * Element name.
57
     */
58
    public const ELEMENT_NAME = 'news';
59
60
    /**
61
     * Publication name.
62
     *
63
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
64
     */
65
    protected $publicationName;
66
67
    /**
68
     * Publication language.
69
     *
70
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
71
     */
72
    protected $publicationLanguage;
73
74
    /**
75
     * Access.
76
     *
77
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
78
     */
79
    protected $access;
80
81
    /**
82
     * List of genres, comma-separated string values.
83
     *
84
     * @dataType \Wszetko\Sitemap\Items\DataTypes\StringType
85
     *
86
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
87
     */
88
    protected $genres;
89
90
    /**
91
     * Date of publication.
92
     *
93
     * @var \Wszetko\Sitemap\Items\DataTypes\DateTimeType
94
     */
95
    protected $publicationDate;
96
97
    /**
98
     * Title.
99
     *
100
     * @var \Wszetko\Sitemap\Items\DataTypes\StringType
101
     */
102
    protected $title;
103
104
    /**
105
     * Key words, comma-separated string values.
106
     *
107
     * @dataType \Wszetko\Sitemap\Items\DataTypes\StringType
108
     *
109
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
110
     */
111
    protected $keywords;
112
113
    /**
114
     * Key words, comma-separated string values.
115
     *
116
     * @dataType \Wszetko\Sitemap\Items\DataTypes\StringType
117
     *
118
     * @var \Wszetko\Sitemap\Items\DataTypes\ArrayType
119
     */
120
    protected $stockTickers;
121
122
    /**
123
     * News constructor.
124
     *
125
     * @param string                   $publicationName
126
     * @param string                   $publicationLanguage
127
     * @param DateTimeInterface|string $publicationDate
128
     * @param string                   $title
129
     *
130
     * @throws \ReflectionException
131
     * @throws \InvalidArgumentException
132
     * @throws \Error
133
     */
134 60
    public function __construct(
135
        string $publicationName,
136
        string $publicationLanguage,
137
        $publicationDate,
138
        string $title
139
    ) {
140 60
        parent::__construct();
141
142 60
        $this->publicationName->setRequired(true);
143 60
        $this->setPublicationName($publicationName);
144 58
        $this->publicationLanguage
145 58
            ->setConversion('lower')
146 58
            ->setValueRegex("/^(zh-cn|zh-tw|([a-z]{2,3}))?$/")
147 58
            ->setRequired(true)
148
        ;
149 58
        $this->publicationDate->setRequired(true);
150 58
        $this->setPublicationLanguage($publicationLanguage);
151 54
        $this->setPublicationDate($publicationDate);
152 50
        $this->setTitle($title);
153 50
        $this->access->setAllowedValues('Subscription, Registration');
154
        /** @var \Wszetko\Sitemap\Items\DataTypes\StringType $generesValue */
155 50
        $generesValue = $this->genres->getBaseDataType();
156 50
        $generesValue->setAllowedValues('PressRelease, Satire, Blog, OpEd, Opinion, UserGenerated');
157 50
        $this->stockTickers->setMaxElements(5);
158
        /** @var \Wszetko\Sitemap\Items\DataTypes\StringType $stickTickersValue */
159 50
        $stickTickersValue = $this->stockTickers->getBaseDataType();
160 50
        $stickTickersValue->setValueRegex("/^(\\w+:\\w+)?$/");
161 50
    }
162
163
    /**
164
     * @return array
165
     *
166
     * @throws \Error
167
     */
168 2
    public function toArray(): array
169
    {
170
        $array = [
171 2
            '_namespace' => static::NAMESPACE_NAME,
172 2
            '_element' => 'news',
173
            'news' => [
174
                'publication' => [
175 2
                    'name' => $this->getPublicationName(),
176 2
                    'language' => $this->getPublicationLanguage(),
177
                ],
178 2
                'publication_date' => $this->getPublicationDate(),
179 2
                'title' => $this->getTitle(),
180
            ],
181
        ];
182
183 2
        if ($this->getAccess()) {
184 2
            $array['news']['access'] = $this->getAccess();
185
        }
186
187 2
        if ($this->getGenres()) {
188 2
            $array['news']['genres'] = implode(', ', $this->getGenres());
189
        }
190
191 2
        if ($this->getKeywords()) {
192 2
            $array['news']['keywords'] = implode(',', $this->getKeywords());
193
        }
194
195 2
        if ($this->getStockTickers()) {
196 2
            $array['news']['stock_tickers'] = implode(', ', $this->getStockTickers());
197
        }
198
199 2
        return $array;
200
    }
201
}
202