content($content); $this->padding($padding); } public function content($content) { $content = $this->convertJsonToArray($content) ?: $content; if ($content instanceof Renderable) { $this->content = $content->render(); } elseif (is_array($content) || is_object($content)) { $this->content = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); } else { $this->content = $content; } return $this; } /** * @param string|null $padding * * @return $this */ public function padding(?string $padding) { if ($padding) { $this->padding = $padding; } return $this; } /** * @param string $width * * @return $this */ public function maxWidth($width) { $this->maxWidth = $width; return $this; } /** * @param mixed $content * * @return array|null */ protected function convertJsonToArray($content) { if ( is_string($content) && ( (strpos($content, '{') === 0 && strpos($content, '}', -1) !== false) || (strpos($content, '[') === 0 && strpos($content, ']', -1) !== false) ) ) { return json_decode($content, true); } } public function render() { $this->defaultHtmlAttribute( 'style', 'white-space:pre-wrap;'.($this->maxWidth ? "max-width:{$this->maxWidth};" : '') ); return <<
formatHtmlAttributes()}>{$this->content}
EOF; } }