public function add($key, $object, $lifetime = 0)
{
// check if a key has been passed
if (is_null($key)) {
throw new NullPointerException('Passed key is null');
}
// check if lifetime is of type integer
if (is_integer($lifetime) === false) {
throw new InvalidLifetimeException(sprintf('Passed lifetime must be an integer, but is %s instead', $lifetime));
}
// check if a primitive datatype is passed
if (is_integer($key) || is_string($key) || is_double($key) || is_bool($key)) {
// add the item and lifetime to the array
$this->items[$key] = $object;
// add a lifetime if passed lifetime > 0
if ($lifetime > 0) {
$this->lifetime[$key] = time() + $lifetime;
}
// and return
return;
}
// 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');
}
// add the item and lifetime to the array
$this->items[$newKey] = $object;
// add a lifetime if passed lifetime > 0
if ($lifetime > 0) {
$this->lifetime[$newKey] = time() + $lifetime;
}
// and return
return;
}
throw new InvalidKeyException('Passed key has to be a primitive datatype or has to implement the __toString() method');
}