|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the pinepain/php-v8-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\Definition\VariableExtractorDefinition; |
|
21
|
|
|
use Pinepain\JsSandbox\Extractors\Definition\VariableExtractorDefinitionInterface; |
|
22
|
|
|
use Pinepain\JsSandbox\Extractors\ExtractorException; |
|
23
|
|
|
use Pinepain\JsSandbox\Extractors\ExtractorInterface; |
|
24
|
|
|
use Pinepain\JsSandbox\Extractors\ObjectComponents\ExtractorsObjectStoreInterface; |
|
25
|
|
|
use UnexpectedValueException; |
|
26
|
|
|
use V8\Context; |
|
27
|
|
|
use V8\NumberObject; |
|
28
|
|
|
use V8\NumberValue; |
|
29
|
|
|
use V8\ObjectValue; |
|
30
|
|
|
use V8\Value; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class InstanceExtractor implements PlainExtractorInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var ExtractorsObjectStoreInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
private $object_store; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct(ExtractorsObjectStoreInterface $object_store) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->object_store = $object_store; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
|
|
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($value instanceof ObjectValue) { |
|
51
|
|
|
try { |
|
52
|
|
|
$instance = $this->object_store->get($value); |
|
53
|
|
|
|
|
54
|
|
|
if ($definition->getNext()) { |
|
55
|
|
|
foreach ($definition->getNext()->getVariations() as $variation) { |
|
56
|
|
|
if (is_a($instance, $variation->getName())) { |
|
57
|
|
|
return $instance; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
throw new ExtractorException('Instance value constraint failed: value is not an instance of given classes/interfaces'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $instance; |
|
65
|
|
|
} catch (UnexpectedValueException $e) { |
|
66
|
|
|
throw new ExtractorException('Unable to find instance for the given object'); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
throw new ExtractorException('Value must be of the type object to be able to find instance, ' . $value->typeOf()->value() . ' given'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|