123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?php
- namespace PHPHtmlParser\Dom;
- use PHPHtmlParser\Dom;
- use stringEncode\Encode;
- /**
- * Class Tag
- *
- * @package PHPHtmlParser\Dom
- */
- class Tag
- {
- /**
- * The name of the tag.
- *
- * @var string
- */
- protected $name;
- /**
- * The attributes of the tag.
- *
- * @var array
- */
- protected $attr = [];
- /**
- * Is this tag self closing.
- *
- * @var bool
- */
- protected $selfClosing = false;
- /**
- * If self-closing, will this use a trailing slash. />
- *
- * @var bool
- */
- protected $trailingSlash = true;
- /**
- * Tag noise
- */
- protected $noise = '';
- /**
- * The encoding class to... encode the tags
- *
- * @var mixed
- */
- protected $encode = null;
- /**
- * Sets up the tag with a name.
- *
- * @param $name
- */
- public function __construct(string $name)
- {
- $this->name = $name;
- }
- /**
- * Magic method to get any of the attributes.
- *
- * @param string $key
- * @return mixed
- */
- public function __get($key)
- {
- return $this->getAttribute($key);
- }
- /**
- * Magic method to set any attribute.
- *
- * @param string $key
- * @param mixed $value
- */
- public function __set($key, $value)
- {
- $this->setAttribute($key, $value);
- }
- /**
- * Returns the name of this tag.
- *
- * @return string
- */
- public function name(): string
- {
- return $this->name;
- }
- /**
- * Sets the tag to be self closing.
- *
- * @return Tag
- * @chainable
- */
- public function selfClosing(): Tag
- {
- $this->selfClosing = true;
- return $this;
- }
- /**
- * Sets the tag to not use a trailing slash.
- *
- * @return Tag
- * @chainable
- */
- public function noTrailingSlash(): Tag
- {
- $this->trailingSlash = false;
- return $this;
- }
- /**
- * Checks if the tag is self closing.
- *
- * @return bool
- */
- public function isSelfClosing(): bool
- {
- return $this->selfClosing;
- }
- /**
- * Sets the encoding type to be used.
- *
- * @param Encode $encode
- * @return void
- */
- public function setEncoding(Encode $encode): void
- {
- $this->encode = $encode;
- }
- /**
- * Sets the noise for this tag (if any)
- *
- * @param string $noise
- * @return Tag
- * @chainable
- */
- public function noise(string $noise): Tag
- {
- $this->noise = $noise;
- return $this;
- }
- /**
- * Set an attribute for this tag.
- *
- * @param string $key
- * @param string|array $value
- * @return Tag
- * @chainable
- */
- public function setAttribute(string $key, $value): Tag
- {
- $key = strtolower($key);
- if ( ! is_array($value)) {
- $value = [
- 'value' => $value,
- 'doubleQuote' => true,
- ];
- }
- $this->attr[$key] = $value;
- return $this;
- }
- /**
- * Set inline style attribute value.
- *
- * @param mixed $attr_key
- * @param mixed $attr_value
- */
- public function setStyleAttributeValue($attr_key, $attr_value): void
- {
- $style_array = $this->getStyleAttributeArray();
- $style_array[$attr_key] = $attr_value;
- $style_string = '';
- foreach ($style_array as $key => $value) {
- $style_string .= $key . ':' . $value . ';';
- }
- $this->setAttribute('style', $style_string);
- }
- /**
- * Get style attribute in array
- *
- * @return array
- */
- public function getStyleAttributeArray(): array
- {
- $value = $this->getAttribute('style')['value'];
- if ($value === null) {
- return [];
- }
- $value = explode(';', substr(trim($value), 0, -1));
- $result = [];
- foreach ($value as $attr) {
- $attr = explode(':', $attr);
- $result[$attr[0]] = $attr[1];
- }
- return $result;
- }
- /**
- * Removes an attribute from this tag.
- *
- * @param mixed $key
- * @return void
- */
- public function removeAttribute($key)
- {
- $key = strtolower($key);
- unset($this->attr[$key]);
- }
- /**
- * Removes all attributes on this tag.
- *
- * @return void
- */
- public function removeAllAttributes()
- {
- $this->attr = [];
- }
- /**
- * Sets the attributes for this tag
- *
- * @param array $attr
- * @return $this
- */
- public function setAttributes(array $attr)
- {
- foreach ($attr as $key => $value) {
- $this->setAttribute($key, $value);
- }
- return $this;
- }
- /**
- * Returns all attributes of this tag.
- *
- * @return array
- */
- public function getAttributes()
- {
- $return = [];
- foreach ($this->attr as $attr => $info) {
- $return[$attr] = $this->getAttribute($attr);
- }
- return $return;
- }
- /**
- * Returns an attribute by the key
- *
- * @param string $key
- * @return mixed
- */
- public function getAttribute(string $key)
- {
- if ( ! isset($this->attr[$key])) {
- return null;
- }
- $value = $this->attr[$key]['value'];
- if (is_string($value) && ! is_null($this->encode)) {
- // convert charset
- $this->attr[$key]['value'] = $this->encode->convert($value);
- }
- return $this->attr[$key];
- }
- /**
- * Returns TRUE if node has attribute
- *
- * @param string $key
- * @return bool
- */
- public function hasAttribute(string $key)
- {
- return isset($this->attr[$key]);
- }
- /**
- * Generates the opening tag for this object.
- *
- * @return string
- */
- public function makeOpeningTag()
- {
- $return = '<'.$this->name;
- // add the attributes
- foreach ($this->attr as $key => $info) {
- $info = $this->getAttribute($key);
- $val = $info['value'];
- if (is_null($val)) {
- $return .= ' '.$key;
- } elseif ($info['doubleQuote']) {
- $return .= ' '.$key.'="'.$val.'"';
- } else {
- $return .= ' '.$key.'=\''.$val.'\'';
- }
- }
- if ($this->selfClosing && $this->trailingSlash) {
- return $return.' />';
- } else {
- return $return.'>';
- }
- }
- /**
- * Generates the closing tag for this object.
- *
- * @return string
- */
- public function makeClosingTag()
- {
- if ($this->selfClosing) {
- return '';
- }
- return '</'.$this->name.'>';
- }
- }
|