Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d0dac69

Browse files
committed
Merge pull request laravel#5449 from crynobone/patch/revert-fluent
Completely revert "remove useless contructor" changes on Illuminate\Support\Fluent where it doesn't respect object that implements IteratorAggregate and ArrayIterator.
2 parents afd0963 + f93a09c commit d0dac69

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/Illuminate/Support/Fluent.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class Fluent implements ArrayAccess, ArrayableInterface, JsonableInterface, Json
2222
*/
2323
public function __construct($attributes = array())
2424
{
25-
$this->attributes = (array) $attributes;
25+
foreach ($attributes as $key => $value)
26+
{
27+
$this->attributes[$key] = $value;
28+
}
2629
}
2730

2831
/**

tests/Support/SupportFluentTest.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ public function testAttributesAreSetByConstructor()
2424

2525

2626
/**
27-
* Test the Fluent constructor.
27+
* Test the Fluent constructor when given \stdClass/object.
2828
*
2929
* @test
3030
*/
3131
public function testAttributesAreSetByConstructorGivenStdClass()
3232
{
3333
$array = array('name' => 'Taylor', 'age' => 25);
34-
3534
$fluent = new Fluent((object) $array);
3635

3736
$refl = new \ReflectionObject($fluent);
@@ -43,6 +42,25 @@ public function testAttributesAreSetByConstructorGivenStdClass()
4342
}
4443

4544

45+
/**
46+
* Test the Fluent constructor when given ArrayIterator interface.
47+
*
48+
* @test
49+
*/
50+
public function testAttributesAreSetByConstructorGivenArrayIterator()
51+
{
52+
$array = array('name' => 'Taylor', 'age' => 25);
53+
$fluent = new Fluent(new FluentArrayIteratorStub($array));
54+
55+
$refl = new \ReflectionObject($fluent);
56+
$attributes = $refl->getProperty('attributes');
57+
$attributes->setAccessible(true);
58+
59+
$this->assertEquals($array, $attributes->getValue($fluent));
60+
$this->assertEquals($array, $fluent->getAttributes());
61+
}
62+
63+
4664
/**
4765
* Test the Fluent::get() method.
4866
*
@@ -114,3 +132,18 @@ public function testToJsonEncodesTheToArrayResult()
114132
$this->assertEquals(json_encode('foo'), $results);
115133
}
116134
}
135+
136+
137+
class FluentArrayIteratorStub implements \IteratorAggregate {
138+
protected $items = array();
139+
140+
public function __construct(array $items = array())
141+
{
142+
$this->items = (array) $items;
143+
}
144+
145+
public function getIterator()
146+
{
147+
return new \ArrayIterator($this->items);
148+
}
149+
}

0 commit comments

Comments
 (0)