Chris@14
|
1 <?php
|
Chris@14
|
2 /**
|
Chris@14
|
3 * Copyright 2011-2017 Anthon Pang. All Rights Reserved.
|
Chris@14
|
4 *
|
Chris@14
|
5 * Licensed under the Apache License, Version 2.0 (the "License");
|
Chris@14
|
6 * you may not use this file except in compliance with the License.
|
Chris@14
|
7 * You may obtain a copy of the License at
|
Chris@14
|
8 *
|
Chris@14
|
9 * http://www.apache.org/licenses/LICENSE-2.0
|
Chris@14
|
10 *
|
Chris@14
|
11 * Unless required by applicable law or agreed to in writing, software
|
Chris@14
|
12 * distributed under the License is distributed on an "AS IS" BASIS,
|
Chris@14
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
Chris@14
|
14 * See the License for the specific language governing permissions and
|
Chris@14
|
15 * limitations under the License.
|
Chris@14
|
16 *
|
Chris@14
|
17 * @package WebDriver
|
Chris@14
|
18 *
|
Chris@14
|
19 * @author Anthon Pang <apang@softwaredevelopment.ca>
|
Chris@14
|
20 */
|
Chris@14
|
21
|
Chris@14
|
22 namespace WebDriver;
|
Chris@14
|
23
|
Chris@14
|
24 use WebDriver\Exception as WebDriverException;
|
Chris@14
|
25
|
Chris@14
|
26 /**
|
Chris@14
|
27 * WebDriver\Storage class
|
Chris@14
|
28 *
|
Chris@14
|
29 * @package WebDriver
|
Chris@14
|
30 *
|
Chris@14
|
31 * @method mixed getKey($key) Get key/value pair.
|
Chris@14
|
32 * @method void deleteKey($key) Delete a specific key.
|
Chris@14
|
33 * @method integer size() Get the number of items in the storage.
|
Chris@14
|
34 */
|
Chris@14
|
35 abstract class Storage extends AbstractWebDriver
|
Chris@14
|
36 {
|
Chris@14
|
37 /**
|
Chris@14
|
38 * {@inheritdoc}
|
Chris@14
|
39 */
|
Chris@14
|
40 protected function methods()
|
Chris@14
|
41 {
|
Chris@14
|
42 return array(
|
Chris@14
|
43 'key' => array('GET', 'DELETE'),
|
Chris@14
|
44 'size' => array('GET'),
|
Chris@14
|
45 );
|
Chris@14
|
46 }
|
Chris@14
|
47
|
Chris@14
|
48 /**
|
Chris@14
|
49 * Get all keys from storage or a specific key/value pair
|
Chris@14
|
50 *
|
Chris@14
|
51 * @return mixed
|
Chris@14
|
52 */
|
Chris@14
|
53 public function get()
|
Chris@14
|
54 {
|
Chris@14
|
55 // get all keys
|
Chris@14
|
56 if (func_num_args() === 0) {
|
Chris@14
|
57 $result = $this->curl('GET', '');
|
Chris@14
|
58
|
Chris@14
|
59 return $result['value'];
|
Chris@14
|
60 }
|
Chris@14
|
61
|
Chris@14
|
62 // get key/value pair
|
Chris@14
|
63 if (func_num_args() === 1) {
|
Chris@14
|
64 return $this->getKey(func_get_arg(0));
|
Chris@14
|
65 }
|
Chris@14
|
66
|
Chris@14
|
67 throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
|
Chris@14
|
68 }
|
Chris@14
|
69
|
Chris@14
|
70 /**
|
Chris@14
|
71 * Set specific key/value pair
|
Chris@14
|
72 *
|
Chris@14
|
73 * @return \WebDriver\Storage
|
Chris@14
|
74 *
|
Chris@14
|
75 * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters
|
Chris@14
|
76 */
|
Chris@14
|
77 public function set()
|
Chris@14
|
78 {
|
Chris@14
|
79 if (func_num_args() === 1
|
Chris@14
|
80 && is_array($arg = func_get_arg(0))
|
Chris@14
|
81 ) {
|
Chris@14
|
82 $this->curl('POST', '', $arg);
|
Chris@14
|
83
|
Chris@14
|
84 return $this;
|
Chris@14
|
85 }
|
Chris@14
|
86
|
Chris@14
|
87 if (func_num_args() === 2) {
|
Chris@14
|
88 $arg = array(
|
Chris@14
|
89 'key' => func_get_arg(0),
|
Chris@14
|
90 'value' => func_get_arg(1),
|
Chris@14
|
91 );
|
Chris@14
|
92 $this->curl('POST', '', $arg);
|
Chris@14
|
93
|
Chris@14
|
94 return $this;
|
Chris@14
|
95 }
|
Chris@14
|
96
|
Chris@14
|
97 throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
|
Chris@14
|
98 }
|
Chris@14
|
99
|
Chris@14
|
100 /**
|
Chris@14
|
101 * Delete storage or a specific key
|
Chris@14
|
102 *
|
Chris@14
|
103 * @return \WebDriver\Storage
|
Chris@14
|
104 *
|
Chris@14
|
105 * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters
|
Chris@14
|
106 */
|
Chris@14
|
107 public function delete()
|
Chris@14
|
108 {
|
Chris@14
|
109 // delete storage
|
Chris@14
|
110 if (func_num_args() === 0) {
|
Chris@14
|
111 $this->curl('DELETE', '');
|
Chris@14
|
112
|
Chris@14
|
113 return $this;
|
Chris@14
|
114 }
|
Chris@14
|
115
|
Chris@14
|
116 // delete key from storage
|
Chris@14
|
117 if (func_num_args() === 1) {
|
Chris@14
|
118 return $this->deleteKey(func_get_arg(0));
|
Chris@14
|
119 }
|
Chris@14
|
120
|
Chris@14
|
121 throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
|
Chris@14
|
122 }
|
Chris@14
|
123
|
Chris@14
|
124 /**
|
Chris@14
|
125 * Factory method to create Storage objects
|
Chris@14
|
126 *
|
Chris@14
|
127 * @param string $type 'local' or 'session' storage
|
Chris@14
|
128 * @param string $url URL
|
Chris@14
|
129 *
|
Chris@14
|
130 * @return \WebDriver\Storage
|
Chris@14
|
131 */
|
Chris@14
|
132 public static function factory($type, $url)
|
Chris@14
|
133 {
|
Chris@14
|
134 // dynamically define custom storage classes
|
Chris@14
|
135 $className = ucfirst(strtolower($type));
|
Chris@14
|
136 $namespacedClassName = __CLASS__ . '\\' . $className;
|
Chris@14
|
137
|
Chris@14
|
138 if (!class_exists($namespacedClassName, false)) {
|
Chris@14
|
139 eval(
|
Chris@14
|
140 'namespace ' . __CLASS__ . '; final class ' . $className . ' extends \\' . __CLASS__ . '{}'
|
Chris@14
|
141 );
|
Chris@14
|
142 }
|
Chris@14
|
143
|
Chris@14
|
144 return new $namespacedClassName($url);
|
Chris@14
|
145 }
|
Chris@14
|
146 }
|