Storage.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright 2011-2017 Anthon Pang. All Rights Reserved.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. * @package WebDriver
  18. *
  19. * @author Anthon Pang <apang@softwaredevelopment.ca>
  20. */
  21. namespace WebDriver;
  22. use WebDriver\Exception as WebDriverException;
  23. /**
  24. * WebDriver\Storage class
  25. *
  26. * @package WebDriver
  27. *
  28. * @method mixed getKey($key) Get key/value pair.
  29. * @method void deleteKey($key) Delete a specific key.
  30. * @method integer size() Get the number of items in the storage.
  31. */
  32. abstract class Storage extends AbstractWebDriver
  33. {
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function methods()
  38. {
  39. return array(
  40. 'key' => array('GET', 'DELETE'),
  41. 'size' => array('GET'),
  42. );
  43. }
  44. /**
  45. * Get all keys from storage or a specific key/value pair
  46. *
  47. * @return mixed
  48. */
  49. public function get()
  50. {
  51. // get all keys
  52. if (func_num_args() === 0) {
  53. $result = $this->curl('GET', '');
  54. return $result['value'];
  55. }
  56. // get key/value pair
  57. if (func_num_args() === 1) {
  58. return $this->getKey(func_get_arg(0));
  59. }
  60. throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
  61. }
  62. /**
  63. * Set specific key/value pair
  64. *
  65. * @return \WebDriver\Storage
  66. *
  67. * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters
  68. */
  69. public function set()
  70. {
  71. if (func_num_args() === 1
  72. && is_array($arg = func_get_arg(0))
  73. ) {
  74. $this->curl('POST', '', $arg);
  75. return $this;
  76. }
  77. if (func_num_args() === 2) {
  78. $arg = array(
  79. 'key' => func_get_arg(0),
  80. 'value' => func_get_arg(1),
  81. );
  82. $this->curl('POST', '', $arg);
  83. return $this;
  84. }
  85. throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
  86. }
  87. /**
  88. * Delete storage or a specific key
  89. *
  90. * @return \WebDriver\Storage
  91. *
  92. * @throw \WebDriver\Exception\UnexpectedParameters if unexpected parameters
  93. */
  94. public function delete()
  95. {
  96. // delete storage
  97. if (func_num_args() === 0) {
  98. $this->curl('DELETE', '');
  99. return $this;
  100. }
  101. // delete key from storage
  102. if (func_num_args() === 1) {
  103. return $this->deleteKey(func_get_arg(0));
  104. }
  105. throw WebDriverException::factory(WebDriverException::UNEXPECTED_PARAMETERS);
  106. }
  107. /**
  108. * Factory method to create Storage objects
  109. *
  110. * @param string $type 'local' or 'session' storage
  111. * @param string $url URL
  112. *
  113. * @return \WebDriver\Storage
  114. */
  115. public static function factory($type, $url)
  116. {
  117. // dynamically define custom storage classes
  118. $className = ucfirst(strtolower($type));
  119. $namespacedClassName = __CLASS__ . '\\' . $className;
  120. if (!class_exists($namespacedClassName, false)) {
  121. eval(
  122. 'namespace ' . __CLASS__ . '; final class ' . $className . ' extends \\' . __CLASS__ . '{}'
  123. );
  124. }
  125. return new $namespacedClassName($url);
  126. }
  127. }