1: <?php
2: /**
3: * @filesource Kotchasan/DOMNode.php
4: *
5: * @copyright 2016 Goragod.com
6: * @license http://www.kotchasan.com/license/
7: *
8: * @see http://www.kotchasan.com/
9: */
10:
11: namespace Kotchasan;
12:
13: /**
14: * คลาสสำหรับ Dom Node.
15: *
16: * @author Goragod Wiriya <admin@goragod.com>
17: *
18: * @since 1.0
19: */
20: class DOMNode
21: {
22: /**
23: * รายการคุณสมบัติของโหนด.
24: *
25: * @var array
26: */
27: public $attributes = array();
28: /**
29: * รายการของโหนดที่อยู่ภายใน
30: * <parentNode><childNode></childNode><childNode></childNode></parentNode>.
31: *
32: * @var array
33: */
34: public $childNodes;
35: /**
36: * ลำดับของโหนด ชั้นนอกสุดคือ 0.
37: *
38: * @var int
39: */
40: public $level;
41: /**
42: * โหนดถัดไป (ลำดับเดียวกัน) ถ้าเป็นโหนดสุดท้ายจะเป็น null
43: * <node></node><nextSibling></nextSibling>.
44: *
45: * @var DOMNode
46: */
47: public $nextSibling;
48: /**
49: * @var mixed
50: */
51: public $nodeName;
52: /**
53: * ข้อความภายในโหนด ถ้าเป็น tag ค่านี้จะเป็น null
54: * <node>nodeValue</node>.
55: *
56: * @var string|null
57: */
58: public $nodeValue;
59: /**
60: * โหนดแม่
61: * <parentNode><childNode></childNode></parentNode>.
62: *
63: * @var DOMNode
64: */
65: public $parentNode;
66: /**
67: * โหนดก่อนหน้า (ลำดับเดียวกัน) ถ้าเป็นโหนดแรกจะเป็น null
68: * <previousSibling></previousSibling><node></node>.
69: *
70: * @var DOMNode
71: */
72: public $previousSibling;
73:
74: /**
75: * class constructor.
76: *
77: * @param string $nodeName ชื่อ tag ถ้าไม่มีชื่อ tag หมายถึงข้อความเปล่าๆ
78: * @param DOMNode|null $parentNode โหนดแม่ ถ้าเป็นโหนดแรกคือ null
79: * @param array $attributes คุณสมบัติของโหนก (properties)
80: * @param string|null $nodeValue ข้อความภายในโหนด ถ้าเป็น tag ค่านี้จะเป็น null
81: */
82: public function __construct($nodeName, $parentNode, $attributes, $nodeValue = null)
83: {
84: $this->nodeName = strtoupper($nodeName);
85: $this->parentNode = $parentNode;
86: $this->nodeValue = $nodeValue;
87: foreach ($attributes as $key => $value) {
88: $this->attributes[strtoupper($key)] = $value;
89: }
90: $this->childNodes = array();
91: }
92:
93: /**
94: * ตรวจสอบว่ามีโหนดลูกหรือไม่
95: * คืนค่า true ถ้ามีโหนดลูก, false ถ้าไม่มี.
96: *
97: * @return bool
98: */
99: public function hasChildNodes()
100: {
101: return !empty($this->childNodes);
102: }
103:
104: /**
105: * ตรวจสอบว่ามีคลาสอยู่หรือไม่
106: * คืนค่า true ถ้ามี.
107: *
108: * @param string $className ชื่อคลาสที่ต้องการตรวจสอบ
109: *
110: * @return bool
111: */
112: public function hasClass($className)
113: {
114: if (!empty($this->attributes['CLASS'])) {
115: $className = strtoupper($className);
116: foreach (explode(' ', strtoupper($this->attributes['CLASS'])) as $item) {
117: if ($item == $className) {
118: return true;
119: }
120: }
121: }
122:
123: return false;
124: }
125:
126: /**
127: * ตรวจสอบว่าเป็น element แบบ Inline หรือไม่
128: * คืนค่า true ถ้าเป็น Inline Elements หรือ false ถ้าเป็น Block-level Elements.
129: *
130: * @return bool
131: */
132: public function isInlineElement()
133: {
134: switch ($this->nodeName) {
135: case 'B':
136: case 'BIG':
137: case 'I':
138: case 'SMALL':
139: case 'TT':
140: case 'ABBR':
141: case 'ACRONYM':
142: case 'CITE':
143: case 'CODE':
144: case 'DFN':
145: case 'EM':
146: case 'STRONG':
147: case 'SAMP':
148: case 'TIME':
149: case 'VAR':
150: case 'A':
151: case 'BDO':
152: case 'BR':
153: case 'IMG':
154: case 'MAP':
155: case 'OBJECT':
156: case 'Q':
157: case 'SCRIPT':
158: case 'SPAN':
159: case 'SUB':
160: case 'BUTTON':
161: case 'INPUT':
162: case 'LABEL':
163: case 'SELECT':
164: case 'TEXTAREA':
165: return true;
166: }
167:
168: return false;
169: }
170:
171: /**
172: * คืนค่า ข้อความทั้งหมดภายในโหนด.
173: *
174: * @return string
175: */
176: public function nodeText()
177: {
178: $txt = '';
179: foreach ($this->childNodes as $node) {
180: if ($node->hasChildNodes()) {
181: $txt .= $this->nodeText();
182: } else {
183: switch ($node->nodeName) {
184: case 'BR':
185: $txt .= "\n";
186: break;
187: case '':
188: $txt .= $node->nodeValue;
189: break;
190: }
191: }
192: }
193:
194: return $this->unentities($txt);
195: }
196:
197: /**
198: * แปลงรหัส HTML เป็นข้อความ เช่น < เป็น <.
199: *
200: * @param string $html
201: *
202: * @return string
203: */
204: public function unentities($html)
205: {
206: return str_replace(array(' ', '&', '<', '>', ''', '"'), array(' ', '&', '<', '>', "'", '"'), $html);
207: }
208: }
209: