Skip to content

Commit

Permalink
new release
Browse files Browse the repository at this point in the history
  • Loading branch information
Smoren committed Aug 22, 2022
1 parent 35ee538 commit f51ed56
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 75 deletions.
45 changes: 37 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ $schema = [
];

$schemator = (new SchematorBuilder())->get();
$output = $schemator->exec($input, $schema);
$output = $schemator->convert($input, $schema);

print_r($output);
/* Array
Expand Down Expand Up @@ -118,6 +118,35 @@ print_r($output);
*/
```

#### Setting errors level

```php
use Smoren\Schemator\Factories\SchematorBuilder;
use Smoren\Schemator\Structs\ErrorsLevelMask;
use Smoren\Schemator\Exceptions\SchematorException;

$input = [
'some_key' => null,
];
$schema = [
'my_value' => ['some_key', ['date', 'Y-m-d']],
];

$schemator = (new SchematorBuilder())
->withErrorsLevelMask(
ErrorsLevelMask::nothing()
->add([SchematorException::FILTER_ERROR, SchematorException::CANNOT_GET_VALUE])
)
->get();

try {
$schemator->convert($input, $schema);
} catch(SchematorException $e) {
echo $e->getMessage(); // filter error: 'date'
}

```

#### Using base filters

```php
Expand Down Expand Up @@ -169,7 +198,7 @@ $schema = [
$schemator = (new SchematorBuilder())
->withFilters(new BaseFiltersStorage())
->get();
$output = $schemator->exec($input, $schema);
$output = $schemator->convert($input, $schema);

print_r($output);
/*
Expand Down Expand Up @@ -211,13 +240,13 @@ use Smoren\Schemator\Factories\SchematorBuilder;
use Smoren\Schemator\Filters\BaseFiltersStorage;

$schemator = (new SchematorBuilder())
->withFilters(BaseFiltersStorage())
->withFilters(new BaseFiltersStorage())
->get();
$input = [
'numbers' => [-1, 10, 5, 22, -10, 0, 35, 7, 8, 9, 0],
];

$output = $schemator->exec($input, [
$output = $schemator->convert($input, [
'positive' => [
'numbers',
['filter', [['>', 0]]],
Expand Down Expand Up @@ -269,7 +298,7 @@ Array
)
*/

$output = $schemator->exec($input, [
$output = $schemator->convert($input, [
'number_types' => ['numbers', [
'replace',
[
Expand Down Expand Up @@ -326,7 +355,7 @@ $schema = [
'street_names' => ['streets', ['startsWith', 'T'], ['implode', ', ']],
];
$output = $schemator->exec($input, $schema);
$output = $schemator->convert($input, $schema);
print_r($output);
/*
Expand All @@ -340,10 +369,10 @@ Array
#### Mass usage
```php
use Smoren\Schemator\Components\Schemator;
use Smoren\Schemator\Factories\SchematorBuilder;
use Smoren\Schemator\Components\MassSchemator;
$massSchemator = new MassSchemator(new Schemator());
$massSchemator = new MassSchemator((new SchematorBuilder())->get());
$cities = [
[
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": ">=7.4.0",
"smoren/extended-exceptions": "1.0.0",
"smoren/helpers": "^0.1.4",
"smoren/bitmap-tools": "^0.2.0"
"smoren/bitmap-tools": "^0.3.0"
},
"require-dev": {
"codeception/codeception": "^4.2.1",
Expand Down
29 changes: 10 additions & 19 deletions src/Components/Schemator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Smoren\Schemator\Interfaces\NestedAccessorFactoryInterface;
use Smoren\Schemator\Interfaces\SchematorInterface;
use Smoren\Schemator\Factories\NestedAccessorFactory;
use Smoren\Schemator\Structs\ErrorsLevelMask;
use Smoren\Schemator\Structs\FilterContext;
use Smoren\Schemator\Exceptions\NestedAccessorException;
use Smoren\Schemator\Exceptions\SchematorException;
Expand All @@ -18,8 +19,6 @@
*/
class Schemator implements SchematorInterface
{
public const ERRORS_LEVEL_DEFAULT = 114;

/**
* @var array<string, callable> filters map
*/
Expand All @@ -37,34 +36,25 @@ class Schemator implements SchematorInterface
*/
protected NestedAccessorFactoryInterface $nestedAccessorFactory;

/**
* Creates bitmap errors level mask
* @param array<int> $errorCodes
* @return int
*/
public static function createErrorsLevelMask(array $errorCodes): int
{
return Bitmap::create($errorCodes)->getValue();
}

/**
* Schemator constructor.
* @param non-empty-string $pathDelimiter delimiter for multilevel paths
* @param int $errorsLevelMask bitmap errors level mask
* @param BitmapInterface|null $errorsLevelMask bitmap errors level mask
* @param NestedAccessorFactoryInterface|null $nestedAccessorFactory nested accessor factory
*/
public function __construct(
string $pathDelimiter = '.',
int $errorsLevelMask = self::ERRORS_LEVEL_DEFAULT,
?BitmapInterface $errorsLevelMask = null,
NestedAccessorFactoryInterface $nestedAccessorFactory = null
) {
$this->pathDelimiter = $pathDelimiter;
$this->setErrorsLevelMask($errorsLevelMask);
$this->errorsLevelMask = $errorsLevelMask ?? ErrorsLevelMask::default();
$this->nestedAccessorFactory = $nestedAccessorFactory ?? new NestedAccessorFactory();
}

/**
* @inheritDoc
* @throws NestedAccessorException
* @throws SchematorException
*/
public function convert($source, array $schema)
{
Expand All @@ -83,6 +73,7 @@ public function convert($source, array $schema)

/**
* @inheritDoc
* @throws SchematorException
*/
public function getValue($source, $key)
{
Expand Down Expand Up @@ -116,9 +107,9 @@ public function setPathDelimiter(string $value): void
/**
* @inheritDoc
*/
public function setErrorsLevelMask(int $value): void
public function setErrorsLevelMask(BitmapInterface $value): void
{
$this->errorsLevelMask = Bitmap::create($value);
$this->errorsLevelMask = $value;
}

/**
Expand Down Expand Up @@ -228,7 +219,7 @@ protected function runFilter(array $filterConfig, $source, $rootSource)

try {
return $this->filterMap[$filterName](
new FilterContext($this, $source, $rootSource),
new FilterContext($this, $source, $rootSource, $filterConfig),
...$filterConfig
);
} catch(SchematorException $e) {
Expand Down
6 changes: 3 additions & 3 deletions src/Exceptions/SchematorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public static function createAsFilterNotFound(string $filterName): SchematorExce
* @param string $filterName name of the filter
* @param mixed $filterConfig arguments for filter
* @param mixed $source source for filtering
* @param Throwable $previous exception thrown in the filter body
* @param ?Throwable $previous exception thrown in the filter body
* @return SchematorException
*/
public static function createAsFilterError(
string $filterName,
$filterConfig,
$source,
Throwable $previous
?Throwable $previous = null
): SchematorException {
return new SchematorException(
"filter error: '{$filterName}'",
SchematorException::FILTER_ERROR,
$previous,
[
'error' => $previous->getMessage(),
'error' => $previous ? $previous->getMessage() : "filter error: '{$filterName}'",
'filter_name' => $filterName,
'config' => $filterConfig,
'source' => $source,
Expand Down
3 changes: 2 additions & 1 deletion src/Factories/SchematorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Smoren\Schemator\Factories;

use Smoren\BitmapTools\Interfaces\BitmapInterface;
use Smoren\Schemator\Interfaces\SchematorBuilderInterface;
use Smoren\Schemator\Interfaces\SchematorInterface;
use Smoren\Schemator\Components\Schemator;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function withPathDelimiter(string $pathDelimiter): SchematorBuilderInterf
/**
* @inheritDoc
*/
public function withErrorsLevelMask(int $errorsLevelMask): SchematorBuilderInterface
public function withErrorsLevelMask(BitmapInterface $errorsLevelMask): SchematorBuilderInterface
{
$this->schemator->setErrorsLevelMask($errorsLevelMask);
return $this;
Expand Down
Loading

0 comments on commit f51ed56

Please sign in to comment.