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 AssocExtractor implements PlainExtractorInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
private $array_with_props; |
35
|
|
|
|
36
|
|
|
public function __construct(bool $array_with_props = true) |
37
|
|
|
{ |
38
|
|
|
$this->array_with_props = $array_with_props; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
|
|
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor) |
45
|
|
|
{ |
46
|
|
|
if ($value instanceof ArrayObject) { |
|
|
|
|
47
|
|
|
$items = $this->extractArrayValues($context, $value, $definition, $extractor); |
48
|
|
|
|
49
|
|
|
if (!$this->array_with_props) { |
50
|
|
|
return $items; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$props = $this->extractObjectValues($context, $value, $definition, $extractor); |
54
|
|
|
|
55
|
|
|
// length is a built-in property which we are not interested here |
56
|
|
|
unset($props['length']); |
57
|
|
|
|
58
|
|
|
return array_merge($items, $props); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if ($value instanceof ObjectValue) { |
|
|
|
|
62
|
|
|
return $this->extractObjectValues($context, $value, $definition, $extractor); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
throw new ExtractorException('Value must be of array or object type, ' . $value->typeOf()->value() . ' given instead'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param Context $context |
70
|
|
|
* @param ArrayObject $value |
71
|
|
|
* @param PlainExtractorDefinitionInterface $definition |
72
|
|
|
* @param ExtractorInterface $extractor |
73
|
|
|
* |
74
|
|
|
* @return array |
75
|
|
|
* @throws ExtractorException |
76
|
|
|
*/ |
77
|
|
|
protected function extractArrayValues(Context $context, ArrayObject $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor): array |
78
|
|
|
{ |
79
|
|
|
$out = []; |
80
|
|
|
$length = $value->length(); |
81
|
|
|
$isolate = $context->getIsolate(); |
82
|
|
|
|
83
|
|
|
$next = $definition->getNext(); |
84
|
|
|
|
85
|
|
|
for ($i = 0; $i < $length; $i++) { |
86
|
|
|
$item = $value->get($context, new IntegerValue($isolate, $i)); |
87
|
|
|
|
88
|
|
View Code Duplication |
if ($next) { |
|
|
|
|
89
|
|
|
try { |
90
|
|
|
$out[] = $extractor->extract($context, $item, $next); |
91
|
|
|
} catch (ExtractorException $e) { |
92
|
|
|
throw new ExtractorException("Failed to convert array item #{$i}: " . $e->getMessage()); |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
$out[] = $item; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $out; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Context $context |
104
|
|
|
* @param ObjectValue $value |
105
|
|
|
* @param PlainExtractorDefinitionInterface $definition |
106
|
|
|
* @param ExtractorInterface $extractor |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
* @throws ExtractorException |
110
|
|
|
*/ |
111
|
|
|
protected function extractObjectValues(Context $context, ObjectValue $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor): array |
112
|
|
|
{ |
113
|
|
|
$own_properties = $value->getOwnPropertyNames($context); |
114
|
|
|
|
115
|
|
|
$length = $own_properties->length(); |
116
|
|
|
$isolate = $context->getIsolate(); |
117
|
|
|
|
118
|
|
|
$out = []; |
119
|
|
|
|
120
|
|
|
$next = $definition->getNext(); |
121
|
|
|
|
122
|
|
|
for ($i = 0; $i < $length; $i++) { |
123
|
|
|
/** @var \V8\PrimitiveValue $prop */ |
124
|
|
|
$prop = $own_properties->get($context, new IntegerValue($isolate, $i)); |
125
|
|
|
$item = $value->get($context, $prop); |
126
|
|
|
|
127
|
|
|
$prop_name = $prop->value(); |
128
|
|
|
|
129
|
|
View Code Duplication |
if ($next) { |
|
|
|
|
130
|
|
|
try { |
131
|
|
|
$out[$prop_name] = $extractor->extract($context, $item, $next); |
132
|
|
|
} catch (ExtractorException $e) { |
133
|
|
|
throw new ExtractorException("Failed to convert array item #{$prop_name}: " . $e->getMessage()); |
134
|
|
|
} |
135
|
|
|
} else { |
136
|
|
|
$out[$prop_name] = $item; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $out; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.