1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the pinepain/js-sandbox PHP library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT license: http://opensource.org/licenses/MIT |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the |
11
|
|
|
* LICENSE file that was distributed with this source or visit |
12
|
|
|
* http://opensource.org/licenses/MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
namespace Pinepain\JsSandbox\Extractors\PlainExtractors; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
use Pinepain\JsSandbox\Extractors\Definition\PlainExtractorDefinitionInterface; |
20
|
|
|
use Pinepain\JsSandbox\Extractors\ExtractorException; |
21
|
|
|
use Pinepain\JsSandbox\Extractors\ExtractorInterface; |
22
|
|
|
use V8\ArrayObject; |
23
|
|
|
use V8\Context; |
24
|
|
|
use V8\IntegerValue; |
25
|
|
|
use V8\ObjectValue; |
26
|
|
|
use V8\Value; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
class ArrayExtractor implements PlainExtractorInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor) |
35
|
|
|
{ |
36
|
|
|
if ($value instanceof ArrayObject) { |
37
|
|
|
$length = $value->length(); |
38
|
|
|
$isolate = $context->getIsolate(); |
39
|
|
|
|
40
|
|
|
$out = []; |
41
|
|
|
|
42
|
|
|
$next = $definition->getNext(); |
43
|
|
|
|
44
|
|
|
for ($i = 0; $i < $length; $i++) { |
45
|
|
|
$item = $value->get($context, new IntegerValue($isolate, $i)); |
46
|
|
|
|
47
|
|
|
if ($next) { |
48
|
|
|
try { |
49
|
|
|
$out[] = $extractor->extract($context, $item, $definition); |
50
|
|
|
} catch (ExtractorException $e) { |
51
|
|
|
throw new ExtractorException("Failed to convert array item #{$i}: " . $e->getMessage()); |
52
|
|
|
} |
53
|
|
|
} else { |
54
|
|
|
$out[] = $item; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $out; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
View Code Duplication |
if ($value instanceof ObjectValue) { |
|
|
|
|
62
|
|
|
$own_properties = $value->getOwnPropertyNames($context); |
63
|
|
|
|
64
|
|
|
$length = $own_properties->length(); |
65
|
|
|
$isolate = $context->getIsolate(); |
66
|
|
|
|
67
|
|
|
$out = []; |
68
|
|
|
|
69
|
|
|
$next = $definition->getNext(); |
70
|
|
|
|
71
|
|
|
for ($i = 0; $i < $length; $i++) { |
72
|
|
|
/** @var \V8\PrimitiveValue $prop */ |
73
|
|
|
$prop = $own_properties->get($context, new IntegerValue($isolate, $i)); |
74
|
|
|
$item = $value->get($context, $prop); |
75
|
|
|
|
76
|
|
|
$prop_name = $prop->value(); |
77
|
|
|
|
78
|
|
|
if ($next) { |
79
|
|
|
try { |
80
|
|
|
$out[$prop_name] = $extractor->extract($context, $item, $definition); |
81
|
|
|
} catch (ExtractorException $e) { |
82
|
|
|
throw new ExtractorException("Failed to convert array item #{$prop_name}: " . $e->getMessage()); |
83
|
|
|
} |
84
|
|
|
} else { |
85
|
|
|
$out[$prop_name] = $item; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $out; |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
throw new ExtractorException('Value must be of array type, ' . $value->typeOf()->value() . ' given instead'); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.