Skip to content

Commit

Permalink
Converter -> ValueProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
aveiv committed Aug 19, 2020
1 parent 5711694 commit 1a3cedd
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 80 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ $reader['mixed_arr']
->getValue(); // returns [1, 2, 3, 4, 5]
```

## Use custom converters
## Use custom value processors

```php
class StripSpacesConverter implements ConverterInterface
class StripSpacesProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand All @@ -95,7 +95,7 @@ class StripSpacesConverter implements ConverterInterface
$reader = new ArrayReader([
'bad_float' => '9 999.99',
]);
$reader->registerConverter('stripSpaces', new StripSpacesConverter());
$reader->registerValueProcessor('stripSpaces', new StripSpacesProcessor());

$reader['bad_float']->stripSpaces()->toFloat()->getValue(); // return 9999.99
```
72 changes: 36 additions & 36 deletions src/ArrayReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace Aveiv\ArrayReader;

use Aveiv\ArrayReader\Converter\BoolConverter;
use Aveiv\ArrayReader\Converter\ConverterInterface;
use Aveiv\ArrayReader\Converter\DateTimeConverter;
use Aveiv\ArrayReader\Converter\FloatConverter;
use Aveiv\ArrayReader\Converter\IntConverter;
use Aveiv\ArrayReader\Converter\IsArrayConverter;
use Aveiv\ArrayReader\Converter\IsBoolConverter;
use Aveiv\ArrayReader\Converter\IsFloatConverter;
use Aveiv\ArrayReader\Converter\IsIntConverter;
use Aveiv\ArrayReader\Converter\IsStringConverter;
use Aveiv\ArrayReader\Converter\StringConverter;
use Aveiv\ArrayReader\ValueProcessor\ToBoolProcessor;
use Aveiv\ArrayReader\ValueProcessor\ValueProcessorInterface;
use Aveiv\ArrayReader\ValueProcessor\ToDateTimeProcessor;
use Aveiv\ArrayReader\ValueProcessor\ToFloatProcessor;
use Aveiv\ArrayReader\ValueProcessor\ToIntProcessor;
use Aveiv\ArrayReader\ValueProcessor\IsArrayProcessor;
use Aveiv\ArrayReader\ValueProcessor\IsBoolProcessor;
use Aveiv\ArrayReader\ValueProcessor\IsFloatProcessor;
use Aveiv\ArrayReader\ValueProcessor\IsIntProcessor;
use Aveiv\ArrayReader\ValueProcessor\IsStringProcessor;
use Aveiv\ArrayReader\ValueProcessor\ToStringProcessor;
use Aveiv\ArrayReader\Exception\MissingValueException;
use Aveiv\ArrayReader\Exception\ReadOnlyException;
use Aveiv\ArrayReader\Exception\UndefinedMethodException;
Expand All @@ -39,9 +39,9 @@ final class ArrayReader implements \ArrayAccess
private array $path = [];

/**
* @var ConverterInterface[]
* @var ValueProcessorInterface[]
*/
private array $converters = [];
private array $valueProcessors = [];

/**
* @psalm-param TValue $value
Expand All @@ -52,22 +52,22 @@ public function __construct($value)
{
$this->value = $value;

$this->registerConverter('isArray', new IsArrayConverter());
$this->registerConverter('isBool', new IsBoolConverter());
$this->registerConverter('isFloat', new IsFloatConverter());
$this->registerConverter('isInt', new IsIntConverter());
$this->registerConverter('isString', new IsStringConverter());

$this->registerConverter('toBool', new BoolConverter());
$this->registerConverter('toDateTime', new DateTimeConverter());
$this->registerConverter('toFloat', new FloatConverter());
$this->registerConverter('toInt', new IntConverter());
$this->registerConverter('toString', new StringConverter());
$this->registerValueProcessor('isArray', new IsArrayProcessor());
$this->registerValueProcessor('isBool', new IsBoolProcessor());
$this->registerValueProcessor('isFloat', new IsFloatProcessor());
$this->registerValueProcessor('isInt', new IsIntProcessor());
$this->registerValueProcessor('isString', new IsStringProcessor());

$this->registerValueProcessor('toBool', new ToBoolProcessor());
$this->registerValueProcessor('toDateTime', new ToDateTimeProcessor());
$this->registerValueProcessor('toFloat', new ToFloatProcessor());
$this->registerValueProcessor('toInt', new ToIntProcessor());
$this->registerValueProcessor('toString', new ToStringProcessor());
}

public function registerConverter(string $key, ConverterInterface $converter): void
public function registerValueProcessor(string $key, ValueProcessorInterface $processor): void
{
$this->converters[mb_strtolower($key)] = $converter;
$this->valueProcessors[mb_strtolower($key)] = $processor;
}

/**
Expand Down Expand Up @@ -199,31 +199,31 @@ public function map(callable $cb): self
*/
public function __call(string $name, array $arguments): self
{
$converterKey = mb_strtolower($name);
$processorKey = mb_strtolower($name);

if (!isset($this->converters[$converterKey])) {
if (!isset($this->valueProcessors[$processorKey])) {
throw new UndefinedMethodException(get_class($this), $name);
}

return $this->to($converterKey);
return $this->to($processorKey);
}

/**
* @param string $converterKey
* @param string $processorKey
* @return self
*/
private function to(string $converterKey): self
private function to(string $processorKey): self
{
$converterKey = mb_strtolower($converterKey);
$processorKey = mb_strtolower($processorKey);
$value = $this->value;
if ($this->hasValue()) {
try {
$value = $this->converters[$converterKey]($this->value);
$value = $this->valueProcessors[$processorKey]($this->value);
} catch (UnexpectedValueException $e) {
if ($pathAsStr = $this->pathAsStr()) {
$msg = sprintf('Cannot convert value "%s": "%s"', $this->pathAsStr(), $e->getMessage());
$msg = sprintf('Cannot process value "%s": "%s"', $this->pathAsStr(), $e->getMessage());
} else {
$msg = sprintf('Cannot convert value: "%s"', $e->getMessage());
$msg = sprintf('Cannot process value: "%s"', $e->getMessage());
}
throw new UnexpectedValueException($msg, intval($e->getCode()), $e);
}
Expand Down Expand Up @@ -319,7 +319,7 @@ private function newStatic($value, ?string $addPath = null): self
if ($addPath) {
$reader->path[] = $addPath;
}
$reader->converters = $this->converters;
$reader->valueProcessors = $this->valueProcessors;
return $reader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class IsArrayConverter implements ConverterInterface
final class IsArrayProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class IsBoolConverter implements ConverterInterface
final class IsBoolProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class IsFloatConverter implements ConverterInterface
final class IsFloatProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class IsIntConverter implements ConverterInterface
final class IsIntProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class IsStringConverter implements ConverterInterface
final class IsStringProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

final class BoolConverter implements ConverterInterface
final class ToBoolProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class DateTimeConverter implements ConverterInterface
final class ToDateTimeProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class FloatConverter implements ConverterInterface
final class ToFloatProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class IntConverter implements ConverterInterface
final class ToIntProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

use Aveiv\ArrayReader\Exception\UnexpectedValueException;

final class StringConverter implements ConverterInterface
final class ToStringProcessor implements ValueProcessorInterface
{
public function __invoke($value)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

declare(strict_types=1);

namespace Aveiv\ArrayReader\Converter;
namespace Aveiv\ArrayReader\ValueProcessor;

interface ConverterInterface
interface ValueProcessorInterface
{
/**
* @param mixed $value
Expand Down
Loading

0 comments on commit 1a3cedd

Please sign in to comment.