1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Storyplayer/Prose |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace Prose; |
45
|
|
|
|
46
|
|
|
use ReflectionObject; |
47
|
|
|
use DataSift\Storyplayer\PlayerLib\StoryTeller; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* base class for all Prose classes |
51
|
|
|
* |
52
|
|
|
* @category Libraries |
53
|
|
|
* @package Storyplayer/Prose |
54
|
|
|
* @author Stuart Herbert <[email protected]> |
55
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
56
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
57
|
|
|
* @link http://datasift.github.io/storyplayer |
58
|
|
|
*/ |
59
|
|
|
class Prose |
60
|
|
|
{ |
61
|
|
|
/** |
62
|
|
|
* the one object that knows how to get to everything |
63
|
|
|
* @var \DataSift\Storyplayer\PlayerLib\StoryTeller |
64
|
|
|
*/ |
65
|
|
|
protected $st = null; |
66
|
|
|
protected $args = array(); |
67
|
|
|
protected $topElement = null; |
68
|
|
|
protected $topXpath = null; |
69
|
|
|
protected $device = null; |
70
|
|
|
|
71
|
|
|
public function __construct(StoryTeller $st, $args = array()) |
72
|
|
|
{ |
73
|
|
|
// save the StoryTeller object; we're going to need it! |
74
|
|
|
$this->st = $st; |
75
|
|
|
|
76
|
|
|
// save any arguments that have been passed into the constructor |
77
|
|
|
// our child classes may be interested in them |
78
|
|
|
if (!is_array($args)) { |
79
|
|
|
throw new E5xx_ActionFailed(__METHOD__); |
80
|
|
|
} |
81
|
|
|
$this->args = $args; |
82
|
|
|
|
83
|
|
|
// setup the page context |
84
|
|
|
$this->initPageContext(); |
85
|
|
|
|
86
|
|
|
// run any context-specific setup that we need |
87
|
|
|
$this->initActions(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function initPageContext() |
91
|
|
|
{ |
92
|
|
|
// make sure we are looking at the right part of the page |
93
|
|
|
$pageContext = $this->st->getPageContext(); |
94
|
|
|
$pageContext->switchToContext($this->st); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* override this method if required (for example, for web browsers) |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
protected function initActions() |
103
|
|
|
{ |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function initDevice() |
107
|
|
|
{ |
108
|
|
|
// start the test device |
109
|
|
|
$this->device = $this->st->getRunningDevice(); |
110
|
|
|
|
111
|
|
|
// set our top XPATH node |
112
|
|
|
// |
113
|
|
|
// for the moment, we are assuming that the test device is |
114
|
|
|
// a web browser, because historically this has always been |
115
|
|
|
// the case |
116
|
|
|
// |
117
|
|
|
// when this assumption is no longer valid, we will need to |
118
|
|
|
// revisit this code |
119
|
|
|
$this->setTopXpath("//html"); |
120
|
|
|
|
121
|
|
|
// set our top element |
122
|
|
|
// |
123
|
|
|
// we cannot assume that the browser has any DOM loaded at all |
124
|
|
|
$this->setTopElement($this->device); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function __call($methodName, $params) |
128
|
|
|
{ |
129
|
|
|
// this only gets called if there's no matching method |
130
|
|
|
throw new E5xx_NotImplemented(get_class($this) . '::' . $methodName); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getTopElement() |
|
|
|
|
134
|
|
|
{ |
135
|
|
|
return $this->topElement; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function setTopElement($element) |
139
|
|
|
{ |
140
|
|
|
$this->topElement = $element; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
protected function getTopXpath() |
144
|
|
|
{ |
145
|
|
|
return $this->topXpath; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $xpath |
150
|
|
|
*/ |
151
|
|
|
protected function setTopXpath($xpath) |
152
|
|
|
{ |
153
|
|
|
$this->topXpath = $xpath; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// ==================================================================== |
157
|
|
|
// |
158
|
|
|
// Convertors go here |
159
|
|
|
// |
160
|
|
|
// -------------------------------------------------------------------- |
161
|
|
|
|
162
|
|
|
public function toNum($string) |
163
|
|
|
{ |
164
|
|
|
$final = str_replace(array(',', '$', ' '), '', $string); |
165
|
|
|
|
166
|
|
|
return (double)$final; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getSourceFilename() |
170
|
|
|
{ |
171
|
|
|
$refObj = new ReflectionObject($this); |
172
|
|
|
return $refObj->getFileName(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.