tag = new HtmlTag(TagName::IMG); $this->attributes = HtmlAttributes::empty() ->with('src', $this->src) ->with('alt', $this->alt) ->with('loading', $this->loading) ->with('decoding', $this->decoding); if ($this->width !== null) { $this->attributes = $this->attributes->with('width', (string) $this->width); } if ($this->height !== null) { $this->attributes = $this->attributes->with('height', (string) $this->height); } if ($this->srcset !== null) { $this->attributes = $this->attributes->with('srcset', $this->srcset); } if ($this->sizes !== null) { $this->attributes = $this->attributes->with('sizes', $this->sizes); } // Merge additional attributes foreach ($this->additionalAttributes->attributes as $name => $value) { $this->attributes = $this->attributes->with($name, $value); } $this->content = ''; } public static function create(string $src, string $alt): self { return new self(src: $src, alt: $alt); } public static function responsive( string $src, string $alt, string $srcset, string $sizes = '100vw' ): self { return new self( src: $src, alt: $alt, srcset: $srcset, sizes: $sizes ); } public function withDimensions(int $width, int $height): self { return new self( src: $this->src, alt: $this->alt, width: $width, height: $height, loading: $this->loading, decoding: $this->decoding, srcset: $this->srcset, sizes: $this->sizes, additionalAttributes: $this->additionalAttributes ); } public function eager(): self { return new self( src: $this->src, alt: $this->alt, width: $this->width, height: $this->height, loading: 'eager', decoding: $this->decoding, srcset: $this->srcset, sizes: $this->sizes, additionalAttributes: $this->additionalAttributes ); } public function syncDecoding(): self { return new self( src: $this->src, alt: $this->alt, width: $this->width, height: $this->height, loading: $this->loading, decoding: 'sync', srcset: $this->srcset, sizes: $this->sizes, additionalAttributes: $this->additionalAttributes ); } public function __toString(): string { return (string) StandardHtmlElement::create( $this->tag->name, $this->attributes, $this->content ); } }