mirror of
https://github.com/Chouchen/ShikiryuRSS.git
synced 2025-09-05 13:04:32 +02:00
✨ Finishes Builders
This commit is contained in:
98
src/Builder/ChannelBuilder.php
Normal file
98
src/Builder/ChannelBuilder.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Builder;
|
||||
|
||||
use DOMElement;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
|
||||
class ChannelBuilder
|
||||
{
|
||||
|
||||
private \DOMDocument $document;
|
||||
|
||||
/**
|
||||
* @param \DOMDocument $document
|
||||
*/
|
||||
public function __construct(\DOMDocument $document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Channel $channel
|
||||
*
|
||||
* @return \DOMElement|false
|
||||
* @throws \DOMException
|
||||
*/
|
||||
public function build(Channel $channel)
|
||||
{
|
||||
$node = $this->document->createElement('channel');
|
||||
foreach (array_filter($channel->toArray(), static fn($el) => !empty($el)) as $name => $value) {
|
||||
if ($name === 'category') {
|
||||
/** @var \Shikiryu\SRSS\Entity\Channel\Category $category */
|
||||
foreach ($value as $category) {
|
||||
$node->appendChild($this->buildCategory($category));
|
||||
}
|
||||
} elseif ($name === 'cloud') {
|
||||
$node->appendChild($this->buildCloud($value));
|
||||
} elseif ($name === 'image') {
|
||||
$node->appendChild($this->buildImage($value));
|
||||
} else {
|
||||
$new_node = $this->document->createElement($name, $value);
|
||||
$node->appendChild($new_node);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Channel\Category $category
|
||||
*
|
||||
* @return bool|\DOMElement
|
||||
* @throws \DOMException
|
||||
*/
|
||||
private function buildCategory(Channel\Category $category): bool|DOMElement
|
||||
{
|
||||
$node = $this->document->createElement('category', $category->value);
|
||||
$node->setAttribute('domain', $category->domain);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Channel\Cloud $cloud
|
||||
*
|
||||
* @return \DOMElement|false
|
||||
* @throws \DOMException
|
||||
*/
|
||||
private function buildCloud(Channel\Cloud $cloud)
|
||||
{
|
||||
$node = $this->document->createElement('cloud');
|
||||
foreach (get_object_vars($cloud) as $name => $value) {
|
||||
if (!is_array($value)) {
|
||||
$node->setAttribute($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Channel\Image $image
|
||||
*
|
||||
* @return \DOMElement|false
|
||||
* @throws \DOMException
|
||||
*/
|
||||
private function buildImage(Channel\Image $image)
|
||||
{
|
||||
$node = $this->document->createElement('image');
|
||||
foreach (get_object_vars($image) as $name => $value) {
|
||||
if (!is_array($value)) {
|
||||
$node->appendChild($this->document->createElement($name, $value));
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
105
src/Builder/ItemBuilder.php
Normal file
105
src/Builder/ItemBuilder.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace Shikiryu\SRSS\Builder;
|
||||
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Entity\Media\Content;
|
||||
|
||||
class ItemBuilder
|
||||
{
|
||||
private \DOMDocument $document;
|
||||
|
||||
/**
|
||||
* @param \DOMDocument $document
|
||||
*/
|
||||
public function __construct(\DOMDocument $document)
|
||||
{
|
||||
$this->document = $document;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Shikiryu\SRSS\Entity\Item $item
|
||||
*
|
||||
* @return \DOMElement|false
|
||||
* @throws \DOMException
|
||||
*/
|
||||
public function build(Item $item): bool|\DOMElement
|
||||
{
|
||||
$node = $this->document->createElement('item');
|
||||
|
||||
foreach (array_filter($item->toArray()) as $name => $value) {
|
||||
if ($name === 'category') {
|
||||
/** @var \Shikiryu\SRSS\Entity\Item\Category $category */
|
||||
foreach ($value as $category) {
|
||||
$node->appendChild($this->buildCategory($category));
|
||||
}
|
||||
} elseif ($name === 'medias') {
|
||||
$group = null;
|
||||
if (count($value) > 1) {
|
||||
$group = $node->appendChild($this->document->createElement('media:group'));
|
||||
}
|
||||
foreach ($value as $media) {
|
||||
if (null === $group) {
|
||||
$node->appendChild($this->buildMedia($media));
|
||||
} else {
|
||||
$group->appendChild($this->buildMedia($media));
|
||||
}
|
||||
}
|
||||
if ($group !== null) {
|
||||
$node->appendChild($group);
|
||||
}
|
||||
} elseif ($name === 'enclosure') {
|
||||
$node->appendChild($this->buildEnclosure($value));
|
||||
} elseif ($name === 'source') {
|
||||
$node->appendChild($this->buildSource($value));
|
||||
} else {
|
||||
$new_node = $this->document->createElement($name, $value);
|
||||
$node->appendChild($new_node);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function buildCategory(Item\Category $category)
|
||||
{
|
||||
$node = $this->document->createElement('category', $category->value);
|
||||
$node->setAttribute('domain', $category->domain);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function buildEnclosure(Item\Enclosure $enclosure)
|
||||
{
|
||||
$node = $this->document->createElement('enclosure');
|
||||
foreach (get_object_vars($enclosure) as $name => $value) {
|
||||
if (!is_array($value)) {
|
||||
$node->setAttribute($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function buildSource(Item\Source $source)
|
||||
{
|
||||
$node = $this->document->createElement('source', $source->value);
|
||||
$node->setAttribute('url', $source->url);
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
private function buildMedia(Content $media)
|
||||
{
|
||||
$node = $this->document->createElement('media:content');
|
||||
foreach (get_object_vars($media) as $name => $value) {
|
||||
if (!is_array($value)) {
|
||||
$node->setAttribute($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $node;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -3,9 +3,6 @@
|
||||
namespace Shikiryu\SRSS\Builder;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use Shikiryu\SRSS\Entity\Channel;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\DOMBuilderException;
|
||||
use Shikiryu\SRSS\SRSS;
|
||||
|
||||
@@ -20,13 +17,16 @@ class SRSSBuilder extends DomDocument
|
||||
$root = $this->createElement('rss');
|
||||
|
||||
$root->setAttribute('version', '2.0');
|
||||
$channel = $this->createElement('channel');
|
||||
|
||||
$srss->channel->generator = 'Shikiryu RSS';
|
||||
|
||||
$this->appendChannelToDom($srss->channel, $channel);
|
||||
$channel_builder = new ChannelBuilder($this);
|
||||
$channel = $channel_builder->build($srss->channel);
|
||||
|
||||
$this->appendItemsToDom($srss->items, $channel);
|
||||
$item_builder = new ItemBuilder($this);
|
||||
foreach ($srss->items as $item) {
|
||||
$channel->appendChild($item_builder->build($item));
|
||||
}
|
||||
|
||||
$root->appendChild($channel);
|
||||
$this->appendChild($root);
|
||||
@@ -62,29 +62,4 @@ class SRSSBuilder extends DomDocument
|
||||
->buildRSS($srss)
|
||||
->saveXml();
|
||||
}
|
||||
|
||||
private function appendChannelToDom(Channel $channel, DOMElement $node): void
|
||||
{
|
||||
foreach (array_filter($channel->toArray(), static fn($el) => !empty($el)) as $name => $value) {
|
||||
$new_node = $this->createElement($name, $value);
|
||||
$node->appendChild($new_node);
|
||||
}
|
||||
}
|
||||
|
||||
private function appendItemsToDom(array $items, DOMElement $channel): void
|
||||
{
|
||||
foreach ($items as $item) {
|
||||
$this->appendItemToDom($item, $channel);
|
||||
}
|
||||
}
|
||||
|
||||
private function appendItemToDom(Item $item, DOMElement $channel): void
|
||||
{
|
||||
$itemNode = $this->createElement('item');
|
||||
foreach (array_filter($item->toArray()) as $name => $value) {
|
||||
$new_node = $this->createElement($name, $value);
|
||||
$itemNode->appendChild($new_node);
|
||||
}
|
||||
$channel->appendChild($itemNode);
|
||||
}
|
||||
}
|
@@ -126,7 +126,11 @@ class SRSS implements Iterator
|
||||
throw new PropertyNotFoundException(Channel::class, $name);
|
||||
}
|
||||
if ((new Validator())->isValidValueForObjectProperty($this->channel, $name, $val)) {
|
||||
$this->channel->{$name} = $val;
|
||||
if (SRSSTools::getPropertyType(Channel::class, $name) === 'array') {
|
||||
$this->channel->{$name}[] = $val;
|
||||
} else {
|
||||
$this->channel->{$name} = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -4,9 +4,15 @@ namespace Shikiryu\SRSS;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use ReflectionProperty;
|
||||
|
||||
class SRSSTools
|
||||
{
|
||||
public static function getPropertyType($object, $property): ?string
|
||||
{
|
||||
$rp = new ReflectionProperty($object, $property);
|
||||
return $rp->getType()?->getName();
|
||||
}
|
||||
public static function check($check, $flag)
|
||||
{
|
||||
return match ($flag) {
|
||||
|
@@ -272,4 +272,9 @@ class Validator
|
||||
{
|
||||
return in_array($value, ['sample', 'full', 'nonstop']);
|
||||
}
|
||||
|
||||
private function _validateEmail($value): bool
|
||||
{
|
||||
return filter_var($value, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user