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