Skip to content

Commit 939f7a2

Browse files
committed
Added a static facade
1 parent 3016d45 commit 939f7a2

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ foreach ($contents as $content)
5454
```
5555

5656
This example loads the html from big.html, a real page found online, and gets all the content-border classes to process. It also shows a few things you can do with a node but it is not an exhaustive list of methods that a node has avaiable.
57+
58+
Alternativly, you can always use the load() method to load the file. It will attempt to find the file using file_exists and, if succesfull, will call loadFromFile() for you. The same applies to a URL and loadFromUrl() method.
59+
60+
Static Facade
61+
------------
62+
63+
You can also mount a static facade for the Dom object.
64+
65+
```PHP
66+
PHPHtmlParser\StaticDom::mount();
67+
68+
Dom::load('tests/big.hmtl');
69+
$objects Dom::find('.content-border');
70+
71+
```
72+
73+
The above php block does the same find and load as the first example but it is done using the static facade, which supports all public methods found in the Dom object.

src/PHPHtmlParser/StaticDom.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
namespace PHPHtmlParser;
3+
4+
final class StaticDom {
5+
6+
private static $dom = null;
7+
8+
public static function __callStatic($method, $arguments)
9+
{
10+
if (self::$dom instanceof Dom)
11+
{
12+
return call_user_func_array([self::$dom, $method], $arguments);
13+
}
14+
else
15+
{
16+
throw new Exception('The dom is not loaded. Can not call a dom method.');
17+
}
18+
}
19+
20+
public static function mount($className = 'Dom', Dom $dom = null)
21+
{
22+
if (class_exists($className))
23+
{
24+
return false;
25+
}
26+
class_alias(__CLASS__, $className);
27+
if ($dom instanceof Dom)
28+
{
29+
self::$dom = $dom;
30+
}
31+
return true;
32+
}
33+
34+
public static function load($str)
35+
{
36+
$dom = new Dom;
37+
self::$dom = $dom;
38+
return $dom->load($str);
39+
}
40+
41+
public static function loadFromFile($file)
42+
{
43+
$dom = new Dom;
44+
return $dom->loadFromFile($file);
45+
}
46+
47+
public static function loadFromUrl($url)
48+
{
49+
$dom = new Dom;
50+
self::$dom = $dom;
51+
return $dom->loadFromUrl($url);
52+
}
53+
}

tests/StaticDomTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use PHPHtmlParser\StaticDom;
4+
5+
class StaticDomTest extends PHPUnit_Framework_TestCase {
6+
7+
public function setUp()
8+
{
9+
StaticDom::mount();
10+
}
11+
12+
public function testLoad()
13+
{
14+
$dom = Dom::load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
15+
$div = $dom->find('div', 0);
16+
$this->assertEquals('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>', $div->outerHtml);
17+
}
18+
19+
public function testLoadWithFile()
20+
{
21+
$dom = Dom::load('tests/small.html');
22+
$this->assertEquals('VonBurgermeister', $dom->find('.post-user font', 0)->text);
23+
}
24+
25+
public function testFind()
26+
{
27+
Dom::load('tests/small.html');
28+
$this->assertEquals('VonBurgermeister', Dom::find('.post-user font', 0)->text);
29+
}
30+
}

0 commit comments

Comments
 (0)