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

Commit 85d4ebc

Browse files
committed
Add json and jsonable extractors
1 parent 13f97a1 commit 85d4ebc

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 <pinepain@gmail.com>
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\Context;
23+
use V8\Exceptions\TryCatchException;
24+
use V8\JSON;
25+
use V8\Value;
26+
27+
28+
class JsonExtractor implements PlainExtractorInterface
29+
{
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
34+
{
35+
try {
36+
$json = JSON::stringify($context, $value);
37+
38+
if ($definition->getNext()) {
39+
$extractor->extract($context, $value, $definition->getNext());
40+
}
41+
42+
return $json;
43+
} catch (TryCatchException $e) {
44+
throw new ExtractorException("Failed to stringify value: " . $e->getMessage());
45+
}
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 <pinepain@gmail.com>
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\Context;
23+
use V8\Exceptions\TryCatchException;
24+
use V8\JSON;
25+
use V8\Value;
26+
27+
28+
class JsonableExtractor implements PlainExtractorInterface
29+
{
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function extract(Context $context, Value $value, PlainExtractorDefinitionInterface $definition, ExtractorInterface $extractor)
34+
{
35+
try {
36+
JSON::stringify($context, $value);
37+
38+
if (!$definition->getNext()) {
39+
throw new ExtractorException("Jsonable extractor should be provided with next extractor");
40+
}
41+
42+
return $extractor->extract($context, $value, $definition->getNext());
43+
} catch (TryCatchException $e) {
44+
throw new ExtractorException("Failed to stringify value: " . $e->getMessage());
45+
}
46+
}
47+
}

src/Laravel/JsSandboxServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
use Pinepain\JsSandbox\Extractors\PlainExtractors\DateTimeExtractor;
3737
use Pinepain\JsSandbox\Extractors\PlainExtractors\FunctionExtractor;
3838
use Pinepain\JsSandbox\Extractors\PlainExtractors\InstanceExtractor;
39+
use Pinepain\JsSandbox\Extractors\PlainExtractors\JsonableExtractor;
40+
use Pinepain\JsSandbox\Extractors\PlainExtractors\JsonExtractor;
3941
use Pinepain\JsSandbox\Extractors\PlainExtractors\NullExtractor;
4042
use Pinepain\JsSandbox\Extractors\PlainExtractors\NumberExtractor;
4143
use Pinepain\JsSandbox\Extractors\PlainExtractors\ObjectExtractor;
@@ -250,6 +252,9 @@ protected function registerExtractor()
250252
$collection->put('instance', $instance = $app->make(InstanceExtractor::class));
251253

252254
$collection->put('assoc', $assoc = new AssocExtractor());
255+
$collection->put('json', $json = new JsonExtractor());
256+
$collection->put('jsonable', $json = new JsonableExtractor());
257+
253258
$collection->put('scalar', $scalar = new ScalarExtractor($string, $number, $bool, $null, $undefined));
254259

255260
return $collection;

0 commit comments

Comments
 (0)