Tag.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace PHPHtmlParser\Dom;
  3. use PHPHtmlParser\Dom;
  4. use stringEncode\Encode;
  5. /**
  6. * Class Tag
  7. *
  8. * @package PHPHtmlParser\Dom
  9. */
  10. class Tag
  11. {
  12. /**
  13. * The name of the tag.
  14. *
  15. * @var string
  16. */
  17. protected $name;
  18. /**
  19. * The attributes of the tag.
  20. *
  21. * @var array
  22. */
  23. protected $attr = [];
  24. /**
  25. * Is this tag self closing.
  26. *
  27. * @var bool
  28. */
  29. protected $selfClosing = false;
  30. /**
  31. * If self-closing, will this use a trailing slash. />
  32. *
  33. * @var bool
  34. */
  35. protected $trailingSlash = true;
  36. /**
  37. * Tag noise
  38. */
  39. protected $noise = '';
  40. /**
  41. * The encoding class to... encode the tags
  42. *
  43. * @var mixed
  44. */
  45. protected $encode = null;
  46. /**
  47. * Sets up the tag with a name.
  48. *
  49. * @param $name
  50. */
  51. public function __construct(string $name)
  52. {
  53. $this->name = $name;
  54. }
  55. /**
  56. * Magic method to get any of the attributes.
  57. *
  58. * @param string $key
  59. * @return mixed
  60. */
  61. public function __get($key)
  62. {
  63. return $this->getAttribute($key);
  64. }
  65. /**
  66. * Magic method to set any attribute.
  67. *
  68. * @param string $key
  69. * @param mixed $value
  70. */
  71. public function __set($key, $value)
  72. {
  73. $this->setAttribute($key, $value);
  74. }
  75. /**
  76. * Returns the name of this tag.
  77. *
  78. * @return string
  79. */
  80. public function name(): string
  81. {
  82. return $this->name;
  83. }
  84. /**
  85. * Sets the tag to be self closing.
  86. *
  87. * @return Tag
  88. * @chainable
  89. */
  90. public function selfClosing(): Tag
  91. {
  92. $this->selfClosing = true;
  93. return $this;
  94. }
  95. /**
  96. * Sets the tag to not use a trailing slash.
  97. *
  98. * @return Tag
  99. * @chainable
  100. */
  101. public function noTrailingSlash(): Tag
  102. {
  103. $this->trailingSlash = false;
  104. return $this;
  105. }
  106. /**
  107. * Checks if the tag is self closing.
  108. *
  109. * @return bool
  110. */
  111. public function isSelfClosing(): bool
  112. {
  113. return $this->selfClosing;
  114. }
  115. /**
  116. * Sets the encoding type to be used.
  117. *
  118. * @param Encode $encode
  119. * @return void
  120. */
  121. public function setEncoding(Encode $encode): void
  122. {
  123. $this->encode = $encode;
  124. }
  125. /**
  126. * Sets the noise for this tag (if any)
  127. *
  128. * @param string $noise
  129. * @return Tag
  130. * @chainable
  131. */
  132. public function noise(string $noise): Tag
  133. {
  134. $this->noise = $noise;
  135. return $this;
  136. }
  137. /**
  138. * Set an attribute for this tag.
  139. *
  140. * @param string $key
  141. * @param string|array $value
  142. * @return Tag
  143. * @chainable
  144. */
  145. public function setAttribute(string $key, $value): Tag
  146. {
  147. $key = strtolower($key);
  148. if ( ! is_array($value)) {
  149. $value = [
  150. 'value' => $value,
  151. 'doubleQuote' => true,
  152. ];
  153. }
  154. $this->attr[$key] = $value;
  155. return $this;
  156. }
  157. /**
  158. * Set inline style attribute value.
  159. *
  160. * @param mixed $attr_key
  161. * @param mixed $attr_value
  162. */
  163. public function setStyleAttributeValue($attr_key, $attr_value): void
  164. {
  165. $style_array = $this->getStyleAttributeArray();
  166. $style_array[$attr_key] = $attr_value;
  167. $style_string = '';
  168. foreach ($style_array as $key => $value) {
  169. $style_string .= $key . ':' . $value . ';';
  170. }
  171. $this->setAttribute('style', $style_string);
  172. }
  173. /**
  174. * Get style attribute in array
  175. *
  176. * @return array
  177. */
  178. public function getStyleAttributeArray(): array
  179. {
  180. $value = $this->getAttribute('style')['value'];
  181. if ($value === null) {
  182. return [];
  183. }
  184. $value = explode(';', substr(trim($value), 0, -1));
  185. $result = [];
  186. foreach ($value as $attr) {
  187. $attr = explode(':', $attr);
  188. $result[$attr[0]] = $attr[1];
  189. }
  190. return $result;
  191. }
  192. /**
  193. * Removes an attribute from this tag.
  194. *
  195. * @param mixed $key
  196. * @return void
  197. */
  198. public function removeAttribute($key)
  199. {
  200. $key = strtolower($key);
  201. unset($this->attr[$key]);
  202. }
  203. /**
  204. * Removes all attributes on this tag.
  205. *
  206. * @return void
  207. */
  208. public function removeAllAttributes()
  209. {
  210. $this->attr = [];
  211. }
  212. /**
  213. * Sets the attributes for this tag
  214. *
  215. * @param array $attr
  216. * @return $this
  217. */
  218. public function setAttributes(array $attr)
  219. {
  220. foreach ($attr as $key => $value) {
  221. $this->setAttribute($key, $value);
  222. }
  223. return $this;
  224. }
  225. /**
  226. * Returns all attributes of this tag.
  227. *
  228. * @return array
  229. */
  230. public function getAttributes()
  231. {
  232. $return = [];
  233. foreach ($this->attr as $attr => $info) {
  234. $return[$attr] = $this->getAttribute($attr);
  235. }
  236. return $return;
  237. }
  238. /**
  239. * Returns an attribute by the key
  240. *
  241. * @param string $key
  242. * @return mixed
  243. */
  244. public function getAttribute(string $key)
  245. {
  246. if ( ! isset($this->attr[$key])) {
  247. return null;
  248. }
  249. $value = $this->attr[$key]['value'];
  250. if (is_string($value) && ! is_null($this->encode)) {
  251. // convert charset
  252. $this->attr[$key]['value'] = $this->encode->convert($value);
  253. }
  254. return $this->attr[$key];
  255. }
  256. /**
  257. * Returns TRUE if node has attribute
  258. *
  259. * @param string $key
  260. * @return bool
  261. */
  262. public function hasAttribute(string $key)
  263. {
  264. return isset($this->attr[$key]);
  265. }
  266. /**
  267. * Generates the opening tag for this object.
  268. *
  269. * @return string
  270. */
  271. public function makeOpeningTag()
  272. {
  273. $return = '<'.$this->name;
  274. // add the attributes
  275. foreach ($this->attr as $key => $info) {
  276. $info = $this->getAttribute($key);
  277. $val = $info['value'];
  278. if (is_null($val)) {
  279. $return .= ' '.$key;
  280. } elseif ($info['doubleQuote']) {
  281. $return .= ' '.$key.'="'.$val.'"';
  282. } else {
  283. $return .= ' '.$key.'=\''.$val.'\'';
  284. }
  285. }
  286. if ($this->selfClosing && $this->trailingSlash) {
  287. return $return.' />';
  288. } else {
  289. return $return.'>';
  290. }
  291. }
  292. /**
  293. * Generates the closing tag for this object.
  294. *
  295. * @return string
  296. */
  297. public function makeClosingTag()
  298. {
  299. if ($this->selfClosing) {
  300. return '';
  301. }
  302. return '</'.$this->name.'>';
  303. }
  304. }