Skip to content

Commit

Permalink
Scrict Types
Browse files Browse the repository at this point in the history
  • Loading branch information
alphp committed Jul 21, 2024
1 parent 1099338 commit 603fe46
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/SimpleXMLExtended.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
<?php
declare(strict_types=1);

namespace Fawno\SimpleXMLExtended;

use SimpleXMLElement;
use DOMDocument;
use DOMNode;
use Exception;
use DOMException;

class SimpleXMLExtended extends SimpleXMLElement {
public static function importDOM (DOMNode $node, string $class_name = SimpleXMLExtended::class) {
public static function importDOM (DOMNode $node, string $class_name = SimpleXMLExtended::class) : ?SimpleXMLElement {
return simplexml_import_dom($node, $class_name);
}

public static function loadFile (string $filename, string $class_name = SimpleXMLExtended::class, int $options = 0, string $ns = '', bool $is_prefix = false) {
public static function loadFile (string $filename, string $class_name = SimpleXMLExtended::class, int $options = 0, string $ns = '', bool $is_prefix = false) : SimpleXMLElement|false {
return simplexml_load_file($filename, $class_name, $options, $ns, $is_prefix);
}

public static function loadXML (string $data, string $class_name = SimpleXMLExtended::class, int $options = 0, string $ns = '', bool $is_prefix = false) {
public static function loadXML (string $data, string $class_name = SimpleXMLExtended::class, int $options = 0, string $ns = '', bool $is_prefix = false) : SimpleXMLElement|false {
return simplexml_load_string($data, $class_name, $options, $ns, $is_prefix);
}

public static function loadHTML (string $data, string $class_name = SimpleXMLExtended::class, int $options = 0) {
public static function loadHTML (string $data, string $class_name = SimpleXMLExtended::class, int $options = 0) : ?SimpleXMLElement {
$node = new DOMDocument();
@$node->loadHTML($data, $options);

if (false === @$node->loadHTML($data, $options)) {
return null;
}

return simplexml_import_dom($node, $class_name);
}

private function addCData (string $cdata) {
private function addCData (string $cdata) : void {
$node = dom_import_simplexml($this);
$node->appendChild($node->ownerDocument->createCDATASection($cdata));
}

public function addChildCData (string $name, string $cdata = null, string $namespace = null) {
public function addChildCData (string $name, string $cdata = null, string $namespace = null) : ?SimpleXMLElement {
$child = $this->addChild($name, null, $namespace);

if (!empty($cdata)) {
Expand All @@ -40,15 +47,15 @@ public function addChildCData (string $name, string $cdata = null, string $names
return $child;
}

public function parentNode () :? SimpleXMLExtended {
public function parentNode () : ?SimpleXMLExtended {
return simplexml_import_dom(dom_import_simplexml($this)->parentNode, SimpleXMLExtended::class);
}

public function removeNode () :? SimpleXMLExtended {
public function removeNode () : ?SimpleXMLExtended {
return $this->parentNode()->removeChild($this);
}

public function removeChild (SimpleXMLExtended $oldnode) :? SimpleXMLExtended {
public function removeChild (SimpleXMLExtended $oldnode) : ?SimpleXMLExtended {
try {
$removed = dom_import_simplexml($this)->removeChild(dom_import_simplexml($oldnode));
} catch (Exception | DOMException $e) {
Expand All @@ -58,23 +65,26 @@ public function removeChild (SimpleXMLExtended $oldnode) :? SimpleXMLExtended {
return $oldnode;
}

public function asText (bool $strip_spaces = false) {
public function asText (bool $strip_spaces = false) : ?string {
$text = strip_tags($this->asXML());
$text = $strip_spaces ? trim(preg_replace('~\s+~', ' ', $text)) : $text;

return ($text ?: null);
}

public function formatXML (string $filename = null) {
public function formatXML (string $filename = null) : string|int|false {
$xmlDocument = new DOMDocument('1.0');
$xmlDocument->preserveWhiteSpace = false;
$xmlDocument->formatOutput = true;
$xmlDocument->loadXML($this->asXML());

if (false === $xmlDocument->loadXML($this->asXML())) {
return false;
}

if (empty($filename)) {
return $xmlDocument->saveXML();
} else {
return $xmlDocument->save($filename);
}

return $xmlDocument->save($filename);
}
}

0 comments on commit 603fe46

Please sign in to comment.