|
19 | 19 | use Pinepain\JsSandbox\Extractors\Definition\PlainExtractorDefinitionInterface;
|
20 | 20 | use Pinepain\JsSandbox\Extractors\ExtractorException;
|
21 | 21 | use Pinepain\JsSandbox\Extractors\ExtractorInterface;
|
| 22 | +use V8\ArrayObject; |
22 | 23 | use V8\Context;
|
23 | 24 | use V8\IntegerValue;
|
24 | 25 | use V8\ObjectValue;
|
|
27 | 28 |
|
28 | 29 | class AssocExtractor implements PlainExtractorInterface
|
29 | 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 | + |
30 | 41 | /**
|
31 | 42 | * {@inheritdoc}
|
32 | 43 | */
|
33 | 44 | public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
|
34 | 45 | {
|
35 |
| - if ($value instanceof ObjectValue) { |
36 |
| - $own_properties = $value->getOwnPropertyNames($context); |
| 46 | + if ($value instanceof ArrayObject) { |
| 47 | + $items = $this->extractArrayValues($context, $value, $definition, $extractor); |
37 | 48 |
|
38 |
| - $length = $own_properties->length(); |
39 |
| - $isolate = $context->getIsolate(); |
| 49 | + if (!$this->array_with_props) { |
| 50 | + return $items; |
| 51 | + } |
40 | 52 |
|
41 |
| - $out = []; |
| 53 | + $props = $this->extractObjectValues($context, $value, $definition, $extractor); |
42 | 54 |
|
43 |
| - $next = $definition->getNext(); |
| 55 | + // length is a built-in property which we are not interested here |
| 56 | + unset($props['length']); |
44 | 57 |
|
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 | + } |
49 | 60 |
|
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(); |
51 | 82 |
|
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()); |
60 | 93 | }
|
| 94 | + } else { |
| 95 | + $out[] = $item; |
61 | 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(); |
62 | 121 |
|
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); |
64 | 126 |
|
| 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 | + } |
65 | 138 | }
|
66 | 139 |
|
67 |
| - throw new ExtractorException('Value must be of object type, ' . $value->typeOf()->value() . ' given instead'); |
| 140 | + return $out; |
68 | 141 | }
|
| 142 | + |
69 | 143 | }
|
0 commit comments