3 * Copyright 2011-2017 Anthon Pang. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @author Anthon Pang <apang@softwaredevelopment.ca>
24 use WebDriver\Exception as WebDriverException;
27 * WebDriver\Storage class
31 * @method mixed getKey($key) Get key/value pair.
32 * @method void deleteKey($key) Delete a specific key.
33 * @method integer size() Get the number of items in the storage.
35 abstract class Storage extends AbstractWebDriver
40 protected function methods()
43 'key' => array('GET', 'DELETE'),
44 'size' => array('GET'),
49 * Get all keys from storage or a specific key/value pair
56 if (func_num_args() === 0) {
57 $result = $this->curl('GET', '');
59 return $result['value'];
63 if (func_num_args() === 1) {
64 return $this->getKey(func_get_arg(0));
67 throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
71 * Set specific key/value pair
73 * @return \WebDriver\Storage
75 * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters
79 if (func_num_args() === 1
80 && is_array($arg = func_get_arg(0))
82 $this->curl('POST', '', $arg);
87 if (func_num_args() === 2) {
89 'key' => func_get_arg(0),
90 'value' => func_get_arg(1),
92 $this->curl('POST', '', $arg);
97 throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
101 * Delete storage or a specific key
103 * @return \WebDriver\Storage
105 * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters
107 public function delete()
110 if (func_num_args() === 0) {
111 $this->curl('DELETE', '');
116 // delete key from storage
117 if (func_num_args() === 1) {
118 return $this->deleteKey(func_get_arg(0));
121 throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
125 * Factory method to create Storage objects
127 * @param string $type 'local' or 'session' storage
128 * @param string $url URL
130 * @return \WebDriver\Storage
132 public static function factory($type, $url)
134 // dynamically define custom storage classes
135 $className = ucfirst(strtolower($type));
136 $namespacedClassName = __CLASS__ . '\\' . $className;
138 if (!class_exists($namespacedClassName, false)) {
140 'namespace ' . __CLASS__ . '; final class ' . $className . ' extends \\' . __CLASS__ . '{}'
144 return new $namespacedClassName($url);