Skip to content
This repository was archived by the owner on Mar 29, 2024. It is now read-only.

Commit 2b62e52

Browse files
committed
Allow to restrict assoc to js array object only
1 parent 0be1e18 commit 2b62e52

File tree

3 files changed

+112
-79
lines changed

3 files changed

+112
-79
lines changed

src/Extractors/PlainExtractors/ArrayExtractor.php

Lines changed: 14 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -21,73 +21,31 @@
2121
use Pinepain\JsSandbox\Extractors\ExtractorInterface;
2222
use V8\ArrayObject;
2323
use V8\Context;
24-
use V8\IntegerValue;
25-
use V8\ObjectValue;
2624
use V8\Value;
2725

2826

2927
class ArrayExtractor implements PlainExtractorInterface
3028
{
29+
/**
30+
* @var AssocExtractor
31+
*/
32+
private $extractor;
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function __construct(AssocExtractor $extractor)
38+
{
39+
$this->extractor = $extractor;
40+
}
41+
3142
/**
3243
* {@inheritdoc}
3344
*/
3445
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
3546
{
3647
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, $next);
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-
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, $next);
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-
48+
return $this->extractor->extract($context, $value, $definition, $extractor);
9149
}
9250

9351
throw new ExtractorException('Value must be of array type, ' . $value->typeOf()->value() . ' given instead');

src/Extractors/PlainExtractors/AssocExtractor.php

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Pinepain\JsSandbox\Extractors\Definition\PlainExtractorDefinitionInterface;
2020
use Pinepain\JsSandbox\Extractors\ExtractorException;
2121
use Pinepain\JsSandbox\Extractors\ExtractorInterface;
22+
use V8\ArrayObject;
2223
use V8\Context;
2324
use V8\IntegerValue;
2425
use V8\ObjectValue;
@@ -27,43 +28,116 @@
2728

2829
class AssocExtractor implements PlainExtractorInterface
2930
{
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+
3041
/**
3142
* {@inheritdoc}
3243
*/
3344
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
3445
{
35-
if ($value instanceof ObjectValue) {
36-
$own_properties = $value->getOwnPropertyNames($context);
46+
if ($value instanceof ArrayObject) {
47+
$items = $this->extractArrayValues($context, $value, $definition, $extractor);
3748

38-
$length = $own_properties->length();
39-
$isolate = $context->getIsolate();
49+
if (!$this->array_with_props) {
50+
return $items;
51+
}
4052

41-
$out = [];
53+
$props = $this->extractObjectValues($context, $value, $definition, $extractor);
4254

43-
$next = $definition->getNext();
55+
// length is a built-in property which we are not interested here
56+
unset($props['length']);
4457

45-
for ($i = 0; $i < $length; $i++) {
46-
/** @var \V8\PrimitiveValue $prop */
47-
$prop = $own_properties->get($context, new IntegerValue($isolate, $i));
48-
$item = $value->get($context, $prop);
58+
return array_merge($items, $props);
59+
}
4960

50-
$prop_name = $prop->value();
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();
5182

52-
if ($next) {
53-
try {
54-
$out[$prop_name] = $extractor->extract($context, $item, $definition);
55-
} catch (ExtractorException $e) {
56-
throw new ExtractorException("Failed to convert assoc item #{$prop_name}: " . $e->getMessage());
57-
}
58-
} else {
59-
$out[$prop_name] = $item;
83+
$next = $definition->getNext();
84+
85+
for ($i = 0; $i < $length; $i++) {
86+
$item = $value->get($context, new IntegerValue($isolate, $i));
87+
88+
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());
6093
}
94+
} else {
95+
$out[] = $item;
6196
}
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();
62121

63-
return $out;
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);
64126

127+
$prop_name = $prop->value();
128+
129+
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+
}
65138
}
66139

67-
throw new ExtractorException('Value must be of object type, ' . $value->typeOf()->value() . ' given instead');
140+
return $out;
68141
}
142+
69143
}

src/Laravel/JsSandboxServiceProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ protected function registerExtractor()
256256

257257
// TODO: register basic extractor
258258

259-
$collection->put('[]', $array = new ArrayExtractor());
259+
$collection->put('[]', $array = new AssocExtractor());
260+
$collection->put('array', $array = new ArrayExtractor(new AssocExtractor(false)));
261+
260262
$collection->put('raw', $raw = new RawExtractor());
261263
$collection->put('primitive', $primitive = new PrimitiveExtractor());
262264

@@ -273,7 +275,6 @@ protected function registerExtractor()
273275
$collection->put('function', $function = new FunctionExtractor());
274276
$collection->put('native-object', $instance = $app->make(NativeObjectExtractor::class));
275277

276-
$collection->put('assoc', $assoc = new AssocExtractor());
277278
$collection->put('json', $json = new JsonExtractor());
278279
$collection->put('jsonable', $json = new JsonableExtractor());
279280

0 commit comments

Comments
 (0)