Overview

Namespaces

  • Scene7
    • Commands
      • Layer
    • Definitions
    • Helpers
      • Html
        • Attributes
    • Requests

Classes

  • AbstractTag
  • Image
  • Picture
  • Source
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 
<?php

namespace Scene7\Helpers\Html;

use Scene7\Requests;

class Picture extends AbstractTag
{
    protected $image;
    protected $sources = [];

    public function __construct(array $attributes = [])
    {
        $this->setAttributes($attributes);
    }

    /**
     * @param Requests\Image $image
     * @param int[] $multipliers
     * @param array $attributes
     * @return Picture
     */
    public function addSourceFromImage(Requests\Image $image, $multipliers = [], array $attributes = [])
    {
        $source = new Source($attributes);
        $source->createFromImage($image, $multipliers);
        return $this->addSource($source);
    }

    /**
     * Add all of the sources with a single call.
     *
     * This looks complicated, but the goal is merely to reduce the amount of code you have to write
     *
     * @param array $mediaQueries
     * @param Requests\Image $image
     * @param array $multipliers
     * @param array $attributes
     * @return $this
     */
    public function addSourceListFromImage(array $mediaQueries, Requests\Image $image, $multipliers = [], array $attributes = [])
    {
        foreach ($mediaQueries as $query => $imageAttributes) {
            // Set the image to match the query
            $cloneImg = clone $image;
            foreach ($imageAttributes as $imageAttribute => $imageAttributeValue) {
                $method = 'set' . ucfirst($imageAttribute);
                if (is_callable([$cloneImg, $method])) {
                    $cloneImg->$method($imageAttributeValue);
                }
            }

            $this->addSourceFromImage($cloneImg, $multipliers, array_merge($attributes, ['media' => $query]));
        }

        return $this;
    }

    /**
     * @param Image|string $image
     * @param array $attributes
     * @return $this
     */
    public function setImage($image, array $attributes = [])
    {
        if (!($image instanceof Image)) {
            $image = new Image($image, $attributes['alt'] ?: '', $attributes);
        }

        $this->image = $image;
        return $this;
    }

    public function addSource(Source $source)
    {
        $this->sources[] = $source;
        return $this;
    }

    public function render()
    {
        $tag = '<picture' . $this->renderAttributes() . '>';

        $tag .= $this->renderSources();
        $tag .= $this->renderImage();

        return $tag . '</picture>';
    }

    protected function renderSources()
    {
        $sources = '';
        foreach ($this->sources as $source) {
            $sources .= $source->render();
        }

        return $sources;
    }

    /**
     * @throws \RuntimeException if no image is set
     * @return string
     */
    protected function renderImage()
    {
        if (!($this->image instanceof Image)) {
            throw new \RuntimeException('No image set');
        }

        return $this->image->render();
    }

    public function __toString()
    {
        return $this->render();
    }
}
API documentation generated by ApiGen