/** * Generates the HTML for the represented <source> or <img> element. * * @param SrcsetGeneratorInterface $srcset_gen * The generator instance that will be used to generate the srcset * attributes. * @param mixed $image * The image representation that will be passed to $srcset_gen * @param string $alt * The for an <img> `alt` attribute. Only used if $this->as_img is true. * * @return string * The HTML for either a <source> or <img> element depending on the value * * @see SrcsetGeneratorInterface */ public function renderWith(SrcsetGeneratorInterface $srcset_gen, $image, $alt = '') { $last = F\last($this->sizes); $srcset = F\map($this->sizes, function (Size $size) use($image, $srcset_gen) { return $srcset_gen->listFor($image, $size); }); $srcset = F\unique(F\flatten($srcset), function (Src $src) { return $src->getUrl(); }); # Not really needed, but makes debugging nicer. usort($srcset, function (Src $l, Src $r) { $l = $l->getWidth() ?: (int) $l->getMultiplier(); $r = $r->getWidth() ?: (int) $r->getMultiplier(); return $l - $r; }); $srcset = implode(', ', $srcset); $sizes = F\map($this->sizes, function (Size $size) use($last) { return $size === $last ? $size->renderWidthOnly() : (string) $size; }); $sizes = implode(', ', $sizes); $media = !$this->as_img ? ' media="' . $last->getMediaQuery() . '"' : ''; $alt = $this->as_img ? ' alt="' . htmlspecialchars($alt) . '"' : ''; $attributes = "srcset=\"{$srcset}\" sizes=\"{$sizes}\"{$media}{$alt}"; return $this->as_img ? "<img {$attributes}>" : "<source {$attributes}>"; }
public function setUp() { $this->functions = F\flatten((array) (func_num_args() > 0 ? func_get_arg(0) : $this->getFunctionName())); foreach ($this->functions as $function) { if (!function_exists($function)) { $this->markTestSkipped(sprintf('Function "%s()" not implemented in %s version', $function, extension_loaded('functional') ? 'native C' : 'PHP userland')); break; } } }
function setUp() { $this->functions = F\flatten(func_num_args() > 0 ? func_get_arg(0) : array(ucfirst(strtolower(str_replace('Test', '', get_class($this)))))); foreach ($this->functions as $function) { if (!function_exists($function)) { $this->markTestSkipped(sprintf('Function "%s()" not implemented in %s version', $function, extension_loaded('functional') ? 'native C' : 'PHP userland')); break; } } }
/** * Flatten Types, e.g. flatten UnionTypes. * * This function won't have much of an effect on most types, unless they contain subtypes. * * Because this function preserves duplicate entries, usually it may be preferable to use Type::unique() * which also flattens and removes duplicate entries. * * @param Type|Type[] ...$types Use array dereferencing to pass an array into this function. * * @return array */ public static function flatten(Type ...$types) { // Flatten types with subtypes e.g. UnionTypes $walker = function (Type $x) { // If the types $x contains is not just itself, go deeper! if (count($x->getTypes()) !== 1 or !first($x->getTypes())->typeof($x)) { return Type::flatten($x); } return $x->getTypes(); }; $x = array_map($walker, $types); return array_flatten($x); }
public function testPassNoCollection() { $this->expectArgumentError('Functional\\flatten() expects parameter 1 to be array or instance of Traversable'); flatten('invalidCollection'); }