Skip to content

Commit c0aef53

Browse files
committed
move serializer functions
1 parent f168d21 commit c0aef53

File tree

2 files changed

+89
-89
lines changed

2 files changed

+89
-89
lines changed

lib/WebDriver/AbstractWebDriver.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,95 @@ public function getTransientOptions()
128128
return $this->transientOptions;
129129
}
130130

131+
/**
132+
* Serialize script arguments (containing web elements and/or shadow roots)
133+
*
134+
* @see https://w3c.github.io/webdriver/#executing-script
135+
*
136+
* @param array $arguments
137+
*
138+
* @return array
139+
*/
140+
protected function serializeArguments(array $arguments)
141+
{
142+
foreach ($arguments as $key => $value) {
143+
if ($value instanceof LegacyElement) {
144+
$arguments[$key] = [LegacyElement::LEGACY_ELEMENT_ID => $value->getID()];
145+
} elseif ($value instanceof Element) {
146+
$arguments[$key] = [Element::WEB_ELEMENT_ID => $value->getID()];
147+
} elseif ($value instanceof Shadow) {
148+
$arguments[$key] = [Shadow::SHADOW_ROOT_ID => $value->getID()];
149+
} elseif (is_array($value)) {
150+
$arguments[$key] = $this->serializeArguments($value);
151+
}
152+
}
153+
154+
return $arguments;
155+
}
156+
157+
/**
158+
* Unserialize result (containing web elements and/or shadow roots)
159+
*
160+
* @param mixed $result
161+
*
162+
* @return mixed
163+
*/
164+
protected function unserializeResult($result)
165+
{
166+
$element = is_array($result) ? $this->makeElement($result) : null;
167+
168+
if ($element !== null) {
169+
return $element;
170+
}
171+
172+
if (is_array($result)) {
173+
foreach ($result as $key => $value) {
174+
$result[$key] = $this->unserializeResult($value);
175+
}
176+
}
177+
178+
return $result;
179+
}
180+
181+
/**
182+
* Factory method for elements
183+
*
184+
* @param array $value
185+
*
186+
* @return \WebDriver\Element|\WebDriver\Shadow|null
187+
*/
188+
protected function makeElement($value)
189+
{
190+
if (array_key_exists(LegacyElement::LEGACY_ELEMENT_ID, $value)) {
191+
$identifier = $value[LegacyElement::LEGACY_ELEMENT_ID];
192+
193+
return new LegacyElement(
194+
$this->getIdentifierPath('/element/' . $identifier),
195+
$identifier
196+
);
197+
}
198+
199+
if (array_key_exists(Element::WEB_ELEMENT_ID, $value)) {
200+
$identifier = $value[Element::WEB_ELEMENT_ID];
201+
202+
return new Element(
203+
$this->getIdentifierPath('/element/' . $identifier),
204+
$identifier
205+
);
206+
}
207+
208+
if (array_key_exists(Shadow::SHADOW_ROOT_ID, $value)) {
209+
$identifier = $value[Shadow::SHADOW_ROOT_ID];
210+
211+
return new Shadow(
212+
$this->getIdentifierPath('/shadow/' . $identifier),
213+
$identifier
214+
);
215+
}
216+
217+
return null;
218+
}
219+
131220
/**
132221
* Curl request to webdriver server.
133222
*

lib/WebDriver/Execute.php

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -58,95 +58,6 @@ public function sync(array $jsonScript)
5858
return $this->unserializeResult($result['value']);
5959
}
6060

61-
/**
62-
* Serialize script arguments (containing web elements and/or shadow roots)
63-
*
64-
* @see https://w3c.github.io/webdriver/#executing-script
65-
*
66-
* @param array $arguments
67-
*
68-
* @return array
69-
*/
70-
protected function serializeArguments(array $arguments)
71-
{
72-
foreach ($arguments as $key => $value) {
73-
if ($value instanceof LegacyElement) {
74-
$arguments[$key] = [LegacyElement::LEGACY_ELEMENT_ID => $value->getID()];
75-
} elseif ($value instanceof Element) {
76-
$arguments[$key] = [Element::WEB_ELEMENT_ID => $value->getID()];
77-
} elseif ($value instanceof Shadow) {
78-
$arguments[$key] = [Shadow::SHADOW_ROOT_ID => $value->getID()];
79-
} elseif (is_array($value)) {
80-
$arguments[$key] = $this->serializeArguments($value);
81-
}
82-
}
83-
84-
return $arguments;
85-
}
86-
87-
/**
88-
* Unserialize result (containing web elements and/or shadow roots)
89-
*
90-
* @param mixed $result
91-
*
92-
* @return mixed
93-
*/
94-
protected function unserializeResult($result)
95-
{
96-
$element = is_array($result) ? $this->makeElement($result) : null;
97-
98-
if ($element !== null) {
99-
return $element;
100-
}
101-
102-
if (is_array($result)) {
103-
foreach ($result as $key => $value) {
104-
$result[$key] = $this->unserializeResult($value);
105-
}
106-
}
107-
108-
return $result;
109-
}
110-
111-
/**
112-
* Factory method for elements
113-
*
114-
* @param array $value
115-
*
116-
* @return \WebDriver\Element|\WebDriver\Shadow|null
117-
*/
118-
protected function makeElement($value)
119-
{
120-
if (array_key_exists(LegacyElement::LEGACY_ELEMENT_ID, $value)) {
121-
$identifier = $value[LegacyElement::LEGACY_ELEMENT_ID];
122-
123-
return new LegacyElement(
124-
$this->getIdentifierPath('/element/' . $identifier),
125-
$identifier
126-
);
127-
}
128-
129-
if (array_key_exists(Element::WEB_ELEMENT_ID, $value)) {
130-
$identifier = $value[Element::WEB_ELEMENT_ID];
131-
132-
return new Element(
133-
$this->getIdentifierPath('/element/' . $identifier),
134-
$identifier
135-
);
136-
}
137-
138-
if (array_key_exists(Shadow::SHADOW_ROOT_ID, $value)) {
139-
$identifier = $value[Shadow::SHADOW_ROOT_ID];
140-
141-
return new Shadow(
142-
$this->getIdentifierPath('/shadow/' . $identifier),
143-
$identifier
144-
);
145-
}
146-
147-
return null;
148-
}
149-
15061
/**
15162
* {@inheritdoc}
15263
*/

0 commit comments

Comments
 (0)