public function get($key)
{
// check if a key has been passed
if (is_null($key)) {
throw new NullPointerException('Passed key is null');
}
// check if a primitive datatype is passed
if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) {
// return the value for the passed key, if it exists
if (array_key_exists($key, $this->items) && !$this->isTimedOut($key)) {
// item is available and NOT timed out
return $this->items[$key];
} elseif (array_key_exists($key, $this->items) && $this->isTimedOut($key)) {
// item is available, but timed out
return;
} else {
// item is generally not available
throw new IndexOutOfBoundsException(sprintf('Index %s out of bounds', $key));
}
}
// check if an object is passed
if (is_object($key)) {
if ($key instanceof String) {
$newKey = $key->stringValue();
} elseif ($key instanceof Float) {
$newKey = $key->floatValue();
} elseif ($key instanceof Integer) {
$newKey = $key->intValue();
} elseif ($key instanceof Boolean) {
$newKey = $key->booleanValue();
} elseif (method_exists($key, '__toString')) {
$newKey = $key->__toString();
} else {
throw new InvalidKeyException('Passed key has to be a primitive datatype or has to implement the __toString() method');
}
// return the value for the passed key, if it exists
if (array_key_exists($newKey, $this->items) && !$this->isTimedOut($newKey)) {
// item is available and NOT timed out
return $this->items[$newKey];
} elseif (array_key_exists($newKey, $this->items) && $this->isTimedOut($newKey)) {
// item is available, but timed out
return;
} else {
// item is generally not available
throw new IndexOutOfBoundsException(sprintf('Index %s out of bounds', $newKey));
}
}
throw new InvalidKeyException('Passed key has to be a primitive datatype or has to implement the __toString() method');
}