🚧 Continues tests, parser and starts builder

For #4
This commit is contained in:
Shikiryu committed 2023-04-07 17:57:35 +02:00
1 parent fec8c122e3
commit 63d0b03b50
18 files changed
+494 -234

No files matched your search

+7 -7
View File
@@ -16,18 +16,18 @@ class Channel extends HasValidator implements SRSSElement
* @required
* @nohtml
*/
public string $title;
public string $title = '';
/**
* @required
* @url
*/
public string $link;
public string $link = '';
/**
* @required
*/
public string $description;
public string $description = '';
/**
* @lang
@@ -96,9 +96,7 @@ class Channel extends HasValidator implements SRSSElement
*/
public function isValid(): bool
{
$annotation_validation = new Validator();
return $annotation_validation->isObjectValid($this);
return (new Validator())->isObjectValid($this);
}
/**
@@ -106,6 +104,8 @@ class Channel extends HasValidator implements SRSSElement
*/
public function toArray(): array
{
return get_object_vars($this);
$vars = get_object_vars($this);
unset($vars['validated']);
return $vars;
}
}
+37 -4
View File
@@ -2,14 +2,47 @@
namespace Shikiryu\SRSS\Entity\Channel;
class Image
use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator;
class Image extends HasValidator implements SRSSElement
{
/**
* @required
* @url
*/
public string $url;
/**
* @required
* @nohtml
*/
public string $title;
/**
* @required
* @url
*/
public string $link;
public int $width; // Maximum value for width is 144, default value is 88.
public int $height; //Maximum value for height is 400, default value is 31.
/**
* @int
* @max 144
*/
public int $width = 88; // Maximum value for width is 144, default value is 88.
/**
* @int
* @max 400
*/
public int $height = 31; //Maximum value for height is 400, default value is 31.
public string $description;
public array $required = ['url', 'title', 'link'];
public function isValid(): bool
{
return (new Validator())->isObjectValid($this);
}
public function toArray(): array
{
return get_object_vars($this);
}
}
+50 -15
View File
@@ -2,33 +2,68 @@
namespace Shikiryu\SRSS\Entity;
class Item implements SRSSElement
use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator;
/**
* https://cyber.harvard.edu/rss/rss.html#hrelementsOfLtitemgt
*/
class Item extends HasValidator implements SRSSElement
{
public ?string $title;
public ?string $link;
public ?string $description;
public ?string $author;
public ?string $category;
public ?string $comments;
public ?string $enclosure;
public ?string $guid;
public ?string $pubDate;
public ?string $source;
/**
* @requiredOr description
* @nohtml
*/
public ?string $title = null;
/**
* @url
*/
public ?string $link = null;
/**
* @requiredOr title
*/
public ?string $description = null;
/**
* @email
*/
public ?string $author = null;
/*
* TODO can be multiple with attributes and all
*/
public ?string $category = null;
/**
* @url
*/
public ?string $comments = null;
/*
* TODO 1 attributes and 1 value
*/
public ?string $enclosure = null;
public ?string $guid = null;
/**
* @date
*/
public ?string $pubDate = null;
/*
* TODO 1 attributes and 1 value
*/
public ?string $source = null;
/**
* @var \Shikiryu\SRSS\Entity\Media\Content[]
* @contentMedia
*/
public array $medias = [];
public array $required = ['description'];
public function isValid(): bool
{
return count(array_filter($this->required, fn($field) => !empty($this->{$field}))) === 0;
return (new Validator())->isObjectValid($this);
}
public function toArray(): array
{
return get_object_vars($this);
$vars = get_object_vars($this);
unset($vars['validated']);
return $vars;
}
}
+63 -110
View File
@@ -2,123 +2,76 @@
namespace Shikiryu\SRSS\Entity\Media;
use DOMDocument;
use DOMElement;
use DOMNode;
use Shikiryu\SRSS\SRSSException;
use Shikiryu\SRSS\SRSSTools;
use Shikiryu\SRSS\Entity\SRSSElement;
use Shikiryu\SRSS\Validator\HasValidator;
use Shikiryu\SRSS\Validator\Validator;
class Content extends DomDocument
class Content extends HasValidator implements SRSSElement
{
protected array $possibilities = [
'url' => 'link',
'fileSize' => 'int', // TODO
'type' => 'media_type', // TODO
'medium' => 'media_medium', // TODO
'isDefault' => 'bool', // TODO
'expression' => 'medium_expression', // TODO
'bitrate' => 'int',
'framerate' => 'int',
'samplingrate' => 'float',
'channels' => 'int',
'duration' => 'int',
'height' => 'int',
'width' => 'int',
'lang' => '',
];
private array $attr = [];
private DOMNode $node;
/**
* Constructor
*
* @param DomNode $node
* @url
*/
public function __construct(?\DOMNode $node = null)
public ?string $url = null;
/**
* @int
*/
public ?int $filesize = null;
/**
* @mediaType
*/
public ?string $type = null;
/**
* @mediaMedium
*/
public ?string $medium = null;
/**
* @bool
*/
public ?bool $isDefault = null;
/**
* @mediaExpression
*/
public ?string $expression = null;
/**
* @int
*/
public ?int $bitrate = null;
/**
* @int
*/
public ?int $framerate = null;
/**
* @float
*/
public ?float $samplerate = null;
/**
* @int
*/
public ?int $channels = null;
/**
* @int
*/
public ?int $duration = null;
/**
* @int
*/
public ?int $height = null;
/**
* @int
*/
public ?int $width = null;
/**
* @lang
*/
public ?string $lang = null;
public function isValid(): bool
{
parent::__construct();
if ($node instanceof DOMElement) {
$this->node = $this->importNode($node, true);
} else {
$this->node = $this->importNode(new DomElement('item'));
}
$this->_loadAttributes();
return (new Validator())->isObjectValid($this);
}
/**
* @return void
*/
private function _loadAttributes(): void
public function toArray(): array
{
foreach ($this->node->attributes as $attributes) {
if (array_key_exists($attributes->name, $this->possibilities)) {
$this->{$attributes->name} = $attributes->value;
}
}
}
/**
* main getter for properties
*
* @param $name
*
* @return null|string
* @throws SRSSException
*/
public function __get($name)
{
if (array_key_exists($name, $this->attr)) {
return $this->attr[$name];
}
if (array_key_exists($name, $this->possibilities)) {
$tmp = $this->node->getElementsByTagName($name);
if ($tmp->length !== 1) {
return null;
}
return $tmp->item(0)->nodeValue;
}
throw new SRSSException(sprintf('%s is not a possible item (%s)', $name, implode(', ', array_keys($this->possibilities))));
}
/**
* @param $name
*
* @return bool
*/
public function __isset($name)
{
return isset($this->attr[$name]);
}
/**
* main setter for properties
*
* @param $name
* @param $val
*
* @throws SRSSException
*/
public function __set($name, $val)
{
if (!array_key_exists($name, $this->possibilities)) {
throw new SRSSException(sprintf('%s is not a possible item (%s)', $name, implode(', ', array_keys($this->possibilities))));
}
$flag = $this->possibilities[$name];
if ($flag !== '') {
$val = SRSSTools::check($val, $flag);
}
if (!empty($val)) {
if ($this->$name === null) {
$this->node->appendChild(new DomElement($name, $val));
}
$this->attr[$name] = $val;
}
return get_object_vars($this);
}
}
-8
View File
@@ -1,8 +0,0 @@
<?php
namespace Shikiryu\SRSS\Entity\Media;
class Group
{
}