PageRenderTime 40ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPWebDriver/WebDriverStorage.php

https://github.com/adiyehezkel/php-webdriver
PHP | 143 lines | 61 code | 17 blank | 65 comment | 14 complexity | f475a7bf355270d3d3af40bac228dba7 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2011-2012 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 <anthonp@nationalfibre.net>
  20. */
  21. /**
  22. * WebDriverStorage class
  23. *
  24. * @package WebDriver
  25. *
  26. * @method mixed getKey($key) Get key/value pair.
  27. * @method void deleteKey($key) Delete a specific key.
  28. * @method integer size() Get the number of items in the storage.
  29. */
  30. abstract class PHPWebDriver_WebDriverStorage extends PHPWebDriver_WebDriverBase
  31. {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function methods()
  36. {
  37. return array(
  38. 'key' => array('GET', 'DELETE'),
  39. 'size' => array('GET'),
  40. );
  41. }
  42. /**
  43. * Get all keys from storage or a specific key/value pair
  44. *
  45. * @return mixed
  46. *
  47. * @throw UnhandledWebDriverError
  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 new PHPWebDriver_UnhandledWebDriverError;
  61. }
  62. /**
  63. * Set specific key/value pair
  64. *
  65. * @return PHPWebDriver_WebDriverStorage
  66. *
  67. * @throw UnhandledWebDriverError
  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 new PHPWebDriver_UnhandledWebDriverError;
  86. }
  87. /**
  88. * Delete storage or a specific key
  89. *
  90. * @return PHPWebDriver_WebDriverStorage
  91. *
  92. * @throw UnhandledWebDriverError
  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 new PHPWebDriver_UnhandledWebDriverError;
  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 PHPWebDriver_WebDriverStorage
  114. */
  115. public static function factory($type, $url)
  116. {
  117. // dynamically define custom storage classes
  118. $className = __CLASS__ . '_' . ucfirst(strtolower($type));
  119. if (!class_exists($className, false)) {
  120. eval(
  121. 'final class ' . $className.' extends PHPWebDriver_WebDriverStorage {}'
  122. );
  123. }
  124. return new $className($url);
  125. }
  126. }