1: <?php
2: namespace Azalea\Selenium\Core;
3:
4: 5: 6:
7: class View extends WebDriver
8: {
9:
10: protected static $hash = "";
11:
12:
13: protected static $selector = "";
14:
15: 16: 17: 18: 19:
20: public function __construct($driver, $args = array())
21: {
22: if (!$driver) {
23: throw new \InvalidArgumentException(get_class($this).": driver not defined");
24: }
25: $this->driver = $driver;
26:
27:
28: if (!$this->verify($args)) {
29: throw new \RuntimeException("Could not verify view: ".get_called_class());
30: }
31: }
32:
33: 34: 35: 36: 37: 38: 39:
40: public static function getHash($args = array())
41: {
42: return self::_replaceStringWithKeys(static::$hash, $args);
43: }
44:
45: 46: 47: 48:
49: public static function setHash($hash)
50: {
51: static::$hash = $hash;
52: }
53:
54: 55: 56: 57:
58: public function updateHash($hash)
59: {
60: $this->execute(array(
61: 'script' => "window.location.hash = '".$hash."'",
62: 'args' => array(),
63: ));
64: return true;
65: }
66:
67: 68: 69: 70: 71: 72:
73: public static function get($driver, $args = array())
74: {
75: if (!$driver) {
76: throw new \InvalidArgumentException(get_class($this).": driver not defined");
77: }
78:
79: $hash = static::getHash($args);
80:
81: if(!empty($hash)) {
82: $driver->execute(array(
83: 'script' => "window.location.hash = '".$hash."'",
84: 'args' => array(),
85: ));
86: }
87:
88: $classname = get_called_class();
89: return new $classname($driver, $args);
90: }
91:
92: 93: 94: 95: 96: 97: 98:
99: protected function getSelector($args = array())
100: {
101: return self::_replaceStringWithKeys(static::$selector, $args);
102: }
103:
104: 105: 106: 107:
108: protected function verify($args = array())
109: {
110: $selector = $this->getSelector($args);
111: if (!empty($selector)) {
112: $this->byCss($selector);
113: }
114: return true;
115: }
116:
117:
118: 119: 120: 121: 122:
123: private static function _replaceStringWithKeys($string='', $args=array())
124: {
125: foreach($args as $key => $val) {
126: if(strpos($string, "{" . $key . "}") !== false) {
127: $string = str_replace("{" . $key . "}", $val, $string);
128: }
129: }
130: return $string;
131: }
132: }
133: ?>