Skip to content

Commit

Permalink
Added fill method
Browse files Browse the repository at this point in the history
  • Loading branch information
maxalmonte14 committed Nov 18, 2018
1 parent a42b2ad commit fb1b05a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/PHPCollections-Collections-BaseCollection.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Determines if two collections are equal.

void PHPCollections\Collections\BaseCollection::fill()

Fills the collection with data.
Fills the collection with a set of data.

* Visibility: **public**

Expand Down
2 changes: 1 addition & 1 deletion src/Collections/BaseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function exists($offset): bool
abstract public function equals(self $collection): bool;

/**
* Fills the collection with data.
* Fills the collection with a set of data.
*
* @param array $data
*
Expand Down
14 changes: 14 additions & 0 deletions src/Collections/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ public function equals(BaseCollection $newDictionary): bool
return $this->toArray() == $newDictionary->toArray();
}

/**
* Fills the dictionary with data.
*
* @param array $data
*
* @return void
*/
public function fill(array $data): void
{
foreach ($data as $key => $entry) {
$this->add($key, $entry);
}
}

/**
* Filters the collection applying
* a given callback.
Expand Down
6 changes: 3 additions & 3 deletions src/Collections/GenericList.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public function __construct(string $type, object ...$data)
/**
* Adds a new object to the collection.
*
* @param mixed $value
* @param object $value
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function add($value): void
public function add(object $value): void
{
Checker::objectIsOfType($value, $this->type, sprintf($this->error, get_class($value)));

Expand Down Expand Up @@ -105,7 +105,7 @@ public function diff(BaseCollection $newGenericList): BaseCollection
*
* @param \PHPCollections\Collections\GenericList $newGenericList
*
* @return \PHPCollections\Collections\GenericList
* @return bool
*/
public function equals(BaseCollection $newGenericList): bool
{
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/ArrayListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,15 @@ public function canNotSumANonNumericFieldOfTheArrayList()
return $name;
}); // Here an InvalidOperationException is thrown!
}

/** @test */
public function itCanFillAnArrayListWithData()
{
$this->arrayList->fill([
'first_value',
'second_value',
]);

$this->assertCount(7, $this->arrayList);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/DictionaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,18 @@ public function canNotSumANonNumericFieldOfTheDictionary()
return $pair->getKey();
}); // Here an InvalidOperationException is thrown!
}

/** @test */
public function itCanFillADictionarytWithData()
{
$this->dictionary->fill([
'first_key' => 'first_value',
'second_key' => 'second_value',
]);

$this->assertCount(9, $this->dictionary);
$this->expectException('\InvalidArgumentException');
$this->expectExceptionMessage('The key type specified for this dictionary is string, you cannot pass an integer');
$this->dictionary->fill([0 => true]);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/GenericListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,18 @@ public function canNotSumANonNumericFieldOfTheGenericList()
return $arrayObject->offsetGet('name');
}); // Here an InvalidOperationException is thrown!
}

/** @test */
public function itCanFillAGenericListWithData()
{
$this->list->fill([
new ArrayObject(['name' => 'Max']),
new ArrayObject(['name' => 'John']),
]);

$this->assertCount(11, $this->list);
$this->expectException('\InvalidArgumentException');
$this->expectExceptionMessage('The type specified for this collection is ArrayObject, you cannot pass an object of type stdClass');
$this->list->fill([new StdClass()]);
}
}

0 comments on commit fb1b05a

Please sign in to comment.