public function setItem($obj, $key = null)
    {
        if (!$key) {
            throw new KeyInvalidException("A key is required.");
        } elseif (!is_string($key)) {
            throw new KeyInvalidException("The key should be a string.");
        } else {
            if (!array_key_exists($key, $this->getItems())) {
                throw new KeyInvalidException("Invalid key $key.");
            }
            $this->items[$key] = $obj;
        }

        return $this;
    }