Test Setup Failed
Push — master ( 2d30d0...6482dc )
by Bogdan
07:58
created

AssocExtractor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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