Skip to content

Commit

Permalink
Main image: Parse URL from description
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Nov 4, 2020
1 parent 91d3d5d commit e3e8cc2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
33 changes: 27 additions & 6 deletions src/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,17 @@ public function load(string $url): array
$feed = [];
foreach ($rss->getElementsByTagName('item') as $node) {
/** @var \DOMElement $node */
$feed[] = new Post(

$description = $this->hydrateDescription($this->hydrateValueToString($node, 'description'));
$feed[] = (new Post(
strip_tags($this->hydrateValueToString($node, 'title')),
html_entity_decode(preg_replace('/^<p>(.+?)<\/p>.*/', '$1', str_replace("\n", ' ', $this->hydrateValueToString($node, 'description')))),
(string) $description['description'],
$this->hydrateValueToString($node, 'link'),
$this->hydrateValueToDateTime($node, 'pubDate'),
$this->hydrateValueToString($node, 'creator'),
$this->hydrateValue($node, 'category')
);
$this->hydrateValueToDateTime($node, 'pubDate')
))
->setCreator($this->hydrateValueToString($node, 'creator'))
->setCategories($this->hydrateValue($node, 'category'))
->setMainImageUrl($description['mainImageUrl'] ?? null);
}

return $feed;
Expand Down Expand Up @@ -79,6 +82,24 @@ private function getContent(string $url, bool $flush = false): string
}


/**
* @return string[]|null[]
*/
private function hydrateDescription(string $description): array
{
$mainImageUrl = null;
if (preg_match('/(.*)<img\s[^>]*?src="([^"]+)"[^>]*?>(.*)/', $description, $parser)) {
$description = trim($parser[1] . ' ' . $parser[3]);
$mainImageUrl = trim($parser[2]);
}

return [
'description' => html_entity_decode(preg_replace('/^<p>(.+?)<\/p>.*/', '$1', str_replace("\n", ' ', trim($description)))),
'mainImageUrl' => $mainImageUrl,
];
}


/**
* @return string[]
*/
Expand Down
45 changes: 39 additions & 6 deletions src/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ class Post

private \DateTimeImmutable $date;

private string $creator;
private ?string $creator = null;

/** @var string[] */
private array $categories;
private array $categories = [];

private ?string $mainImageUrl = null;


/**
* @param string[] $categories
*/
public function __construct(string $title, string $description, string $link, \DateTimeImmutable $date, string $creator, array $categories)
public function __construct(string $title, string $description, string $link, \DateTimeImmutable $date)
{
$this->title = $title;
$this->description = $description;
$this->link = $link;
$this->date = $date;
$this->creator = $creator;
$this->categories = $categories;
}


Expand Down Expand Up @@ -59,17 +59,50 @@ public function getDate(): \DateTimeImmutable
}


public function getCreator(): string
public function getCreator(): ?string
{
return $this->creator;
}


public function setCreator(?string $creator): self
{
$this->creator = $creator;

return $this;
}


/**
* @return string[]
*/
public function getCategories(): array
{
return $this->categories;
}


/**
* @param string[] $categories
*/
public function setCategories(array $categories): self
{
$this->categories = $categories;

return $this;
}


public function getMainImageUrl(): ?string
{
return $this->mainImageUrl;
}


public function setMainImageUrl(?string $mainImageUrl): self
{
$this->mainImageUrl = $mainImageUrl;

return $this;
}
}

0 comments on commit e3e8cc2

Please sign in to comment.