-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntry.php
More file actions
95 lines (77 loc) · 2.93 KB
/
Entry.php
File metadata and controls
95 lines (77 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
declare(strict_types = 1);
namespace AtomGenerator;
use DateTimeInterface;
use SimpleXMLElement;
use Webmozart\Assert\Assert;
class Entry extends AbstractElement
{
protected ?string $summary = null;
protected ?string $summaryType = null;
protected ?string $content = null;
protected ?string $contentType = null;
protected ?string $contentSrc = null;
protected ?DateTimeInterface $publishedDateTime = null;
protected ?Feed $source = null;
public function setSummary(?string $summary, ?string $type = null): void
{
Assert::true((null !== $summary) || (null === $type));
Assert::oneOf($type, [null, 'text', 'xhtml', 'html']);
$this->summary = $summary;
$this->summaryType = $type;
}
public function setContent(?string $content, ?string $type = null, ?string $src = null): void
{
Assert::true(((null !== $content) && (null === $src))
|| ((null === $content) && (null !== $src))
|| ((null === $content) && (null === $src) && (null === $type)));
if (null !== $src) {
self::assertURL($src);
}
$this->content = $content;
$this->contentType = $type;
$this->contentSrc = $src;
}
public function setPublishedDateTime(?DateTimeInterface $publishedDateTime): void
{
$this->publishedDateTime = $publishedDateTime;
}
public function setSource(?Feed $sourceFeed): void
{
if (null !== $sourceFeed) {
Assert::count($sourceFeed->getEntries(), 0, 'Source feed must not contain entries.');
}
$this->source = $sourceFeed;
}
public function addChildrenTo(SimpleXMLElement $parent): void
{
if ((null === $this->content) && (null === $this->contentSrc)) {
$valid = false;
foreach ($this->links as $link) {
if ('alternate' === $link['rel']) {
$valid = true;
}
}
Assert::true($valid, 'Content must be provided if there is no alternate link.');
}
$entry = $parent->addChild('entry');
parent::addChildrenTo($entry);
if (null !== $this->publishedDateTime) {
$entry->addChild('published', $this->publishedDateTime->format(DATE_ATOM));
}
if (null !== $this->summary) {
self::addChildWithTypeToElement($entry, 'summary', $this->summary, $this->summaryType);
}
if (null !== $this->content) {
self::addChildWithTypeToElement($entry, 'content', $this->content, $this->contentType);
}
if (null !== $this->contentSrc) {
$content = self::addChildWithTypeToElement($entry, 'content', null, $this->contentType);
$content->addAttribute('src', $this->contentSrc);
}
if (null !== $this->source) {
$source = $entry->addChild('source');
$this->source->addChildrenTo($source);
}
}
}