Skip to content

Commit

Permalink
replace Mutable::push with Mutable::add method, solve Laravel version…
Browse files Browse the repository at this point in the history
… compatibility
  • Loading branch information
vitalyrotari authored and endihunter committed Oct 7, 2020
1 parent aa75174 commit 868402b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
22 changes: 10 additions & 12 deletions src/Collection/Mutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,21 @@
class Mutable extends BaseCollection
{
/**
* Push an item onto the end of the collection.
* Add an element to the collection.
*
* @param mixed $values [optional]
* @param mixed $element
* @param Closure $callback
* @return $this
*/
public function push(...$values)
public function add($element, Closure $callback = null)
{
$element = $values[0] ?? null;
$callback = $values[1] ?? null;

$element = $this->createElement($element);

if ($callback instanceof Closure) {
if ($callback) {
$callback($element);
}

parent::push($element);
parent::add($element);

return $this;
}
Expand All @@ -51,13 +49,13 @@ public function insert($element, $position, Closure $callback = null): self
}

if (\is_string($position)) {
$this->push($element);
$this->add($element);

return $this->move($element->id(), $position);
}

if ($position >= $this->count()) {
return $this->push($element);
return $this->add($element);
}

if (0 === $position) {
Expand Down Expand Up @@ -267,7 +265,7 @@ public function group(string $id, Closure $callback): self

$callback($group);

$this->push($group);
$this->add($group);

return $this;
}
Expand Down Expand Up @@ -295,7 +293,7 @@ public function stack(array $elements, string $groupId, $position = null): self
if ($position) {
$this->insert($group, $position);
} else {
$this->push($group);
$this->add($group);
}

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Module/HasFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ trait HasFilters
*/
public function addFilter(Filter $filter)
{
$this->filters->push($filter);
$this->filters->add($filter);

return $this;
}
Expand All @@ -55,7 +55,7 @@ public function addFilter(Filter $filter)
*/
public function addScope(Scope $scope)
{
$this->scopes->push($scope);
$this->scopes->add($scope);

return $this;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Traits/Module/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ protected function scaffoldForm()

foreach (options_fetch() as $option) {
$element = Text::make($option->key)->setValue($option->value);

$collection->push(
$element
);
$collection->add($element);
}

return $collection;
Expand Down
8 changes: 4 additions & 4 deletions tests/Collection/MutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function it_initializes_a_collection()
/** @test */
public function it_pushes_an_element()
{
$this->collection->push('test', function ($e) {
$this->collection->add('test', function ($e) {
return $e;
});

Expand Down Expand Up @@ -293,8 +293,8 @@ protected function makeElementsCollection()
$collection = (new Mutable());

return $collection
->push($this->e('first'))
->push($this->e('second'))
->push($this->e('third'));
->add($this->e('first'))
->add($this->e('second'))
->add($this->e('third'));
}
}
8 changes: 4 additions & 4 deletions tests/Form/Collection/FormCollectionMutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setUp()
public function it_creates_a_form_element_from_string_id()
{
$collection = new Mutable();
$collection->push('title');
$collection->add('title');

$this->assertCount(1, $collection->all());
$this->assertInstanceOf(Text::class, $collection->find('title'));
Expand Down Expand Up @@ -60,8 +60,8 @@ public function it_creates_a_form_element_from_object()
$collection = new Mutable();

$collection
->push($title = Text::make('title'))
->push($body = Textarea::make('body'));
->add($title = Text::make('title'))
->add($body = Textarea::make('body'));

$this->assertCount(2, $collection->all());
$this->assertSame([$title, $body], $collection->all());
Expand All @@ -79,7 +79,7 @@ public function it_creates_a_form_element_from_object()
*/
public function it_allows_editing_of_new_created_element($collection)
{
$collection->push(
$collection->add(
$desc = Textarea::make('description'),
function (Textarea $element) {
$element->setTitle('New description');
Expand Down

0 comments on commit 868402b

Please sign in to comment.