Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pattern() #6

Merged
merged 1 commit into from Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions readme.md
Expand Up @@ -222,6 +222,16 @@ Ranges of numbers are specified using a combination of `min()` and `max()`:
$schema = Expect::int()->min(10)->max(20);
```

Regular expressions
-------------------

String can be restricted by regular expression using the `pattern()`:

```php
// just 9 numbers
$schema = Expect::pattern('\d{9}');
```

Data mapping to objects
-----------------------

Expand Down
13 changes: 13 additions & 0 deletions src/Schema/Elements/Type.php
Expand Up @@ -30,6 +30,9 @@ final class Type implements Schema
/** @var array */
private $range = [null, null];

/** @var string|null */
private $pattern;


public function __construct(string $type)
{
Expand Down Expand Up @@ -77,6 +80,13 @@ public function items($type = 'mixed'): self
}


public function pattern(string $pattern): self
{
$this->pattern = $pattern;
return $this;
}


/********************* processing ****************d*g**/


Expand Down Expand Up @@ -126,6 +136,9 @@ public function complete($value, Context $context)
}

$expected = $this->type . ($this->range === [null, null] ? '' : ':' . implode('..', $this->range));
if ($this->pattern !== null) {
$expected = $this->type . ':' . $this->pattern;
}
if (!$this->doValidate($value, $expected, $context)) {
return;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Schema/Expect.php
Expand Up @@ -43,6 +43,14 @@ public static function __callStatic(string $name, array $args): Type
}


public static function pattern(string $pattern): Type
{
$type = new Type('pattern');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be Type('string'), or isn't?

$type->pattern($pattern);
return $type;
}


public static function type(string $type): Type
{
return new Type($type);
Expand Down
26 changes: 26 additions & 0 deletions tests/Schema/Expect.pattern.phpt
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

use Nette\Schema\Expect;
use Nette\Schema\Processor;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test(function () {
$schema = Expect::pattern('\d{9}');

Assert::same('123456789', (new Processor)->process($schema, '123456789'));
});


test(function () {
$schema = Expect::pattern('\d{9}');

checkValidationErrors(function () use ($schema) {
(new Processor)->process($schema, '123');
}, ['The option expects to be pattern in range \d{9}, string \'123\' given.']);
});