1
0
mirror of https://github.com/Chouchen/ShikiryuRSS.git synced 2025-09-06 01:14:32 +02:00

🚧 Continues tests, parser and starts builder

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

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);
}
}