Skip to content

Commit

Permalink
add slug validator
Browse files Browse the repository at this point in the history
  • Loading branch information
tflori committed Apr 6, 2018
1 parent 56d51fe commit fa26c5e
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 7 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,11 @@ In this library the following validators are included:
- `NotEmpty`: Value must not be empty
- `Numeric`: Value must be numeric
- `PregMatch`: Value must match regular expression `$pattern`
- `Slug`: Value must contain only slug characters (a-z, 0-9, -, _)
- `StrLen`: String length from value must be between `$min` and `$max`
- `Truthful`: Converted to boolean the value must be true
- `Url`: Value must be a valid URL

Planned:

- `Slug`: Value must contain only slug characters (a-z, 0-9, -, _)

# Predefined Filters

The following filters are included in this library:
Expand Down
1 change: 1 addition & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @method static Validator\NotEmpty notEmpty()
* @method static Validator\Numeric numeric(string $decimalPoint = '.')
* @method static Validator\PregMatch pregMatch(string $pattern)
* @method static Validator\Slug slug()
* @method static Validator\StrLen strLen(int $min, int $max = 0)
* @method static Validator\Truthful truthful()
* @method static Validator\Url url(string $coverage = 'complete', $schemes = ['https', 'http', 'ftp'])
Expand Down
2 changes: 1 addition & 1 deletion src/Validator/Alpha.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(bool $allowSpaces = false)
public function validate($value, array $context = []): bool
{
$regex = '/^[\pL\pM' . ($this->allowSpaces ? ' ' : '') . ']*$/u';
if (preg_match_all($regex, $value)) {
if (preg_match($regex, $value)) {
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Validator/AlphaNumeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(bool $allowSpaces = false)
public function validate($value, array $context = []): bool
{
$regex = '/^[\pL\pM\pN' . ($this->allowSpaces ? ' ' : '') . ']*$/u';
if (preg_match_all($regex, $value)) {
if (preg_match($regex, $value)) {
return true;
}

Expand Down
31 changes: 31 additions & 0 deletions src/Validator/Slug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Verja\Validator;

use Verja\Error;
use Verja\Validator;

class Slug extends Validator
{
/**
* Validate $value
*
* @param mixed $value
* @param array $context
* @return bool
*/
public function validate($value, array $context = []): bool
{
$regex = '/^[0-9a-z-_]+$/u';
if (preg_match($regex, $value)) {
return true;
}

$this->error = new Error(
'NO_SLUG',
$value,
'value should be a valid slug'
);
return false;
}
}
2 changes: 1 addition & 1 deletion tests/Validator/AlphaNumericTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AlphaNumericTest extends TestCase
/** @dataProvider provideAlphanumericStrings
* @param $value
* @test */
public function acceptsStringsWithAlphabeticalCharacters($value)
public function acceptsStringsWithAlphanumericCharacters($value)
{
$validator = new AlphaNumeric();

Expand Down
75 changes: 75 additions & 0 deletions tests/Validator/SlugTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Verja\Test\Validator;

use Verja\Error;
use Verja\Test\TestCase;
use Verja\Validator\Slug;

class SlugTest extends TestCase
{
/** @dataProvider provideSlugs
* @param $value
* @test */
public function acceptsStringsWithSlugs($value)
{
$validator = new Slug();

$result = $validator->validate($value);

self::assertTrue($result);
}

/** @dataProvider provideNonSlugs
* @param $value
* @test */
public function doesNotAcceptOtherCharacters($value)
{
$validator = new Slug();

$result = $validator->validate($value);

self::assertFalse($result);
}


/** @test */
public function storesAnErrorMessage()
{
$validator = new Slug();

$validator->validate('foo?');

self::assertEquals(
new Error(
'NO_SLUG',
'foo?',
'value should be a valid slug'
),
$validator->getError()
);
}

public function provideSlugs()
{
return [
['42'],
['foobar'],
['foo-bar'],
['foo_bar'],
];
}

public function provideNonSlugs()
{
return [
['۴۲'], // Arabic 42
[''],
['à'],
['Müller'],
['Karīna'],
['string with space'],
[''], // empty is not valid
];
}
}

0 comments on commit fa26c5e

Please sign in to comment.