You are here

public static function ResponsiveImage::parse in Image Lazyloader 8

Creates a responsive image instance from a string.

Return value

$this

5 calls to ResponsiveImage::parse()
lazyloader_preprocess_image in ./lazyloader.module
Implements hook_preprocess_image().
lazyloader_preprocess_responsive_image in ./lazyloader.module
Implements hook_preprocess_responsive_image().
ResponsiveImageTest::testMultipleImages in tests/src/Unit/ResponsiveImageTest.php
Tests multiple images.
ResponsiveImageTest::testSingleImage in tests/src/Unit/ResponsiveImageTest.php
@covers ::count @covers ::parse
ResponsiveImageTest::testSingleImageWithDensity in tests/src/Unit/ResponsiveImageTest.php
@covers ::count @covers ::parse

File

src/ResponsiveImage.php, line 53

Class

ResponsiveImage
Class ResponsiveImage.

Namespace

Drupal\lazyloader

Code

public static function parse($string) {
  $strings = array_map('trim', explode(',', $string));
  $images = array_map(function ($string) {
    $elements = explode(' ', $string);
    $object = new \stdClass();
    $object->uri = $elements[0];
    $object->density = NULL;
    $object->width = NULL;
    unset($elements[0]);
    foreach ($elements as $element) {
      if ($element[strlen($element) - 1] === 'w') {
        $object->width = substr($element, 0, -1);
      }
      if ($element[strlen($element) - 1] === 'x') {
        $object->density = substr($element, 0, -1);
      }
    }
    return $object;
  }, $strings);
  return new static($images);
}