/** * Creates a new option. * * @param string $value * @param mixed $attributes */ public static function create($value, $attributes = null) { $option = new static(); $option->attr('value', $value); if (!is_array($attributes)) { return $option->html($attributes ? $attributes : $value); } foreach ($attributes as $n => $v) { $option->{$n}($v); } if (!$option->html()) { $option->html($value); } return $option; }
/** * Add content * * @param mixed $text * * @return this */ public function add($text) { if ($text !== null && $text !== '') { if (!$text instanceof static) { if (preg_match('/(?P<all>\\<(?P<tag>' . implode('|', $this->allowed_tags) . ')(?P<attr>(\\s+[a-z-_0-9]+\\=\\"[^\\"<>]*\\")*)\\>(?P<content>.*?)\\<\\/\\2>)/ui', $text, $matches, PREG_OFFSET_CAPTURE) === 1 || preg_match('/(?P<all>\\<(?P<tag>' . implode('|', $this->allowed_singleton_tags) . ')(?P<attr>(\\s+[a-z-_0-9]+\\=\\"[^\\"<>]*\\")*)(\\s*\\/)?\\>)/ui', $text, $matches, PREG_OFFSET_CAPTURE) === 1) { $tag = new static($matches['tag'][0]); if (preg_match_all('/\\s+(?P<name>[a-z-_0-9]+)\\=\\"(?P<value>[^\\"<>]*)\\"/ui', $matches['attr'][0], $attr_matches) > 0) { foreach ($attr_matches['name'] as $key => $name) { $tag->attr($name, $attr_matches['value'][$key]); } } if (isset($matches['content'])) { $tag->setContent($matches['content'][0]); } $this->add(substr($text, 0, $matches['all'][1])); $this->content[] = $tag; $this->add(substr($text, $matches['all'][1] + strlen($matches['all'][0]))); return $this; } } $this->content[] = $text; } return $this; }