Merge branch '1.x' into 2.x

* 1.x:
  tweaked twig_to_array
This commit is contained in:
Fabien Potencier
2019-04-11 08:51:43 +02:00
2 changed files with 13 additions and 13 deletions
+11 -7
View File
@@ -719,9 +719,13 @@ function twig_last(Environment $env, $item)
*/
function twig_join_filter($value, $glue = '', $and = null)
{
if (!twig_test_iterable($value)) {
$value = (array) $value;
}
$value = twig_to_array($value, false);
if (!\is_array($value) || 0 === \count($value)) {
if (0 === \count($value)) {
return '';
}
@@ -1320,14 +1324,10 @@ function twig_to_array($seq, $preserveKeys = true)
}
if (!\is_array($seq)) {
return (array) $seq;
return $seq;
}
if (!$preserveKeys) {
return array_values($seq);
}
return $seq;
return $preserveKeys ? $seq : array_values($seq);
}
/**
@@ -1482,6 +1482,10 @@ function twig_constant_is_defined($constant, $object = null)
*/
function twig_array_batch($items, $size, $fill = null, $preserveKeys = true)
{
if (!twig_test_iterable($items)) {
throw new RuntimeError(sprintf('The "batch" filter expects an array or "Traversable", got "%s".', \is_object($from) ? \get_class($from) : \gettype($from)));
}
$size = ceil($size);
$result = array_chunk(twig_to_array($items, $preserveKeys), $size, $preserveKeys);
+2 -6
View File
@@ -40,18 +40,14 @@ class WithNode extends Node
->write(sprintf('$%s = ', $varsName))
->subcompile($this->getNode('variables'))
->raw(";\n")
->write(sprintf("if (\$%s instanceof \\Traversable) {\n", $varsName))
->indent()
->write(sprintf("\$%s = iterator_to_array(\$%s);\n", $varsName, $varsName))
->outdent()
->write("}\n")
->write(sprintf("if (!is_array(\$%s)) {\n", $varsName))
->write(sprintf("if (!twig_test_iterable(\$%s)) {\n", $varsName))
->indent()
->write("throw new RuntimeError('Variables passed to the \"with\" tag must be a hash.', ")
->repr($this->getTemplateLine())
->raw(", \$this->source);\n")
->outdent()
->write("}\n")
->write(sprintf("\$%s = twig_to_array(\$%s);\n", $varsName, $varsName))
;
if ($this->getAttribute('only')) {