Completed
Push — master ( b09ecd...ff3168 )
by Jakub
02:47
created

RssChannelItem.php$1 ➔ getCategories()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Rss;
5
6
/**
7
 * Rss Channel Item
8
 *
9
 * @author Jakub Konečný
10
 * @property string $title
11
 * @property string $description
12
 * @property string $link
13
 * @property int $pubDate
14
 * @property string $author
15
 * @property string $comments
16
 * @property string $guid
17
 * @property string $sourceUrl
18
 * @property string $sourceTitle
19
 * @property \Nexendrie\Utils\Collection|Category[] $categories
20
 * @property \Nexendrie\Utils\Collection|Enclosure[] $enclosures
21
 */
22 1
class RssChannelItem {
23 1
  use \Nette\SmartObject;
24
  
25
  /** @var string */
26
  protected $title;
27
  /** @var string */
28
  protected $description;
29
  /** @var string */
30
  protected $link;
31
  /** @var int */
32
  protected $pubDate;
33
  /** @var string */
34
  protected $author = "";
35
  /** @var string */
36
  protected $comments = "";
37
  /** @var string */
38
  protected $guid = "";
39
  /** @var \stdClass */
40
  protected $source;
41
  /** @var CategoriesCollection|Category[] */
42
  protected $categories;
43
  /** @var \Nexendrie\Utils\Collection|Enclosure[] */
44
  protected $enclosures;
45
  
46
  public function __construct(string $title, string $description, string $link, int $pubDate) {
47 1
    $this->title = $title;
48 1
    $this->description = $description;
49 1
    $this->link = $link;
50 1
    $this->pubDate = $pubDate;
51 1
    $this->categories = new CategoriesCollection();
52 1
    $this->enclosures = new class extends \Nexendrie\Utils\Collection implements IXmlConvertible {
53
      /** @var string */
54
      protected $class = Enclosure::class;
55
56
      public function appendToXml(\SimpleXMLElement &$parent): void {
57 1
        array_walk($this->items, function(Enclosure $value) use($parent) {
58 1
          $value->appendToXml($parent);
59 1
        });
60 1
      }
61
    };
62 1
    $this->source = new class extends \stdClass implements IXmlConvertible {
63
      /** @var string */
64
      public $url = "";
65
      /** @var string */
66
      public $title = "";
67
68
      public function appendToXml(\SimpleXMLElement &$parent): void {
69 1
        if($this->url !== "") {
70 1
          $element = $parent->addChild("source", $this->title);
71 1
          $element->addAttribute("url", $this->url);
72
        }
73 1
      }
74
    };
75 1
  }
76
  
77
  public function getTitle(): string {
78 1
    return $this->title;
79
  }
80
  
81
  public function setTitle(string $title): void {
82 1
    $this->title = $title;
83 1
  }
84
  
85
  public function getDescription(): string {
86 1
    return $this->description;
87
  }
88
  
89
  public function setDescription(string $description): void {
90 1
    $this->description = $description;
91 1
  }
92
  
93
  public function getLink(): string {
94 1
    return $this->link;
95
  }
96
  
97
  public function setLink(string $link): void {
98 1
    $this->link = $link;
99 1
  }
100
  
101
  public function getPubDate(): int {
102 1
    return $this->pubDate;
103
  }
104
  
105
  public function setPubDate(int $pubDate): void {
106 1
    $this->pubDate = $pubDate;
107 1
  }
108
109
  public function getAuthor(): string {
110 1
    return $this->author;
111
  }
112
113
  public function setAuthor(string $author): void {
114 1
    $this->author = $author;
115 1
  }
116
117
  public function getComments(): string {
118 1
    return $this->comments;
119
  }
120
121
  public function setComments(string $comments): void {
122 1
    $this->comments = $comments;
123 1
  }
124
125
  public function getGuid(): string {
126 1
    return $this->guid;
127
  }
128
129
  public function setGuid(string $guid): void {
130 1
    $this->guid = $guid;
131 1
  }
132
133
  public function getSourceUrl(): string {
134 1
    return $this->source->url;
135
  }
136
137
  public function setSourceUrl(string $sourceUrl): void {
138 1
    $this->source->url = $sourceUrl;
139 1
  }
140
141
  public function getSourceTitle(): string {
142 1
    return $this->source->title;
143
  }
144
145
  public function setSourceTitle(string $sourceTitle): void {
146 1
    $this->source->title = $sourceTitle;
147 1
  }
148
149
  /**
150
   * @return \Nexendrie\Utils\Collection|Category[]
151
   */
152
  public function getCategories(): \Nexendrie\Utils\Collection {
153 1
    return $this->categories;
154
  }
155
156
  /**
157
   * @return \Nexendrie\Utils\Collection|Enclosure[]
158
   */
159
  public function getEnclosures(): \Nexendrie\Utils\Collection {
160 1
    return $this->enclosures;
161
  }
162
163
  protected function shortenDescription(string $description, int $maxLength): string {
164 1
    if($maxLength < 1) {
165 1
      return $description;
166
    }
167 1
    $originalDescription = $description;
168 1
    $description = substr($description, 0, $maxLength);
169 1
    if($description !== $originalDescription) {
170 1
      $description .= "...";
171
    }
172 1
    return $description;
173
  }
174
175
  /**
176
   * @param mixed $value
177
   * @return mixed
178
   */
179
  protected function normalizeValue(string $name, $value, Generator $generator) {
180 1
    switch($name) {
181 1
      case "pubDate":
182 1
        return date($generator->dateTimeFormat, $value);
183 1
      case "description":
184 1
        return $this->shortenDescription($value, $generator->shortenDescription);
185
      default:
186 1
        return $value;
187
    }
188
  }
189
190
  public function toXml(\SimpleXMLElement &$element, Generator $generator): void {
191 1
    $properties = get_object_vars($this);
192 1
    foreach($properties as $property => $value) {
193 1
      if($value === "") {
194 1
        continue;
195
      }
196 1
      $value = $this->normalizeValue($property, $value, $generator);
197 1
      if($value instanceof IXmlConvertible) {
198 1
        $value->appendToXml($element);
199
      } else {
200 1
        $element->addChild($property, $value);
201
      }
202
    }
203 1
  }
204
}
205
?>