X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=vendor%2Finstaclick%2Fphp-webdriver%2Flib%2FWebDriver%2FStorage.php;fp=vendor%2Finstaclick%2Fphp-webdriver%2Flib%2FWebDriver%2FStorage.php;h=d4586ff956373df44cef3ee316845c41f108d949;hb=1270d9129ce8f27c9b28b10518e32132c58e0aca;hp=0000000000000000000000000000000000000000;hpb=c27c0f0cdaa3f354b1fe54a56ae7e854be6e3f68;p=yaffs-website diff --git a/vendor/instaclick/php-webdriver/lib/WebDriver/Storage.php b/vendor/instaclick/php-webdriver/lib/WebDriver/Storage.php new file mode 100644 index 000000000..d4586ff95 --- /dev/null +++ b/vendor/instaclick/php-webdriver/lib/WebDriver/Storage.php @@ -0,0 +1,146 @@ + + */ + +namespace WebDriver; + +use WebDriver\Exception as WebDriverException; + +/** + * WebDriver\Storage class + * + * @package WebDriver + * + * @method mixed getKey($key) Get key/value pair. + * @method void deleteKey($key) Delete a specific key. + * @method integer size() Get the number of items in the storage. + */ +abstract class Storage extends AbstractWebDriver +{ + /** + * {@inheritdoc} + */ + protected function methods() + { + return array( + 'key' => array('GET', 'DELETE'), + 'size' => array('GET'), + ); + } + + /** + * Get all keys from storage or a specific key/value pair + * + * @return mixed + */ + public function get() + { + // get all keys + if (func_num_args() === 0) { + $result = $this->curl('GET', ''); + + return $result['value']; + } + + // get key/value pair + if (func_num_args() === 1) { + return $this->getKey(func_get_arg(0)); + } + + throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS); + } + + /** + * Set specific key/value pair + * + * @return \WebDriver\Storage + * + * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters + */ + public function set() + { + if (func_num_args() === 1 + && is_array($arg = func_get_arg(0)) + ) { + $this->curl('POST', '', $arg); + + return $this; + } + + if (func_num_args() === 2) { + $arg = array( + 'key' => func_get_arg(0), + 'value' => func_get_arg(1), + ); + $this->curl('POST', '', $arg); + + return $this; + } + + throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS); + } + + /** + * Delete storage or a specific key + * + * @return \WebDriver\Storage + * + * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters + */ + public function delete() + { + // delete storage + if (func_num_args() === 0) { + $this->curl('DELETE', ''); + + return $this; + } + + // delete key from storage + if (func_num_args() === 1) { + return $this->deleteKey(func_get_arg(0)); + } + + throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS); + } + + /** + * Factory method to create Storage objects + * + * @param string $type 'local' or 'session' storage + * @param string $url URL + * + * @return \WebDriver\Storage + */ + public static function factory($type, $url) + { + // dynamically define custom storage classes + $className = ucfirst(strtolower($type)); + $namespacedClassName = __CLASS__ . '\\' . $className; + + if (!class_exists($namespacedClassName, false)) { + eval( + 'namespace ' . __CLASS__ . '; final class ' . $className . ' extends \\' . __CLASS__ . '{}' + ); + } + + return new $namespacedClassName($url); + } +}