Skip to content

Commit

Permalink
Updated docblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
maxalmonte14 committed Sep 21, 2018
1 parent 789bda7 commit 708db53
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 106 deletions.
75 changes: 49 additions & 26 deletions src/Collections/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
class ArrayList extends BaseCollection
{
/**
* Add a new element to the collection
* Add a new element to the collection.
*
* @param mixed $val
*
* @return void
*/
public function add($val)
Expand All @@ -22,9 +23,10 @@ public function add($val)

/**
* Check if the collection
* contains a given value
* contains a given value.
*
* @param mixed $val
*
* @return bool
*/
public function contains($val)
Expand All @@ -40,63 +42,72 @@ public function contains($val)

/**
* Return all the coincidences found
* for the given callback or null
* for the given callback or null.
*
* @param callable $callback
* @return PHPCollections\ArrayList|null
* @return \PHPCollections\ArrayList|null
*/
public function filter(callable $callback)
{
$matcheds = [];

foreach ($this->data as $value) {
if (call_user_func($callback, $value) === true) {
$matcheds[] = $value;
}
}

return count($matcheds) > 0 ? new $this(array_values($matcheds)) : null;
}

/**
/**
* Search one or more elements in
* the collection
* the collection.
*
* @param callable $callback
* @param boolean $shouldStop
* @return PHPCollections\ArrayList|null
*
* @return \PHPCollections\ArrayList|null
*/
public function find(callable $callback, $shouldStop = false)
{
$matcheds = [];

foreach ($this->data as $key => $item) {
if ($callback($item, $key) === true) {
$matcheds[] = $item;

if ($shouldStop) {
break;
}
}
}

return count($matcheds) > 0 ? new $this($matcheds) : null;
}

/**
* Get the first element of the collection
* Get the first element of the collection.
*
* @throws OutOfRangeException
* @throws \OutOfRangeException
*
* @return mixed
*/
public function first()
{
if ($this->count() == 0) {
throw new OutOfRangeException("You're trying to get data into an empty collection.");
}

return $this->data[0];
}

/**
* Get the element specified
* at the given index
* at the given index.
*
* @param int $offset
* @param int $offset
*
* @return mixed
*/
public function get($offset)
Expand All @@ -105,38 +116,43 @@ public function get($offset)
}

/**
* Get the last element of the collection
* Get the last element of the collection.
*
* @throws OutOfRangeException
* @throws \OutOfRangeException
*
* @return mixed
*/
public function last()
{
if ($this->count() == 0) {
throw new OutOfRangeException("You're trying to get data into an empty collection.");
}

return $this->data[$this->count() - 1];
}

/**
* Update elements in the collection by
* applying a given callback function
* applying a given callback function.
*
* @param callable $callback
* @return PHPCollections\ArrayList|null
* @param callable $callback
*
* @return \PHPCollections\ArrayList|null
*/
public function map(callable $callback)
{
$matcheds = array_map($callback, $this->data);

return count($matcheds) > 0 ? new $this(array_values($matcheds)) : null;
}

/**
* Merge new data with the actual
* collection and returns a new one
* collection and returns a new one.
*
* @param array $data
* @return PHPCollections\ArrayList
* @param array $data
*
* @return \PHPCollections\ArrayList
*/
public function merge(array $data)
{
Expand All @@ -147,24 +163,28 @@ public function merge(array $data)
* Return a random element of
* the collection
*
* @throws PHPCollections\Exceptions\InvalidOperationException
* @throws \PHPCollections\Exceptions\InvalidOperationException
*
* @return mixed
*/
public function rand()
{
if ($this->count() == 0) {
throw new InvalidOperationException('You cannot get a random element from an empty collection.');
}

$randomIndex = array_rand($this->data);

return $this->get($randomIndex);
}

/**
* Return a new collection with the
* reversed values
* reversed values.
*
* @throws PHPCollections\Exceptions\InvalidOperationException
* @return PHPCollections\ArrayList
* @throws \PHPCollections\Exceptions\InvalidOperationException
*
* @return \PHPCollections\ArrayList
*/
public function reverse()
{
Expand All @@ -176,18 +196,21 @@ public function reverse()

/**
* Update the value of the element
* at the given index
* at the given index.
*
* @param int $index
* @param mixed $value
* @param int $index
* @param mixed $value
*
* @return bool
*/
public function update($index, $value)
{
if (!$this->exists($index)) {
throw new InvalidOperationException('You cannot update a non-existent value');
}

$this->data[$index] = $value;

return $this->data[$index] === $value;
}
}
49 changes: 26 additions & 23 deletions src/Collections/BaseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,29 @@

/**
* The base for iterable, countable
* and arrayable collections
* and arrayable collections.
*/
abstract class BaseCollection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
{

/**
* The data container
* The data container.
*
* @var array
*/
protected $data;

/**
* Represents the index of the data array
* Represents the index of the data array.
*
* @var integer
*/
private $position = 0;

/**
* BaseCollection constructor
* BaseCollection constructor.
*
* @param array $data
*
* @return void
*/
public function __construct(array $data = [])
{
Expand All @@ -43,7 +41,7 @@ public function __construct(array $data = [])

/**
* Remove all the elements
* of the data array
* of the data array.
*
* @return void
*/
Expand All @@ -53,7 +51,7 @@ public function clear()
}

/**
* Return the length of the collection
* Return the length of the collection.
*
* @return int
*/
Expand All @@ -64,9 +62,10 @@ public function count()

/**
* Check if the given index
* exists in the collection
* exists in the collection.
*
* @param int $offset
* @param int $offset
*
* @return bool
*/
public function exists($offset)
Expand All @@ -76,9 +75,9 @@ public function exists($offset)

/**
* Return an array iterator for
* the data array
* the data array.
*
* @return ArrayIterator
* @return \ArrayIterator
*/
public function getIterator()
{
Expand All @@ -87,7 +86,7 @@ public function getIterator()

/**
* Defines the behavior of the collection
* when json_encode is called
* when json_encode is called.
*
* @return array
*/
Expand All @@ -97,9 +96,10 @@ public function jsonSerialize()
}

/**
* Check if an offset exists in the collection
* Check if an offset exists in the collection.
*
* @param int $offset
* @param int $offset
*
* @return bool
*/
public function offsetExists($offset)
Expand All @@ -108,9 +108,10 @@ public function offsetExists($offset)
}

/**
* Get a value in the collection
* Get a value from the collection.
*
* @param int $offset
* @param int $offset
* @return mixed|null
*/
public function offsetGet($offset)
Expand All @@ -119,10 +120,11 @@ public function offsetGet($offset)
}

/**
* Set a value in the collection
* Set a value in the collection.
*
* @param int $offset
* @param mixed $value
* @param int $offset
* @param mixed $value
*
* @return void
*/
public function offsetSet($offset, $value)
Expand All @@ -135,9 +137,10 @@ public function offsetSet($offset, $value)
}

/**
* Unset an offset in the collection
* Unset an offset in the collection.
*
* @param int $offset
* @param int $offset
*
* @return void
*/
public function offsetUnset($offset)
Expand All @@ -147,7 +150,7 @@ public function offsetUnset($offset)

/**
* Returns a plain array with
* your dictionary data
* your dictionary data.
*
* @return array
*/
Expand Down
Loading

0 comments on commit 708db53

Please sign in to comment.