-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
]; | ||
} | ||
} |