Chris@14: Chris@14: */ Chris@14: Chris@14: namespace WebDriver; Chris@14: Chris@14: use WebDriver\Exception as WebDriverException; Chris@14: Chris@14: /** Chris@14: * WebDriver\Storage class Chris@14: * Chris@14: * @package WebDriver Chris@14: * Chris@14: * @method mixed getKey($key) Get key/value pair. Chris@14: * @method void deleteKey($key) Delete a specific key. Chris@14: * @method integer size() Get the number of items in the storage. Chris@14: */ Chris@14: abstract class Storage extends AbstractWebDriver Chris@14: { Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: protected function methods() Chris@14: { Chris@14: return array( Chris@14: 'key' => array('GET', 'DELETE'), Chris@14: 'size' => array('GET'), Chris@14: ); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Get all keys from storage or a specific key/value pair Chris@14: * Chris@14: * @return mixed Chris@14: */ Chris@14: public function get() Chris@14: { Chris@14: // get all keys Chris@14: if (func_num_args() === 0) { Chris@14: $result = $this->curl('GET', ''); Chris@14: Chris@14: return $result['value']; Chris@14: } Chris@14: Chris@14: // get key/value pair Chris@14: if (func_num_args() === 1) { Chris@14: return $this->getKey(func_get_arg(0)); Chris@14: } Chris@14: Chris@14: throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Set specific key/value pair Chris@14: * Chris@14: * @return \WebDriver\Storage Chris@14: * Chris@14: * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters Chris@14: */ Chris@14: public function set() Chris@14: { Chris@14: if (func_num_args() === 1 Chris@14: && is_array($arg = func_get_arg(0)) Chris@14: ) { Chris@14: $this->curl('POST', '', $arg); Chris@14: Chris@14: return $this; Chris@14: } Chris@14: Chris@14: if (func_num_args() === 2) { Chris@14: $arg = array( Chris@14: 'key' => func_get_arg(0), Chris@14: 'value' => func_get_arg(1), Chris@14: ); Chris@14: $this->curl('POST', '', $arg); Chris@14: Chris@14: return $this; Chris@14: } Chris@14: Chris@14: throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Delete storage or a specific key Chris@14: * Chris@14: * @return \WebDriver\Storage Chris@14: * Chris@14: * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters Chris@14: */ Chris@14: public function delete() Chris@14: { Chris@14: // delete storage Chris@14: if (func_num_args() === 0) { Chris@14: $this->curl('DELETE', ''); Chris@14: Chris@14: return $this; Chris@14: } Chris@14: Chris@14: // delete key from storage Chris@14: if (func_num_args() === 1) { Chris@14: return $this->deleteKey(func_get_arg(0)); Chris@14: } Chris@14: Chris@14: throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS); Chris@14: } Chris@14: Chris@14: /** Chris@14: * Factory method to create Storage objects Chris@14: * Chris@14: * @param string $type 'local' or 'session' storage Chris@14: * @param string $url URL Chris@14: * Chris@14: * @return \WebDriver\Storage Chris@14: */ Chris@14: public static function factory($type, $url) Chris@14: { Chris@14: // dynamically define custom storage classes Chris@14: $className = ucfirst(strtolower($type)); Chris@14: $namespacedClassName = __CLASS__ . '\\' . $className; Chris@14: Chris@14: if (!class_exists($namespacedClassName, false)) { Chris@14: eval( Chris@14: 'namespace ' . __CLASS__ . '; final class ' . $className . ' extends \\' . __CLASS__ . '{}' Chris@14: ); Chris@14: } Chris@14: Chris@14: return new $namespacedClassName($url); Chris@14: } Chris@14: }