Skip to content

Commit

Permalink
Laravel 5.5.27 changes (tighten#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
besologic authored Dec 20, 2017
1 parent ea6c2f9 commit 07d58f7
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 55 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Contracts/Support/Htmlable.php
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();
}
14 changes: 9 additions & 5 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,21 @@ public static function last($array, callable $callback = null, $default = null)
*/
public static function flatten($array, $depth = INF)
{
return array_reduce($array, function ($result, $item) use ($depth) {
$result = [];

foreach ($array as $item) {
$item = $item instanceof Collection ? $item->all() : $item;

if (! is_array($item)) {
return array_merge($result, [$item]);
$result[] = $item;
} elseif ($depth === 1) {
return array_merge($result, array_values($item));
$result = array_merge($result, array_values($item));
} else {
return array_merge($result, static::flatten($item, $depth - 1));
$result = array_merge($result, static::flatten($item, $depth - 1));
}
}, []);
}

return $result;
}

/**
Expand Down
103 changes: 57 additions & 46 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,7 @@ public function contains($key, $operator = null, $value = null)
return in_array($key, $this->items);
}

if (func_num_args() == 2) {
$value = $operator;

$operator = '=';
}

return $this->contains($this->operatorForWhere($key, $operator, $value));
return $this->contains($this->operatorForWhere(...func_get_args()));
}

/**
Expand Down Expand Up @@ -275,9 +269,11 @@ public function crossJoin(...$lists)
*
* @return void
*/
public function dd()
public function dd(...$args)
{
dd($this->all());
call_user_func_array([$this, 'dump'], $args);

die(1);
}

/**
Expand Down Expand Up @@ -383,24 +379,22 @@ public function every($key, $operator = null, $value = null)
return true;
}

if (func_num_args() == 2) {
$value = $operator;

$operator = '=';
}

return $this->every($this->operatorForWhere($key, $operator, $value));
return $this->every($this->operatorForWhere(...func_get_args()));
}

/**
* Get all items except for those with the specified keys.
*
* @param mixed $keys
* @param \Illuminate\Support\Collection|mixed $keys
* @return static
*/
public function except($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
if ($keys instanceof self) {
$keys = $keys->keys()->all();
} elseif (! is_array($keys)) {
$keys = func_get_args();
}

return new static(Arr::except($this->items, $keys));
}
Expand Down Expand Up @@ -431,9 +425,9 @@ public function filter(callable $callback = null)
public function when($value, callable $callback, callable $default = null)
{
if ($value) {
return $callback($this);
return $callback($this, $value);
} elseif ($default) {
return $default($this);
return $default($this, $value);
}

return $this;
Expand Down Expand Up @@ -462,13 +456,7 @@ public function unless($value, callable $callback, callable $default = null)
*/
public function where($key, $operator, $value = null)
{
if (func_num_args() == 2) {
$value = $operator;

$operator = '=';
}

return $this->filter($this->operatorForWhere($key, $operator, $value));
return $this->filter($this->operatorForWhere(...func_get_args()));
}

/**
Expand All @@ -479,27 +467,37 @@ public function where($key, $operator, $value = null)
* @param mixed $value
* @return \Closure
*/
protected function operatorForWhere($key, $operator, $value)
protected function operatorForWhere($key, $operator, $value = null)
{
if (func_num_args() == 2) {
$value = $operator;

$operator = '=';
}

return function ($item) use ($key, $operator, $value) {
$retrieved = data_get($item, $key);

try {
switch ($operator) {
default:
case '=':
case '==': return $retrieved == $value;
case '!=':
case '<>': return $retrieved != $value;
case '<': return $retrieved < $value;
case '>': return $retrieved > $value;
case '<=': return $retrieved <= $value;
case '>=': return $retrieved >= $value;
case '===': return $retrieved === $value;
case '!==': return $retrieved !== $value;
}
} catch (Exception $e) {
return false;
$strings = array_filter([$retrieved, $value], function ($value) {
return is_string($value) || (is_object($value) && method_exists($value, '__toString'));
});

if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) {
return in_array($operator, ['!=', '<>', '!==']);
}

switch ($operator) {
default:
case '=':
case '==': return $retrieved == $value;
case '!=':
case '<>': return $retrieved != $value;
case '<': return $retrieved < $value;
case '>': return $retrieved > $value;
case '<=': return $retrieved <= $value;
case '>=': return $retrieved >= $value;
case '===': return $retrieved === $value;
case '!==': return $retrieved !== $value;
}
};
}
Expand Down Expand Up @@ -586,6 +584,19 @@ public function first(callable $callback = null, $default = null)
return Arr::first($this->items, $callback, $default);
}

/**
* Get the first item by the given key value pair.
*
* @param string $key
* @param mixed $operator
* @param mixed $value
* @return static
*/
public function firstWhere($key, $operator, $value = null)
{
return $this->first($this->operatorForWhere(...func_get_args()));
}

/**
* Get a flattened array of the items in the collection.
*
Expand Down Expand Up @@ -1597,9 +1608,9 @@ public function jsonSerialize()
return json_decode($value->toJson(), true);
} elseif ($value instanceof Arrayable) {
return $value->toArray();
} else {
return $value;
}

return $value;
}, $this->items);
}

Expand Down
46 changes: 46 additions & 0 deletions src/Illuminate/Support/HtmlString.php
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();
}
}
65 changes: 61 additions & 4 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 07d58f7

Please sign in to comment.