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

Commit 2d30d0a

Browse files
authored
Merge pull request #8 from pinepain/new-features
Bug fixes and new features
2 parents 344583c + f893e81 commit 2d30d0a

36 files changed

+1950
-260
lines changed

src/Decorators/DecoratorSpec.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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\Decorators;
17+
18+
19+
class DecoratorSpec implements DecoratorSpecInterface
20+
{
21+
/**
22+
* @var string
23+
*/
24+
private $name;
25+
/**
26+
* @var array
27+
*/
28+
private $arguments;
29+
30+
/**
31+
* @param string $name
32+
* @param array $arguments
33+
*/
34+
public function __construct(string $name, array $arguments = [])
35+
{
36+
$this->name = $name;
37+
$this->arguments = $arguments;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function getName(): string
44+
{
45+
return $this->name;
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function getArguments(): array
52+
{
53+
return $this->arguments;
54+
}
55+
}
+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\Decorators;
17+
18+
19+
use Pinepain\JsSandbox\Specs\Builder\ArgumentValueBuilderInterface;
20+
use Pinepain\JsSandbox\Specs\Builder\Exceptions\ArgumentValueBuilderException;
21+
22+
23+
class DecoratorSpecBuilder implements DecoratorSpecBuilderInterface
24+
{
25+
private $regexp = '/
26+
^
27+
\@(?<name>[a-z_]+(?:[\w-]*\w)?)
28+
\s*
29+
(?:
30+
\(
31+
\s*
32+
(?<params>
33+
(
34+
(?:[^\'\"\(\)\,\s]+) # literal
35+
|
36+
(?:[+-]?[0-9]+\.?[0-9]*) # numbers (no exponential notation)
37+
|
38+
(?:\'[^\']*\') # single-quoted string
39+
|
40+
(?:\"[^\"]*\") # double-quoted string
41+
|
42+
(?:\[\s*\]) # empty array
43+
|
44+
(?:\{\s*\}) # empty object
45+
|
46+
true | false | null
47+
)(?:\s*\,\s*((?-3))*)*
48+
)?
49+
\s*
50+
\)
51+
)?
52+
\s*
53+
$
54+
/xi';
55+
/**
56+
* @var ArgumentValueBuilderInterface
57+
*/
58+
private $argument;
59+
60+
/**
61+
* @param ArgumentValueBuilderInterface $argument
62+
*/
63+
public function __construct(ArgumentValueBuilderInterface $argument)
64+
{
65+
$this->argument = $argument;
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function build(string $definition): DecoratorSpecInterface
72+
{
73+
$definition = trim($definition);
74+
75+
if (!$definition) {
76+
throw new DecoratorSpecBuilderException('Definition must be non-empty string');
77+
}
78+
79+
try {
80+
if (preg_match($this->regexp, $definition, $matches)) {
81+
82+
$params = array_slice($matches, 5);
83+
$decorator = $this->buildDecorator($matches['name'], $params);
84+
85+
return $decorator;
86+
}
87+
} catch (ArgumentValueBuilderException $e) {
88+
// We don't care about what specific issue we hit inside,
89+
// for API user it means that the definition is invalid
90+
}
91+
92+
throw new DecoratorSpecBuilderException("Unable to parse definition: '{$definition}'");
93+
}
94+
95+
/**
96+
* @param string $name
97+
* @param array $raw_args
98+
*
99+
* @return DecoratorSpecInterface
100+
*/
101+
protected function buildDecorator(string $name, array $raw_args): DecoratorSpecInterface
102+
{
103+
$arguments = [];
104+
foreach ($raw_args as $raw_arg) {
105+
$arguments[] = $this->argument->build($raw_arg, true);
106+
}
107+
108+
return new DecoratorSpec($name, $arguments);
109+
}
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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\Decorators;
17+
18+
19+
use Pinepain\JsSandbox\Specs\Builder\Exceptions\SpecBuilderException;
20+
21+
22+
class DecoratorSpecBuilderException extends SpecBuilderException
23+
{
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Decorators;
17+
18+
19+
interface DecoratorSpecBuilderInterface
20+
{
21+
/**
22+
* @param string $definition
23+
*
24+
* @return DecoratorSpecInterface
25+
* @throws DecoratorSpecBuilderException
26+
*/
27+
public function build(string $definition): DecoratorSpecInterface;
28+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
namespace Pinepain\JsSandbox\Decorators;
16+
17+
18+
interface DecoratorSpecInterface
19+
{
20+
/**
21+
* @return string
22+
*/
23+
public function getName(): string;
24+
25+
/**
26+
* @return array
27+
*/
28+
public function getArguments(): array;
29+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Decorators;
17+
18+
19+
use OutOfBoundsException;
20+
use OverflowException;
21+
use Pinepain\JsSandbox\Decorators\Definitions\DecoratorInterface;
22+
23+
24+
class DecoratorsCollection implements DecoratorsCollectionInterface
25+
{
26+
protected $decorators = [];
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function get(string $name): DecoratorInterface
32+
{
33+
if (!isset($this->decorators[$name])) {
34+
throw new OutOfBoundsException("Decorator '{$name}' not found");
35+
}
36+
37+
return $this->decorators[$name];
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function put(string $name, DecoratorInterface $extractor)
44+
{
45+
if (isset($this->decorators[$name])) {
46+
throw new OverflowException("Decorator with the same name ('{$name}') already exists");
47+
}
48+
$this->decorators[$name] = $extractor;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Decorators;
17+
18+
19+
use OutOfBoundsException;
20+
use OverflowException;
21+
use Pinepain\JsSandbox\Decorators\Definitions\DecoratorInterface;
22+
23+
24+
interface DecoratorsCollectionInterface
25+
{
26+
/**
27+
* @param string $name
28+
*
29+
* @return DecoratorInterface
30+
* @throws OutOfBoundsException
31+
*/
32+
public function get(string $name): DecoratorInterface;
33+
34+
/**
35+
* @param string $name
36+
* @param DecoratorInterface $extractor
37+
*
38+
* @return mixed
39+
* @throws OverflowException
40+
*/
41+
public function put(string $name, DecoratorInterface $extractor);
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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\Decorators\Definitions;
17+
18+
19+
use Pinepain\JsSandbox\Wrappers\FunctionComponents\Runtime\ExecutionContextInterface;
20+
21+
22+
interface DecoratorInterface
23+
{
24+
public function decorate(callable $callback, ExecutionContextInterface $exec): callable;
25+
}

0 commit comments

Comments
 (0)