1
+ <?php
2
+ /**
3
+ * AbstractComponent.php
4
+ *
5
+ *
6
+ * @license see LICENSE File
7
+ * @filename AbstractComponent.php
8
+ * @package inky-parse
9
+ * @author Thomas Hampe <[email protected] >
10
+ * @copyright 2013-2016 Thomas Hampe
11
+ * @date 10.01.16
12
+ */
13
+
14
+
15
+ namespace Hampe \Inky \Component ;
16
+
17
+
18
+ use PHPHtmlParser \Dom \HtmlNode ;
19
+
20
+ abstract class AbstractComponentFactory implements ComponentFactoryInterface
21
+ {
22
+
23
+ protected function copyChildren (HtmlNode $ fromElement , HtmlNode $ toElement )
24
+ {
25
+ $ newNodeChildren = $ fromElement ->getChildren ();
26
+ foreach ($ newNodeChildren as $ child ) {
27
+ $ toElement ->addChild ($ child );
28
+ }
29
+ return $ newNodeChildren ;
30
+ }
31
+
32
+ protected function node ($ tag , $ attributes = array ())
33
+ {
34
+ $ node = new HtmlNode ($ tag );
35
+ foreach ($ attributes as $ key => $ attribute ) {
36
+ $ node ->setAttribute ($ key , $ attribute );
37
+ }
38
+ return $ node ;
39
+ }
40
+
41
+ protected function table ($ attributes = array ())
42
+ {
43
+ return $ this ->node ('table ' , $ attributes );
44
+ }
45
+
46
+ protected function tbody ($ attributes = array ())
47
+ {
48
+ return $ this ->node ('tbody ' , $ attributes );
49
+ }
50
+
51
+ protected function tr ($ attributes = array ())
52
+ {
53
+ return $ this ->node ('tr ' , $ attributes );
54
+ }
55
+
56
+ protected function td ($ attributes = array ())
57
+ {
58
+ return $ this ->node ('td ' , $ attributes );
59
+ }
60
+
61
+ protected function th ($ attributes = array ())
62
+ {
63
+ return $ this ->node ('th ' , $ attributes );
64
+ }
65
+
66
+ protected function img ($ attributes = array ())
67
+ {
68
+ $ node = $ this ->node ('img ' , $ attributes );
69
+ $ node ->getTag ()->selfClosing ();
70
+ return $ node ;
71
+ }
72
+
73
+ protected function elementHasCssClass (HtmlNode $ element , $ cssClass )
74
+ {
75
+ $ class = $ element ->getAttribute ('class ' );
76
+ return is_string ($ class ) && strpos ($ class , $ cssClass ) !== false ;
77
+ }
78
+
79
+ protected function addCssClass ($ cssClass , HtmlNode $ element )
80
+ {
81
+ $ element ->setAttribute ('class ' , trim ($ cssClass . ' ' . $ element ->getAttribute ('class ' )));
82
+ }
83
+ }
0 commit comments