Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
13 / 13 |
CRAP | |
100.00% |
24 / 24 |
IterableCollection | |
100.00% |
1 / 1 |
|
100.00% |
14 / 14 |
17 | |
100.00% |
24 / 24 |
current() | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getIterableCollection() | |
100.00% |
1 / 1 |
1 | ||||||
key() | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
next() | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
rewind() | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
valid() | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getAll() | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
getFirst() | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
isEmpty() | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
count() | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
offsetExists($offset) | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
offsetGet($offset) | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
offsetSet($offset, $value) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
offsetUnset($offset) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
<?php | |
namespace TriNetAssess; | |
/** | |
* Class IterableCollection | |
* | |
* @package TriNetAssess | |
*/ | |
abstract class IterableCollection | |
extends JsonSerializableObject | |
implements \Iterator, \ArrayAccess, \Countable | |
{ | |
/** | |
* The current array position | |
* | |
* @var integer | |
*/ | |
private $iterableCollectionPosition = 0; | |
/** | |
* Return the current element | |
* | |
* @return mixed Can return any type. | |
*/ | |
public function current() | |
{ | |
return $this->getIterableCollection()[$this->iterableCollectionPosition]; | |
} | |
/** | |
* Returns the iterable collection | |
* | |
* @return array | |
*/ | |
abstract protected function getIterableCollection(); | |
/** | |
* Return the key of the current element | |
* | |
* @return mixed scalar on success, or null on failure. | |
*/ | |
public function key() | |
{ | |
return $this->iterableCollectionPosition; | |
} | |
/** | |
* Move forward to next element | |
* | |
* @return void Any returned value is ignored. | |
*/ | |
public function next() | |
{ | |
++$this->iterableCollectionPosition; | |
} | |
/** | |
* Rewind the Iterator to the first element | |
* | |
* @return void Any returned value is ignored. | |
*/ | |
public function rewind() | |
{ | |
$this->iterableCollectionPosition = 0; | |
} | |
/** | |
* Checks if current position is valid | |
* | |
* @return bool The return value will be casted to boolean and then evaluated. | |
*/ | |
public function valid() | |
{ | |
return isset($this->getIterableCollection()[$this->iterableCollectionPosition]); | |
} | |
/** | |
* Return the entire collection | |
* | |
* @return array | |
*/ | |
public function getAll() | |
{ | |
$collection = $this->getIterableCollection(); | |
if (!is_array($collection)) { | |
return []; | |
} | |
return $collection; | |
} | |
/** | |
* Return the first element of the collection | |
* | |
* @return mixed | |
*/ | |
public function getFirst() | |
{ | |
if ($this->isEmpty()) { | |
throw new \OutOfBoundsException("Object collection is empty"); | |
} | |
return $this->getIterableCollection()[0]; | |
} | |
/** | |
* Whether a collection is empty | |
* | |
* @return bool true on success or false on failure. | |
*/ | |
public function isEmpty() | |
{ | |
$collection = $this->getIterableCollection(); | |
return ( | |
!is_array($collection) | |
|| !count($collection) | |
); | |
} | |
/** | |
* Count elements of an object | |
* | |
* @return int The custom count as an integer. | |
*/ | |
public function count() | |
{ | |
return count($this->getIterableCollection()); | |
} | |
/** | |
* Whether a offset exists | |
* | |
* @param mixed $offset An offset to check for. | |
* | |
* @return bool true on success or false on failure. | |
*/ | |
public function offsetExists($offset) | |
{ | |
return array_key_exists($offset, $this->getIterableCollection()); | |
} | |
/** | |
* Offset to retrieve | |
* | |
* @param mixed $offset The offset to retrieve. | |
* | |
* @return mixed Can return all value types. | |
*/ | |
public function offsetGet($offset) | |
{ | |
return $this->getIterableCollection()[$offset]; | |
} | |
/** | |
* Offset to set | |
* | |
* @param mixed $offset The offset to assign the value to. | |
* @param mixed $value The value to set. | |
* | |
* @return void | |
*/ | |
public function offsetSet($offset, $value) | |
{ | |
$this->getIterableCollection()[$offset] = $value; | |
} | |
/** | |
* Offset to unset | |
* | |
* @param mixed $offset The offset to unset. | |
* | |
* @return void | |
*/ | |
public function offsetUnset($offset) | |
{ | |
unset($this->getIterableCollection()[$offset]); | |
} | |
} |