Skip to content

Commit

Permalink
added CategoriesCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
konecnyjakub committed Nov 17, 2019
1 parent 3f3808c commit ff31687
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
24 changes: 24 additions & 0 deletions src/CategoriesCollection.php
@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);

namespace Nexendrie\Rss;

use Nexendrie\Utils\Collection as BaseCollection;

/**
* CategoriesCollection
*
* @author Jakub Konečný
* @internal
*/
final class CategoriesCollection extends BaseCollection implements IXmlConvertible {
/** @var string */
protected $class = Category::class;

public function appendToXml(\SimpleXMLElement &$parent): void {
array_walk($this->items, function(Category $value) use($parent) {
$value->appendToXml($parent);
});
}
}
?>
11 changes: 4 additions & 7 deletions src/Generator.php
Expand Up @@ -158,6 +158,9 @@ protected function configureOptions(OptionsResolver $resolver): void {
});
$resolver->setAllowedTypes("rating", "string");
$resolver->setAllowedTypes("categories", Category::class . "[]");
$resolver->setNormalizer("categories", function(Options $options, array $value) {
return CategoriesCollection::fromArray($value);
});
$resolver->setAllowedTypes("skipDays", "string[]");
$resolver->setAllowedValues("skipDays", function(array $value) {
$allowedValues = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", ];
Expand Down Expand Up @@ -194,9 +197,7 @@ public function generate(array $info): string {
$info = $resolver->resolve($info);
/** @var \SimpleXMLElement $channel */
$channel = simplexml_load_file($this->template);
$properties = array_filter($resolver->getDefinedOptions(), function(string $value) {
return !in_array($value, ["categories", ], true);
});
$properties = $resolver->getDefinedOptions();
foreach($properties as $property) {
$this->writeProperty($channel, $info, $property);
}
Expand All @@ -206,10 +207,6 @@ public function generate(array $info): string {
if($this->docs !== "") {
$channel->channel->docs = $this->docs;
}
$categories = Arrays::get($info, "categories", []);
array_walk($categories, function(Category $value) use($channel) {
$value->appendToXml($channel->channel);
});
/** @var RssChannelItem $item */
foreach($items as $item) {
/** @var \SimpleXMLElement $i */
Expand Down
13 changes: 2 additions & 11 deletions src/RssChannelItem.php
Expand Up @@ -38,7 +38,7 @@ class RssChannelItem {
protected $guid = "";
/** @var \stdClass */
protected $source;
/** @var \Nexendrie\Utils\Collection|Category[] */
/** @var CategoriesCollection|Category[] */
protected $categories;
/** @var \Nexendrie\Utils\Collection|Enclosure[] */
protected $enclosures;
Expand All @@ -48,16 +48,7 @@ public function __construct(string $title, string $description, string $link, in
$this->description = $description;
$this->link = $link;
$this->pubDate = $pubDate;
$this->categories = new class extends \Nexendrie\Utils\Collection implements IXmlConvertible {
/** @var string */
protected $class = Category::class;

public function appendToXml(\SimpleXMLElement &$parent): void {
array_walk($this->items, function(Category $value) use($parent) {
$value->appendToXml($parent);
});
}
};
$this->categories = new CategoriesCollection();
$this->enclosures = new class extends \Nexendrie\Utils\Collection implements IXmlConvertible {
/** @var string */
protected $class = Enclosure::class;
Expand Down

0 comments on commit ff31687

Please sign in to comment.