forked from tighten/collect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
186 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Illuminate\Contracts\Support; | ||
|
||
interface Htmlable | ||
{ | ||
/** | ||
* Get content as a string of HTML. | ||
* | ||
* @return string | ||
*/ | ||
public function toHtml(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Illuminate\Support; | ||
|
||
use Illuminate\Contracts\Support\Htmlable; | ||
|
||
class HtmlString implements Htmlable | ||
{ | ||
/** | ||
* The HTML string. | ||
* | ||
* @var string | ||
*/ | ||
protected $html; | ||
|
||
/** | ||
* Create a new HTML string instance. | ||
* | ||
* @param string $html | ||
* @return void | ||
*/ | ||
public function __construct($html) | ||
{ | ||
$this->html = $html; | ||
} | ||
|
||
/** | ||
* Get the HTML string. | ||
* | ||
* @return string | ||
*/ | ||
public function toHtml() | ||
{ | ||
return $this->html; | ||
} | ||
|
||
/** | ||
* Get the HTML string. | ||
* | ||
* @return string | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->toHtml(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,19 @@ public function testFirstWithDefaultAndWithoutCallback() | |
$this->assertEquals('default', $result); | ||
} | ||
|
||
public function testFirstWhere() | ||
{ | ||
$data = new Collection([ | ||
['material' => 'paper', 'type' => 'book'], | ||
['material' => 'rubber', 'type' => 'gasket'], | ||
]); | ||
|
||
$this->assertEquals('book', $data->firstWhere('material', 'paper')['type']); | ||
$this->assertEquals('gasket', $data->firstWhere('material', 'rubber')['type']); | ||
$this->assertNull($data->firstWhere('material', 'nonexistant')); | ||
$this->assertNull($data->firstWhere('nonexistant', 'key')); | ||
} | ||
|
||
public function testLastReturnsLastItemInCollection() | ||
{ | ||
$c = new Collection(['foo', 'bar']); | ||
|
@@ -123,10 +136,10 @@ public function testCollectionIsConstructed() | |
$this->assertSame([false], $collection->all()); | ||
|
||
$collection = new Collection(null); | ||
$this->assertSame([], $collection->all()); | ||
$this->assertEmpty($collection->all()); | ||
|
||
$collection = new Collection; | ||
$this->assertSame([], $collection->all()); | ||
$this->assertEmpty($collection->all()); | ||
} | ||
|
||
public function testGetArrayableItems() | ||
|
@@ -402,11 +415,53 @@ public function testWhere() | |
$c->where('v', $object)->values()->all() | ||
); | ||
|
||
$this->assertEquals( | ||
[['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]], | ||
$c->where('v', '<>', $object)->values()->all() | ||
); | ||
|
||
$this->assertEquals( | ||
[['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]], | ||
$c->where('v', '!=', $object)->values()->all() | ||
); | ||
|
||
$this->assertEquals( | ||
[['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]], | ||
$c->where('v', '!==', $object)->values()->all() | ||
); | ||
|
||
$this->assertEquals( | ||
[], | ||
$c->where('v', '>', $object)->values()->all() | ||
); | ||
|
||
$c = new Collection([['v' => 1], ['v' => $object]]); | ||
$this->assertEquals( | ||
[['v' => $object]], | ||
$c->where('v', $object)->values()->all() | ||
); | ||
|
||
$this->assertEquals( | ||
[['v' => 1], ['v' => $object]], | ||
$c->where('v', '<>', null)->values()->all() | ||
); | ||
|
||
$this->assertEquals( | ||
[], | ||
$c->where('v', '<', null)->values()->all() | ||
); | ||
|
||
$c = new Collection([['v' => 1], ['v' => new \Illuminate\Support\HtmlString('hello')]]); | ||
$this->assertEquals( | ||
[['v' => new \Illuminate\Support\HtmlString('hello')]], | ||
$c->where('v', 'hello')->values()->all() | ||
); | ||
|
||
$c = new Collection([['v' => 1], ['v' => 'hello']]); | ||
$this->assertEquals( | ||
[['v' => 'hello']], | ||
$c->where('v', new \Illuminate\Support\HtmlString('hello'))->values()->all() | ||
); | ||
} | ||
|
||
public function testWhereStrict() | ||
|
@@ -918,9 +973,11 @@ public function testExcept() | |
|
||
$this->assertEquals(['first' => 'Taylor'], $data->except(['last', 'email', 'missing'])->all()); | ||
$this->assertEquals(['first' => 'Taylor'], $data->except('last', 'email', 'missing')->all()); | ||
$this->assertEquals(['first' => 'Taylor'], $data->except(collect(['last' => 'Otwell', 'email' => '[email protected]', 'missing']))->all()); | ||
|
||
$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->except(['last'])->all()); | ||
$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->except('last')->all()); | ||
$this->assertEquals(['first' => 'Taylor', 'email' => '[email protected]'], $data->except(collect(['last' => 'Otwell']))->all()); | ||
} | ||
|
||
public function testPluckWithArrayAndObjectValues() | ||
|
@@ -2306,8 +2363,8 @@ public function testWhen() | |
{ | ||
$collection = new Collection(['michael', 'tom']); | ||
|
||
$collection->when(true, function ($collection) { | ||
return $collection->push('adam'); | ||
$collection->when('adam', function ($collection, $newName) { | ||
return $collection->push($newName); | ||
}); | ||
|
||
$this->assertSame(['michael', 'tom', 'adam'], $collection->toArray()); | ||
|