Added numeric check for slug attribut

This commit is contained in:
Kevin Frantz 2019-01-20 11:03:21 +01:00
parent 36c3c28a66
commit c5375d4e0d
3 changed files with 33 additions and 0 deletions

View File

@ -2,6 +2,8 @@
namespace App\Attribut; namespace App\Attribut;
use App\Exception\UnvalidValueException;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
@ -12,16 +14,30 @@ trait SlugAttribut
*/ */
protected $slug; protected $slug;
/**
* @param string $slug
*
* @throws UnvalidValueException
*/
public function setSlug(string $slug): void public function setSlug(string $slug): void
{ {
if (is_numeric($slug)) {
throw new UnvalidValueException('A slug must not be numeric!');
}
$this->slug = $slug; $this->slug = $slug;
} }
/**
* @return string
*/
public function getSlug(): string public function getSlug(): string
{ {
return $this->slug; return $this->slug;
} }
/**
* @return bool
*/
public function hasSlug(): bool public function hasSlug(): bool
{ {
return isset($this->slug); return isset($this->slug);

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exception;
/**
* @author kevinfrantz
*/
class UnvalidValueException extends \Exception
{
}

View File

@ -5,6 +5,7 @@ namespace Tests\Attribut;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use App\Attribut\SlugAttributInterface; use App\Attribut\SlugAttributInterface;
use App\Attribut\SlugAttribut; use App\Attribut\SlugAttribut;
use App\Exception\UnvalidValueException;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -39,4 +40,10 @@ class SlugAttributTest extends TestCase
$this->assertNull($this->slugAttribut->setSlug('')); $this->assertNull($this->slugAttribut->setSlug(''));
$this->assertTrue($this->slugAttribut->hasSlug()); $this->assertTrue($this->slugAttribut->hasSlug());
} }
public function testNumericSetException(): void
{
$this->expectException(UnvalidValueException::class);
$this->slugAttribut->setSlug('1234');
}
} }