Skip to content

Commit

Permalink
PSR-2 coding styles applied
Browse files Browse the repository at this point in the history
  • Loading branch information
maxalmonte14 committed Nov 2, 2017
1 parent 2eb1c2b commit 9b6e4db
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 87 deletions.
48 changes: 31 additions & 17 deletions src/Collections/ArrayList.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use PHPCollections\Exceptions\InvalidOperationException;

class ArrayList extends BaseCollection
/**
* A list of values of any type
*/
class ArrayList extends BaseCollection
{
/**
* Add a new element to the collection
Expand All @@ -26,33 +29,37 @@ public function add($val)
*/
public function contains($val)
{
foreach ($this->data as $value)
if ($value === $val) return true;
foreach ($this->data as $value) {
if ($value === $val) {
return true;
}
}

return false;
}

/**
* Return all the coincidences found
* for the given callback or null
*
*
* @param callable $callback
* @return PHPCollections\ArrayList|null
*/
public function filter(callable $callback)
{
$matcheds = [];
foreach ($this->data as $value) {
if (call_user_func($callback, $value) === true)
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
*
*
* @param callable $callback
* @param boolean $shouldStop
* @return PHPCollections\ArrayList|null
Expand All @@ -63,7 +70,9 @@ public function find(callable $callback, $shouldStop = false)
foreach ($this->data as $key => $item) {
if ($callback($item, $key) === true) {
$matcheds[] = $item;
if ($shouldStop) break;
if ($shouldStop) {
break;
}
}
}
return count($matcheds) > 0 ? new $this($matcheds) : null;
Expand All @@ -77,13 +86,14 @@ public function find(callable $callback, $shouldStop = false)
*/
public function first()
{
if ($this->count() == 0)
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
* Get the element specified
* at the given index
*
* @param int $offset
Expand All @@ -102,8 +112,9 @@ public function get($offset)
*/
public function last()
{
if ($this->count() == 0)
if ($this->count() == 0) {
throw new OutOfRangeException("You're trying to get data into an empty collection.");
}
return $this->data[$this->count() - 1];
}

Expand Down Expand Up @@ -133,36 +144,38 @@ public function merge(array $data)
}

/**
* Return a random element of
* Return a random element of
* the collection
*
* @throws PHPCollections\Exceptions\InvalidOperationException
* @return mixed
*/
public function rand()
{
if ($this->count() == 0)
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
*
*
* @throws PHPCollections\Exceptions\InvalidOperationException
* @return PHPCollections\ArrayList
*/
public function reverse()
{
if ($this->count() == 0)
if ($this->count() == 0) {
throw new InvalidOperationException('You cannot reverse an empty collection.');
}
return new $this(array_reverse($this->data));
}

/**
* Update the value of the element
* Update the value of the element
* at the given index
*
* @param int $index
Expand All @@ -171,9 +184,10 @@ public function reverse()
*/
public function update($index, $value)
{
if (!$this->exists($index))
if (!$this->exists($index)) {
throw new InvalidOperationException('You cannot update a non-existent value');
}
$this->data[$index] = $value;
return $this->data[$index] === $value;
}
}
}
22 changes: 14 additions & 8 deletions src/Collections/BaseCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
use JsonSerializable;
use ArrayIterator;

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

/**
* The data container
Expand All @@ -26,7 +31,7 @@ abstract class BaseCollection implements ArrayAccess, Countable, IteratorAggrega

/**
* BaseCollection constructor
*
*
* @param array $data
*
* @return void
Expand All @@ -37,7 +42,7 @@ public function __construct(array $data = [])
}

/**
* Remove all the elements
* Remove all the elements
* of the data array
*
* @return void
Expand All @@ -58,7 +63,7 @@ public function count()
}

/**
* Check if the given index
* Check if the given index
* exists in the collection
*
* @param int $offset
Expand All @@ -72,7 +77,7 @@ public function exists($offset)
/**
* Return an array iterator for
* the data array
*
*
* @return ArrayIterator
*/
public function getIterator()
Expand All @@ -83,7 +88,7 @@ public function getIterator()
/**
* Defines the behavior of the collection
* when json_encode is called
*
*
* @return array
*/
public function jsonSerialize()
Expand Down Expand Up @@ -122,10 +127,11 @@ public function offsetGet($offset)
*/
public function offsetSet($offset, $value)
{
if (is_null($offset))
if (is_null($offset)) {
array_push($this->data, $value);
else
} else {
$this->data[$offset] = $value;
}
}

/**
Expand Down
44 changes: 30 additions & 14 deletions src/Collections/Dictionary.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
use InvalidArgumentException;
use PHPCollections\Exceptions\InvalidOperationException;

/**
* A Pair object collection
* represented by a generic
* type key and value
*/
class Dictionary extends BaseCollection
{
/**
* The type of the keys
* for this dictionary
*
*
* @var mixed
*/
private $keyType;
Expand All @@ -35,8 +40,9 @@ public function __construct($keyType, $valueType, array $data = [])
$this->keyType = $keyType;
$this->valueType = $valueType;

foreach ($data as $key => $value)
foreach ($data as $key => $value) {
$this->data[$key] = new Pair($key, $value);
}
}

/**
Expand All @@ -58,7 +64,7 @@ public function add($key, $value)
* attribute, if not throws and Exception
*
* @param mixed $data
* @throws InvalidArgumentException
* @throws InvalidArgumentException
* @return void
*/
private function checkType(array $values)
Expand All @@ -85,8 +91,9 @@ public function filter(callable $callback)
{
$matcheds = [];
foreach ($this->data as $key => $value) {
if (call_user_func($callback, $value->getKey(), $value->getValue()) === true)
if (call_user_func($callback, $value->getKey(), $value->getValue()) === true) {
$matcheds[$value->getKey()] = $value->getValue();
}
}
return count($matcheds) > 0 ? new $this($this->keyType, $this->valueType, $matcheds) : null;
}
Expand Down Expand Up @@ -116,9 +123,12 @@ public function find(callable $callback)
*/
public function first()
{
if ($this->count() == 0)
if ($this->count() == 0) {
throw new InvalidOperationException('You cannot get the first element of an empty collection');
foreach ($this->data as $key => $value) return $this->get($key);
}
foreach ($this->data as $key => $value) {
return $this->get($key);
}
}

/**
Expand Down Expand Up @@ -162,8 +172,9 @@ public function getValueType()
*/
public function last()
{
if ($this->count() == 0)
if ($this->count() == 0) {
throw new InvalidOperationException('You cannot get the last element of an empty collection');
}
$values = array_values($this->toArray());
return $values[$this->count() - 1];
}
Expand All @@ -189,8 +200,9 @@ public function map(callable $callback)
*/
public function merge(Dictionary $newDictionary)
{
foreach ($newDictionary->toArray() as $key => $value)
foreach ($newDictionary->toArray() as $key => $value) {
$this->checkType(['key' => $key, 'value' => $value]);
}
return new $this($this->keyType, $this->valueType, array_merge($this->data, $newDictionary->toArray()));
}

Expand All @@ -203,14 +215,16 @@ public function merge(Dictionary $newDictionary)
public function remove($key)
{
$exits = $this->offsetExists($key);
if ($exits) $this->offsetUnset($key);
if ($exits) {
$this->offsetUnset($key);
}
return $exits;
}

/**
* Sort collection data by values
* applying a given callback
*
*
* @param callable $callback
* @return bool
*/
Expand All @@ -228,8 +242,9 @@ public function sort(callable $callback)
public function toArray()
{
$array = [];
foreach ($this->data as $pair)
foreach ($this->data as $pair) {
$array[$pair->getKey()] = $pair->getValue();
}
return $array;
}

Expand All @@ -245,7 +260,7 @@ public function toJson()
}

/**
* Update the value of one Pair
* Update the value of one Pair
* in the collection
*
* @param mixed $key
Expand All @@ -255,9 +270,10 @@ public function toJson()
public function update($key, $value)
{
$this->checkType(['key' => $key, 'value' => $value]);
if (!$this->offsetExists($key))
if (!$this->offsetExists($key)) {
throw new InvalidOperationException('You cannot update a non-existent value');
}
$this->data[$key]->setValue($value);
return $this->data[$key]->getValue() === $value;
}
}
}
Loading

0 comments on commit 9b6e4db

Please sign in to comment.