Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00%
0 / 1
0.00%
0 / 17
CRAP
0.00%
0 / 127
Maybe
0.00%
0 / 1
0.00%
0 / 17
1560
0.00%
0 / 127
 __construct($val = null)
0.00%
0 / 1
6
0.00%
0 / 8
 isAssocArray($arr)
0.00%
0 / 1
2
0.00%
0 / 4
 __toString()
0.00%
0 / 1
6
0.00%
0 / 7
 __get($key)
0.00%
0 / 1
6
0.00%
0 / 7
 __set($key, $val)
0.00%
0 / 1
6
0.00%
0 / 7
 __call($method, $args)
0.00%
0 / 1
6
0.00%
0 / 7
 offsetExists($offset)
0.00%
0 / 1
12
0.00%
0 / 7
 offsetGet($offset)
0.00%
0 / 1
12
0.00%
0 / 7
 offsetSet($offset, $value)
0.00%
0 / 1
12
0.00%
0 / 6
 offsetUnset($offset)
0.00%
0 / 1
12
0.00%
0 / 6
 __value()
0.00%
0 / 1
20
0.00%
0 / 13
 current()
0.00%
0 / 1
12
0.00%
0 / 12
 next()
0.00%
0 / 1
2
0.00%
0 / 4
 key()
0.00%
0 / 1
6
0.00%
0 / 9
 valid()
0.00%
0 / 1
12
0.00%
0 / 12
 rewind()
0.00%
0 / 1
6
0.00%
0 / 7
 jsonSerialize()
0.00%
0 / 1
2
0.00%
0 / 4
<?php
/**
* Author: davert
* Date: 30.07.12
*
* Class Maybe
* Represents either empty values or defined from results.
*
*/
namespace Codeception;
class Maybe implements \ArrayAccess, \Iterator, \JsonSerializable
{
protected $position = 0;
protected $val = null;
protected $assocArray = null;
function __construct($val = null)
{
$this->val = $val;
if (is_array($this->val)) {
$this->assocArray = $this->isAssocArray($this->val);
}
$this->position = 0;
}
private function isAssocArray($arr)
{
return array_keys($arr) !== range(0, count($arr) - 1);
}
function __toString()
{
if ($this->val === null) {
return "?";
}
return $this->val;
}
function __get($key)
{
if ($this->val === null) {
return new Maybe();
}
return $this->val->key;
}
function __set($key, $val)
{
if ($this->val === null) {
return;
}
$this->val->key = $val;
}
function __call($method, $args)
{
if ($this->val === null) {
return new Maybe();
}
return call_user_func_array(array($this->val, $method), $args);
}
public function offsetExists($offset)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
return isset($this->val[$offset]);
}
return false;
}
public function offsetGet($offset)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
return $this->val[$offset];
}
return new Maybe();
}
public function offsetSet($offset, $value)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
$this->val[$offset] = $value;
}
}
public function offsetUnset($offset)
{
if (is_array($this->val) or ($this->val instanceof \ArrayAccess)) {
unset($this->val[$offset]);
}
}
public function __value()
{
$val = $this->val;
if (is_array($val)) {
foreach ($val as $k => $v) {
if ($v instanceof self) {
$v = $v->__value();
}
$val[$k] = $v;
}
}
return $val;
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the current element
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
*/
public function current()
{
if (! is_array($this->val)) {
return null;
}
if ($this->assocArray) {
$keys = array_keys($this->val);
return $this->val[$keys[$this->position]];
} else {
return $this->val[$this->position];
}
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Move forward to next element
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
*/
public function next()
{
++$this->position;
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Return the key of the current element
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
*/
public function key()
{
if ($this->assocArray) {
$keys = array_keys($this->val);
return $keys[$this->position];
} else {
return $this->position;
}
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Checks if current position is valid
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then evaluated.
* Returns true on success or false on failure.
*/
public function valid()
{
if (! is_array($this->val)) {
return null;
}
if ($this->assocArray) {
$keys = array_keys($this->val);
return isset($keys[$this->position]);
} else {
return isset($this->val[$this->position]);
}
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Rewind the Iterator to the first element
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
*/
public function rewind()
{
if (is_array($this->val)) {
$this->assocArray = $this->isAssocArray($this->val);
}
$this->position = 0;
}
/**
* (PHP 5 >= 5.4.0)
* Serializes the object to a value that can be serialized natively by json_encode().
* @link http://docs.php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed Returns data which can be serialized by json_encode(), which is a value of any type other than a resource.
*/
public function jsonSerialize()
{
return $this->__value();
}
}