Skip to content

Commit

Permalink
Renamed: methods for better readability
Browse files Browse the repository at this point in the history
Added: additional check methods for CC
Added: array collection serialization support
Fix: added missing methods for immutable array
  • Loading branch information
sauls committed Feb 17, 2018
1 parent 5a368be commit ae817eb
Show file tree
Hide file tree
Showing 5 changed files with 319 additions and 47 deletions.
71 changes: 60 additions & 11 deletions src/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use function Sauls\Component\Helper\array_set_value;
use Sauls\Component\Helper\Exception\PropertyNotAccessibleException;

class ArrayCollection implements Collection
class ArrayCollection implements Collection, \Serializable
{
/**
* @var array
Expand Down Expand Up @@ -74,7 +74,7 @@ public function removeKey($key)
return array_remove_key($this->elements, $key, false);
}

public function removeElement($element)
public function removeValue($element)
{
return array_remove_value($this->elements, $element);
}
Expand Down Expand Up @@ -104,34 +104,82 @@ public function map(\Closure $function)
return \array_map($function, $this->elements);
}

public function hasKey($key): bool
public function keyOrValueExists($keyOrValue): bool
{
if ($this->keyExists($keyOrValue)) {
return true;
}

return $this->valueExists($keyOrValue);
}

public function keyOrValueDoesNotExists($keyOrValue): bool
{
return false === $this->keyOrValueExists($keyOrValue);
}

public function keyExists($key): bool
{
return array_key_exists($this->elements, $key);
}

public function hasValue($value): bool
public function keyDoesNotExists($key): bool
{
return false === $this->keyExists($key);
}

public function valueExists($value): bool
{
return empty(array_deep_search($this->elements, $value)) ? false : true;
}

public function valueDoesNotExists($value): bool
{
return false === $this->valueExists($value);
}

public function valueIsNull($key): bool
{
return null === $this->get($key);
}

public function valueIsNotNull($key): bool
{
return false === $this->valueIsNull($key);
}

public function isEmpty(): bool
{
return empty($this->elements);
}

public function __toString(): string
{
return __CLASS__ . '@' . spl_object_hash($this);
return __CLASS__ . '#' . $this->getHash();
}

public function getHash(): string
{
return md5($this->serialize());
}

/**
* @return string
*/
public function getSplHash(): string
{
return \spl_object_hash($this);
}


public function getIterator()
{
return new \ArrayIterator($this->elements);
}

public function offsetExists($offset)
{
return $this->hasKey($offset);
return $this->keyExists($offset);
}

/**
Expand All @@ -157,12 +205,13 @@ public function count(): int
return \count($this->elements);
}

public function has($keyOrValue): bool
public function serialize(): string
{
if ($this->hasKey($keyOrValue)) {
return true;
}
return \serialize($this->elements);
}

return $this->hasValue($keyOrValue);
public function unserialize($value): void
{
$this->elements = \unserialize($value);
}
}
15 changes: 11 additions & 4 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,22 @@ public function get($key, $default = null);
public function merge(array $elements): void;
public function replace(array $elements): void;
public function removeKey($key);
public function removeElement($element);
public function removeValue($element);
public function slice($offset, $length = null): array;
public function clear(): void;
public function all(): array;
public function filter(\Closure $function);
public function map(\Closure $function);
public function has($keyOrValue): bool;
public function hasKey($key): bool;
public function hasValue($value): bool;
public function keyOrValueExists($keyOrValue): bool;
public function keyOrValueDoesNotExists($keyOrValue): bool;
public function keyExists($key): bool;
public function keyDoesNotExists($key): bool;
public function valueExists($value): bool;
public function valueDoesNotExists($value): bool;
public function valueIsNull($key): bool;
public function valueIsNotNull($key): bool;
public function isEmpty(): bool;
public function __toString(): string;
public function getHash(): string;
public function getSplHash(): string;
}
32 changes: 31 additions & 1 deletion src/ImmutableArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,40 @@ public function removeKey($key)
/**
* @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException
*/
public function removeElement($element)
public function removeValue($element)
{
throw new UnsupportedOperationException();
}

/**
* @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException
*/
public function unserialize($value): void
{
throw new UnsupportedOperationException();
}

/**
* @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException
*/
public function map(\Closure $function)
{
throw new UnsupportedOperationException();
}

/**
* @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException
*/
public function offsetSet($offset, $value)
{
throw new UnsupportedOperationException();
}

/**
* @throws \Sauls\Component\Collection\Exception\UnsupportedOperationException
*/
public function offsetUnset($offset)
{
throw new UnsupportedOperationException();
}
}
Loading

0 comments on commit ae817eb

Please sign in to comment.