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

Commit

Permalink
Merge pull request #8 from pinepain/new-features
Browse files Browse the repository at this point in the history
Bug fixes and new features
  • Loading branch information
pinepain authored Nov 6, 2017
2 parents 344583c + f893e81 commit 2d30d0a
Show file tree
Hide file tree
Showing 36 changed files with 1,950 additions and 260 deletions.
55 changes: 55 additions & 0 deletions src/Decorators/DecoratorSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php declare(strict_types=1);

/*
* This file is part of the pinepain/js-sandbox PHP library.
*
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/


namespace Pinepain\JsSandbox\Decorators;


class DecoratorSpec implements DecoratorSpecInterface
{
/**
* @var string
*/
private $name;
/**
* @var array
*/
private $arguments;

/**
* @param string $name
* @param array $arguments
*/
public function __construct(string $name, array $arguments = [])
{
$this->name = $name;
$this->arguments = $arguments;
}

/**
* {@inheritdoc}
*/
public function getName(): string
{
return $this->name;
}

/**
* {@inheritdoc}
*/
public function getArguments(): array
{
return $this->arguments;
}
}
110 changes: 110 additions & 0 deletions src/Decorators/DecoratorSpecBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php declare(strict_types=1);

/*
* This file is part of the pinepain/js-sandbox PHP library.
*
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/


namespace Pinepain\JsSandbox\Decorators;


use Pinepain\JsSandbox\Specs\Builder\ArgumentValueBuilderInterface;
use Pinepain\JsSandbox\Specs\Builder\Exceptions\ArgumentValueBuilderException;


class DecoratorSpecBuilder implements DecoratorSpecBuilderInterface
{
private $regexp = '/
^
\@(?<name>[a-z_]+(?:[\w-]*\w)?)
\s*
(?:
\(
\s*
(?<params>
(
(?:[^\'\"\(\)\,\s]+) # literal
|
(?:[+-]?[0-9]+\.?[0-9]*) # numbers (no exponential notation)
|
(?:\'[^\']*\') # single-quoted string
|
(?:\"[^\"]*\") # double-quoted string
|
(?:\[\s*\]) # empty array
|
(?:\{\s*\}) # empty object
|
true | false | null
)(?:\s*\,\s*((?-3))*)*
)?
\s*
\)
)?
\s*
$
/xi';
/**
* @var ArgumentValueBuilderInterface
*/
private $argument;

/**
* @param ArgumentValueBuilderInterface $argument
*/
public function __construct(ArgumentValueBuilderInterface $argument)
{
$this->argument = $argument;
}

/**
* {@inheritdoc}
*/
public function build(string $definition): DecoratorSpecInterface
{
$definition = trim($definition);

if (!$definition) {
throw new DecoratorSpecBuilderException('Definition must be non-empty string');
}

try {
if (preg_match($this->regexp, $definition, $matches)) {

$params = array_slice($matches, 5);
$decorator = $this->buildDecorator($matches['name'], $params);

return $decorator;
}
} catch (ArgumentValueBuilderException $e) {
// We don't care about what specific issue we hit inside,
// for API user it means that the definition is invalid
}

throw new DecoratorSpecBuilderException("Unable to parse definition: '{$definition}'");
}

/**
* @param string $name
* @param array $raw_args
*
* @return DecoratorSpecInterface
*/
protected function buildDecorator(string $name, array $raw_args): DecoratorSpecInterface
{
$arguments = [];
foreach ($raw_args as $raw_arg) {
$arguments[] = $this->argument->build($raw_arg, true);
}

return new DecoratorSpec($name, $arguments);
}
}
24 changes: 24 additions & 0 deletions src/Decorators/DecoratorSpecBuilderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

/*
* This file is part of the pinepain/js-sandbox PHP library.
*
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/


namespace Pinepain\JsSandbox\Decorators;


use Pinepain\JsSandbox\Specs\Builder\Exceptions\SpecBuilderException;


class DecoratorSpecBuilderException extends SpecBuilderException
{
}
28 changes: 28 additions & 0 deletions src/Decorators/DecoratorSpecBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

/*
* This file is part of the pinepain/js-sandbox PHP library.
*
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/


namespace Pinepain\JsSandbox\Decorators;


interface DecoratorSpecBuilderInterface
{
/**
* @param string $definition
*
* @return DecoratorSpecInterface
* @throws DecoratorSpecBuilderException
*/
public function build(string $definition): DecoratorSpecInterface;
}
29 changes: 29 additions & 0 deletions src/Decorators/DecoratorSpecInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

/*
* This file is part of the pinepain/js-sandbox PHP library.
*
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/

namespace Pinepain\JsSandbox\Decorators;


interface DecoratorSpecInterface
{
/**
* @return string
*/
public function getName(): string;

/**
* @return array
*/
public function getArguments(): array;
}
50 changes: 50 additions & 0 deletions src/Decorators/DecoratorsCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php declare(strict_types=1);

/*
* This file is part of the pinepain/js-sandbox PHP library.
*
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/


namespace Pinepain\JsSandbox\Decorators;


use OutOfBoundsException;
use OverflowException;
use Pinepain\JsSandbox\Decorators\Definitions\DecoratorInterface;


class DecoratorsCollection implements DecoratorsCollectionInterface
{
protected $decorators = [];

/**
* {@inheritdoc}
*/
public function get(string $name): DecoratorInterface
{
if (!isset($this->decorators[$name])) {
throw new OutOfBoundsException("Decorator '{$name}' not found");
}

return $this->decorators[$name];
}

/**
* {@inheritdoc}
*/
public function put(string $name, DecoratorInterface $extractor)
{
if (isset($this->decorators[$name])) {
throw new OverflowException("Decorator with the same name ('{$name}') already exists");
}
$this->decorators[$name] = $extractor;
}
}
42 changes: 42 additions & 0 deletions src/Decorators/DecoratorsCollectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

/*
* This file is part of the pinepain/js-sandbox PHP library.
*
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/


namespace Pinepain\JsSandbox\Decorators;


use OutOfBoundsException;
use OverflowException;
use Pinepain\JsSandbox\Decorators\Definitions\DecoratorInterface;


interface DecoratorsCollectionInterface
{
/**
* @param string $name
*
* @return DecoratorInterface
* @throws OutOfBoundsException
*/
public function get(string $name): DecoratorInterface;

/**
* @param string $name
* @param DecoratorInterface $extractor
*
* @return mixed
* @throws OverflowException
*/
public function put(string $name, DecoratorInterface $extractor);
}
25 changes: 25 additions & 0 deletions src/Decorators/Definitions/DecoratorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php declare(strict_types=1);

/*
* This file is part of the pinepain/js-sandbox PHP library.
*
* Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
*
* Licensed under the MIT license: http://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the
* LICENSE file that was distributed with this source or visit
* http://opensource.org/licenses/MIT
*/


namespace Pinepain\JsSandbox\Decorators\Definitions;


use Pinepain\JsSandbox\Wrappers\FunctionComponents\Runtime\ExecutionContextInterface;


interface DecoratorInterface
{
public function decorate(callable $callback, ExecutionContextInterface $exec): callable;
}
Loading

0 comments on commit 2d30d0a

Please sign in to comment.