Skip to content

Commit 4ea6b61

Browse files
author
Yann Eugoné
committed
Merge pull request #1 from ekkinox/enum-provider
Eloquent enum related exception messages
2 parents 8cba49d + b815008 commit 4ea6b61

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

Enum/AbstractTranslatedEnum.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class AbstractTranslatedEnum implements EnumInterface
3232
public function __construct(TranslatorInterface $translator, $transPattern)
3333
{
3434
if (false === strpos($transPattern, '%s')) {
35-
throw new InvalidTranslatePatternException($transPattern);
35+
throw InvalidTranslatePatternException::placeholderRequired($transPattern);
3636
}
3737

3838
$this->translator = $translator;

Exception/DuplicatedEnumException.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@
77
*/
88
class DuplicatedEnumException extends \BadMethodCallException
99
{
10-
//todo
10+
/**
11+
* @param string $name
12+
*
13+
* @return DuplicatedEnumException
14+
*/
15+
public static function alreadyRegistered($name)
16+
{
17+
return new self(sprintf('Enum with name "%s" is already registered', $name));
18+
}
1119
}

Exception/InvalidEnumException.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@
77
*/
88
class InvalidEnumException extends \DomainException
99
{
10-
//todo
10+
/**
11+
* @param string $name
12+
*
13+
* @return InvalidEnumException
14+
*/
15+
public static function nonexistent($name)
16+
{
17+
return new self(sprintf('Nonexistent enum with name "%s" in registry', $name));
18+
}
1119
}

Exception/InvalidTranslatePatternException.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,13 @@
77
*/
88
class InvalidTranslatePatternException extends \InvalidArgumentException
99
{
10-
//todo
10+
/**
11+
* @param string $transPattern
12+
*
13+
* @return InvalidTranslatePatternException
14+
*/
15+
public static function placeholderRequired($transPattern)
16+
{
17+
return new self(sprintf('Translation pattern "%s" must contain %%s placeholder', $transPattern));
18+
}
1119
}

Registry/EnumRegistry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class EnumRegistry implements EnumRegistryInterface
2222
public function add(EnumInterface $enum)
2323
{
2424
if ($this->has($enum->getName())) {
25-
throw new DuplicatedEnumException($enum->getName());
25+
throw DuplicatedEnumException::alreadyRegistered($enum->getName());
2626
}
2727

2828
$this->enums[$enum->getName()] = $enum;
@@ -34,7 +34,7 @@ public function add(EnumInterface $enum)
3434
public function get($name)
3535
{
3636
if (!$this->has($name)) {
37-
throw new InvalidEnumException($name);
37+
throw InvalidEnumException::nonexistent($name);
3838
}
3939

4040
return $this->enums[$name];

0 commit comments

Comments
 (0)