123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace PHPHtmlParser;
- use PHPHtmlParser\Exceptions\NotLoadedException;
- /**
- * Class StaticDom
- *
- * @package PHPHtmlParser
- */
- final class StaticDom
- {
- private static $dom = null;
- /**
- * Attempts to call the given method on the most recent created dom
- * from bellow.
- *
- * @param string $method
- * @param array $arguments
- * @throws NotLoadedException
- * @return mixed
- */
- public static function __callStatic(string $method, array $arguments)
- {
- if (self::$dom instanceof Dom) {
- return call_user_func_array([self::$dom, $method], $arguments);
- } else {
- throw new NotLoadedException('The dom is not loaded. Can not call a dom method.');
- }
- }
- /**
- * Call this to mount the static facade. The facade allows you to use
- * this object as a $className.
- *
- * @param string $className
- * @param Dom $dom
- * @return bool
- */
- public static function mount(string $className = 'Dom', Dom $dom = null): bool
- {
- if (class_exists($className)) {
- return false;
- }
- class_alias(__CLASS__, $className);
- if ($dom instanceof Dom) {
- self::$dom = $dom;
- }
- return true;
- }
- /**
- * Creates a new dom object and calls load() on the
- * new object.
- *
- * @param string $str
- * @return Dom
- */
- public static function load(string $str): Dom
- {
- $dom = new Dom;
- self::$dom = $dom;
- return $dom->load($str);
- }
- /**
- * Creates a new dom object and calls loadFromFile() on the
- * new object.
- *
- * @param string $file
- * @return Dom
- */
- public static function loadFromFile(string $file): Dom
- {
- $dom = new Dom;
- self::$dom = $dom;
- return $dom->loadFromFile($file);
- }
- /**
- * Creates a new dom object and calls loadFromUrl() on the
- * new object.
- *
- * @param string $url
- * @param array $options
- * @param CurlInterface $curl
- * @return Dom
- */
- public static function loadFromUrl(string $url, array $options = [], CurlInterface $curl = null): Dom
- {
- $dom = new Dom;
- self::$dom = $dom;
- if (is_null($curl)) {
- // use the default curl interface
- $curl = new Curl;
- }
- return $dom->loadFromUrl($url, $options, $curl);
- }
- /**
- * Sets the $dom variable to null.
- */
- public static function unload(): void
- {
- self::$dom = null;
- }
- }
|