2020-06-26 14:34:52 +00:00
|
|
|
<?php
|
2020-08-04 11:25:22 +00:00
|
|
|
|
2021-06-08 09:52:11 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-06-26 14:34:52 +00:00
|
|
|
/**
|
2022-02-19 16:06:11 +00:00
|
|
|
* @copyright 2021 Ad Aures
|
2020-06-26 14:34:52 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
|
|
|
|
* @link https://castopod.org/
|
|
|
|
*/
|
|
|
|
|
2020-08-04 11:25:22 +00:00
|
|
|
namespace App\Libraries;
|
|
|
|
|
2021-08-11 15:47:23 +00:00
|
|
|
use DOMDocument;
|
2020-08-04 11:25:22 +00:00
|
|
|
use SimpleXMLElement;
|
|
|
|
|
2020-06-26 14:34:52 +00:00
|
|
|
class SimpleRSSElement extends SimpleXMLElement
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Adds a child with $value inside CDATA
|
2020-10-12 19:21:50 +00:00
|
|
|
*
|
|
|
|
* @param string $name — The name of the child element to add.
|
2021-05-12 14:00:25 +00:00
|
|
|
* @param string $value — [optional] If specified, the value of the child element.
|
2021-05-06 14:00:48 +00:00
|
|
|
* @param string|null $namespace [optional] If specified, the namespace to which the child element belongs.
|
|
|
|
*
|
2021-05-12 14:00:25 +00:00
|
|
|
* @return static The addChild method returns a SimpleXMLElement object representing the child added to the XML node.
|
2020-06-26 14:34:52 +00:00
|
|
|
*/
|
2022-03-04 14:33:48 +00:00
|
|
|
public function addChildWithCDATA(string $name, string $value = '', ?string $namespace = null): static
|
2021-05-19 16:35:13 +00:00
|
|
|
{
|
2022-03-23 09:48:22 +00:00
|
|
|
$newChild = parent::addChild($name, null, $namespace);
|
2022-09-28 14:00:05 +00:00
|
|
|
$node = dom_import_simplexml($newChild);
|
|
|
|
$no = $node->ownerDocument;
|
2023-04-14 11:11:53 +00:00
|
|
|
if ($no instanceof DOMDocument) {
|
2022-09-28 14:00:05 +00:00
|
|
|
$node->appendChild($no->createCDATASection($value));
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
return $newChild;
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|
2020-10-12 19:21:50 +00:00
|
|
|
|
|
|
|
/**
|
2021-05-19 16:35:13 +00:00
|
|
|
* Adds a child element to the XML node with escaped $value if specified. Override of addChild method as
|
|
|
|
* SimpleXMLElement's addChild method doesn't escape ampersand
|
2020-10-12 19:21:50 +00:00
|
|
|
*
|
|
|
|
* @param string $name — The name of the child element to add.
|
|
|
|
* @param string $value — [optional] If specified, the value of the child element.
|
|
|
|
* @param string $namespace [optional] If specified, the namespace to which the child element belongs.
|
2021-07-29 10:46:51 +00:00
|
|
|
* @param boolean $escape [optional] The value is escaped by default, can be set to false.
|
2021-05-12 14:00:25 +00:00
|
|
|
*
|
|
|
|
* @return static The addChild method returns a SimpleXMLElement object representing the child added to the XML node.
|
2020-10-12 19:21:50 +00:00
|
|
|
*/
|
2022-03-04 14:33:48 +00:00
|
|
|
public function addChild($name, $value = null, $namespace = null, $escape = true): static
|
2020-10-12 19:21:50 +00:00
|
|
|
{
|
2022-03-23 09:48:22 +00:00
|
|
|
$newChild = parent::addChild($name, null, $namespace);
|
2022-09-28 14:00:05 +00:00
|
|
|
$node = dom_import_simplexml($newChild);
|
|
|
|
$no = $node->ownerDocument;
|
|
|
|
$value = $escape ? esc($value ?? '') : $value ?? '';
|
|
|
|
if (! $no instanceof DOMDocument) {
|
|
|
|
return $newChild;
|
|
|
|
}
|
2020-10-12 19:21:50 +00:00
|
|
|
|
2022-09-28 14:00:05 +00:00
|
|
|
if (is_array($value)) {
|
|
|
|
return $newChild;
|
2020-10-12 19:21:50 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 14:00:05 +00:00
|
|
|
$node->appendChild($no->createTextNode($value));
|
|
|
|
|
2021-05-17 17:11:23 +00:00
|
|
|
return $newChild;
|
2020-10-12 19:21:50 +00:00
|
|
|
}
|
2020-06-26 14:34:52 +00:00
|
|
|
}
|