mirror of
https://github.com/Chouchen/ShikiryuRSS.git
synced 2024-11-21 19:38:52 +01:00
🔒 Remove dead code and another way to validate data
This commit is contained in:
parent
3419e7d46c
commit
a6a31e18e1
@ -3,12 +3,8 @@
|
||||
namespace Shikiryu\SRSS\Parser;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMException;
|
||||
use DOMNode;
|
||||
use Shikiryu\SRSS\Entity\Item;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
use Shikiryu\SRSS\SRSSTools;
|
||||
|
||||
/**
|
||||
* @property string|null $description
|
||||
@ -17,17 +13,6 @@ class ItemParser extends DomDocument
|
||||
{
|
||||
|
||||
protected DOMNode $node; // item node
|
||||
protected $attr; // item's properties
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param DomNode $node
|
||||
*/
|
||||
public function __construct($node = null)
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Item $item
|
||||
@ -84,103 +69,4 @@ class ItemParser extends DomDocument
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* getter of item DomElement
|
||||
*/
|
||||
public function getItem(): ?DOMNode
|
||||
{
|
||||
$this->appendChild($this->node);
|
||||
|
||||
return $this->getElementsByTagName('item')->item(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* check if current item is valid (following specifications)
|
||||
* @return bool
|
||||
*/
|
||||
public function isValid(): bool
|
||||
{
|
||||
return $this->description != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function __isset($name)
|
||||
{
|
||||
return isset($this->attr[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* main setter for properties
|
||||
*
|
||||
* @param $name
|
||||
* @param $val
|
||||
*
|
||||
* @throws SRSSException|DOMException
|
||||
*/
|
||||
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 ($val instanceof DOMElement) {
|
||||
$this->node->appendChild($val);
|
||||
} elseif ($this->$name === null) {
|
||||
$this->node->appendChild(new DomElement($name, $val));
|
||||
}
|
||||
$this->attr[$name] = $val;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* main getter for properties
|
||||
*
|
||||
* @param $name
|
||||
*
|
||||
* @return null|string
|
||||
* @throws SRSSException
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
if (isset($this->attr[$name])) {
|
||||
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))));
|
||||
}
|
||||
|
||||
/**
|
||||
* transform current item's object into an array
|
||||
* @return array
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$infos = [];
|
||||
foreach ($this->attr as $attrName => $attrVal) {
|
||||
$infos[$attrName] = $attrVal;
|
||||
}
|
||||
|
||||
return $infos;
|
||||
}
|
||||
}
|
@ -15,6 +15,7 @@ use Shikiryu\SRSS\Exception\PropertyNotFoundException;
|
||||
use Shikiryu\SRSS\Exception\SRSSException;
|
||||
use Shikiryu\SRSS\Exception\UnreadableRSSException;
|
||||
use Shikiryu\SRSS\Parser\SRSSParser;
|
||||
use Shikiryu\SRSS\Validator\Validator;
|
||||
|
||||
/**
|
||||
* @property null|string $title
|
||||
@ -124,10 +125,9 @@ class SRSS implements Iterator
|
||||
if (!property_exists(Channel::class, $name)) {
|
||||
throw new PropertyNotFoundException(Channel::class, $name);
|
||||
}
|
||||
// TODO add validator ?
|
||||
// if ((new Validator())->isPropertyValid($this->channel, $name)) {
|
||||
if ((new Validator())->isValidValueForObjectProperty($this->channel, $name, $val)) {
|
||||
$this->channel->{$name} = $val;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -7,7 +7,7 @@ use DateTimeInterface;
|
||||
|
||||
class SRSSTools
|
||||
{
|
||||
/*public static function check($check, $flag)
|
||||
public static function check($check, $flag)
|
||||
{
|
||||
return match ($flag) {
|
||||
'nohtml' => self::noHTML($check),
|
||||
@ -18,13 +18,12 @@ class SRSSTools
|
||||
'int' => self::checkInt($check),
|
||||
'hour' => self::checkHour($check),
|
||||
'day' => self::checkDay($check),
|
||||
'folder' => [],
|
||||
'media_type' => self::checkMediaType($check),
|
||||
'media_medium' => self::checkMediaMedium($check),
|
||||
'bool' => self::checkBool($check),
|
||||
'medium_expression' => self::checkMediumExpression($check)
|
||||
};
|
||||
}*/
|
||||
}
|
||||
|
||||
/**
|
||||
* format the RSS to the wanted format
|
||||
@ -90,11 +89,10 @@ class SRSSTools
|
||||
* @param $check string to format
|
||||
*
|
||||
* @return string formatted string
|
||||
* TODO CDATA ?
|
||||
*/
|
||||
public static function HTML4XML(string $check): string
|
||||
{
|
||||
return htmlspecialchars($check);
|
||||
return sprintf('<![CDATA[ %s ]]>', htmlspecialchars($check));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,31 +12,74 @@ use Shikiryu\SRSS\Entity\Media\Content;
|
||||
class Validator
|
||||
{
|
||||
protected ?object $object = null;
|
||||
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
* @param $property
|
||||
* @return ReflectionProperty|null
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function isPropertyValid($object, $property): bool
|
||||
private function getReflectedProperty($object, $property): ?ReflectionProperty
|
||||
{
|
||||
$properties = array_filter($this->_getClassProperties(get_class($object)), static fn($p) => $p->getName() === $property);
|
||||
$properties = array_filter(
|
||||
$this->_getClassProperties(get_class($object)),
|
||||
static fn($p) => $p->getName() === $property
|
||||
);
|
||||
|
||||
if (count($properties) !== 1) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
$properties = current($properties);
|
||||
$propertyValue = $object->{$properties->name};
|
||||
$propertyAnnotations = $this->_getPropertyAnnotations($properties);
|
||||
return current($properties);
|
||||
}
|
||||
|
||||
if (empty($propertyValue) && !in_array('required', $propertyAnnotations, true)) {
|
||||
/**
|
||||
* @param $object
|
||||
* @param $property
|
||||
* @param $value
|
||||
* @return bool
|
||||
*/
|
||||
public function isValidValueForObjectProperty($object, $property, $value): bool
|
||||
{
|
||||
try {
|
||||
$property = $this->getReflectedProperty($object, $property);
|
||||
} catch (ReflectionException) {
|
||||
return false;
|
||||
}
|
||||
$propertyAnnotations = $this->_getPropertyAnnotations($property);
|
||||
|
||||
if (empty($value) && !in_array('required', $propertyAnnotations, true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($propertyAnnotations as $propertyAnnotation) {
|
||||
$annotation = explode(' ', $propertyAnnotation);
|
||||
|
||||
$object->validated[$properties->name] = $this->_validateProperty($annotation, $propertyValue);
|
||||
$object->validated[$property->name] = $this->_validateProperty($annotation, $value);
|
||||
}
|
||||
|
||||
return false;
|
||||
return count(array_filter($object->validated, static fn($v) => ($v !== null && $v === false))) === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $object
|
||||
* @param $property
|
||||
* @return bool
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
private function objectHasProperty($object, $property): bool
|
||||
{
|
||||
return $this->getReflectedProperty($object, $property) instanceof ReflectionProperty;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function isPropertyValid($object, $property): bool
|
||||
{
|
||||
return $this->objectHasProperty($object, $property) &&
|
||||
$this->isValidValueForObjectProperty($object, $property, $object->{$property});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -65,7 +108,6 @@ class Validator
|
||||
|
||||
foreach ($properties as $property) {
|
||||
$propertyValue = $object->{$property['name']};
|
||||
// $propertyAnnotations = $this->_getPropertyAnnotations($property, get_class($object));
|
||||
|
||||
if (empty($propertyValue) && !in_array('required', $property['rules'], true)) {
|
||||
continue;
|
||||
|
@ -18,10 +18,13 @@ class BasicBuilderTest extends TestCase
|
||||
}
|
||||
public function testCreateBasicRSS(): void
|
||||
{
|
||||
$title = 'My Blog';
|
||||
$description = 'is the best';
|
||||
$link = 'http://shikiryu.com/devblog/';
|
||||
$srss = SRSS::create();
|
||||
$srss->title = 'My Blog';
|
||||
$srss->description = 'is the best';
|
||||
$srss->link = 'http://shikiryu.com/devblog/';
|
||||
$srss->title = $title;
|
||||
$srss->description = $description;
|
||||
$srss->link = $link;
|
||||
$items = [
|
||||
['title' => 'title 1', 'link' => 'http://shikiryu.com/devblog/article-1', 'pubDate' => SRSSTools::getRSSDate('2012-03-05 12:02:01'), 'description' => 'description 1'],
|
||||
['title' => 'title 2', 'link' => 'http://shikiryu.com/devblog/article-2', 'pubDate' => SRSSTools::getRSSDate('2022-03-05 22:02:02'), 'description' => 'description 2'],
|
||||
@ -39,6 +42,10 @@ class BasicBuilderTest extends TestCase
|
||||
|
||||
self::assertTrue($srss->isValid());
|
||||
|
||||
self::assertEquals($title, $srss->title);
|
||||
self::assertEquals($description, $srss->description);
|
||||
self::assertEquals($link, $srss->link);
|
||||
|
||||
$builder = new SRSSBuilder();
|
||||
$builder->build($srss, $this->saved);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user