Skip to content

Commit 9cd28c1

Browse files
authored
Allow \Blade::stringable() to be called on native Iterables (#49591)
1 parent 3559497 commit 9cd28c1

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/Illuminate/View/Compilers/Concerns/CompilesEchos.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ public function applyEchoHandler($value)
162162
return call_user_func($this->echoHandlers[get_class($value)], $value);
163163
}
164164

165+
if (is_iterable($value) && isset($this->echoHandlers['iterable'])) {
166+
return call_user_func($this->echoHandlers['iterable'], $value);
167+
}
168+
165169
return $value;
166170
}
167171
}

tests/View/Blade/BladeEchoHandlerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,34 @@ public static function handlerLogicDataProvider()
8080
];
8181
}
8282

83+
/**
84+
* @dataProvider handlerWorksWithIterableDataProvider
85+
*/
86+
public function testHandlerWorksWithIterables($blade, $closure, $expectedOutput)
87+
{
88+
$this->compiler->stringable('iterable', $closure);
89+
90+
app()->singleton('blade.compiler', function () {
91+
return $this->compiler;
92+
});
93+
94+
ob_start();
95+
eval(Str::of($this->compiler->compileString($blade))->remove(['<?php', '?>']));
96+
$output = ob_get_contents();
97+
ob_end_clean();
98+
99+
$this->assertSame($expectedOutput, $output);
100+
}
101+
102+
public static function handlerWorksWithIterableDataProvider()
103+
{
104+
return [
105+
['{{[1,"two",3]}}', function(iterable $arr) {
106+
return implode(', ', $arr);
107+
}, "1, two, 3"],
108+
];
109+
}
110+
83111
/**
84112
* @dataProvider nonStringableDataProvider
85113
*/

0 commit comments

Comments
 (0)