Skip to content
This repository was archived by the owner on Jul 8, 2023. It is now read-only.

Commit 075664d

Browse files
committed
Implemented customizable exception. Closes #13.
1 parent 5477d9e commit 075664d

13 files changed

+190
-147
lines changed

src/Eloquent/Enumeration/Enumeration.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Eloquent\Enumeration;
1313

14+
use Exception;
15+
1416
/**
1517
* Base class for C++ style enumerations.
1618
*/
@@ -22,7 +24,7 @@ abstract class Enumeration extends Multiton
2224
* @param scalar $value The value associated with the member instance.
2325
*
2426
* @return Enumeration The first member instance with the supplied value.
25-
* @throws UndefinedInstanceException If no associated instance is found.
27+
* @throws Exception\UndefinedInstanceException If no associated instance is found.
2628
*/
2729
public static final function instanceByValue($value)
2830
{
@@ -32,7 +34,7 @@ public static final function instanceByValue($value)
3234
}
3335
}
3436

35-
throw new Exception\UndefinedInstanceException(get_called_class(), 'value', $value);
37+
throw static::createUndefinedInstanceException(get_called_class(), 'value', $value);
3638
}
3739

3840
/**
@@ -66,7 +68,7 @@ protected static final function initializeMultiton()
6668
* @param string $key The string key to associate with this member instance.
6769
* @param scalar $value The value of this member instance.
6870
*
69-
* @throws ExtendsConcreteException If the constructed instance has an invalid inheritance hierarchy.
71+
* @throws Exception\ExtendsConcreteException If the constructed instance has an invalid inheritance hierarchy.
7072
*/
7173
protected function __construct($key, $value)
7274
{

src/Eloquent/Enumeration/Exception/Exception.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Eloquent/Enumeration/Exception/ExtendsConcreteException.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Eloquent\Enumeration\Exception;
1313

14+
use Exception;
15+
use LogicException;
16+
1417
/**
1518
* The supplied member instance extends an already concrete base class.
1619
*
@@ -22,12 +25,42 @@ final class ExtendsConcreteException extends LogicException
2225
/**
2326
* Construct a new ExtendsConcreteException instance.
2427
*
25-
* @param string $class The class of the supplied instance.
28+
* @param string $className The class of the supplied instance.
2629
* @param string $parentClass The concrete parent class.
27-
* @param \Exception $previous The previous exception, if any.
30+
* @param Exception $previous The previous exception, if any.
31+
*/
32+
public function __construct($className, $parentClass, Exception $previous = null)
33+
{
34+
$this->className = $className;
35+
$this->parentClass = $parentClass;
36+
37+
parent::__construct(
38+
sprintf(
39+
"Class '%s' cannot extend concrete class '%s'.",
40+
$this->className(),
41+
$this->parentClass()
42+
),
43+
0,
44+
$previous
45+
);
46+
}
47+
48+
/**
49+
* @return string
2850
*/
29-
public function __construct($class, $parentClass, \Exception $previous = null)
51+
public function className()
3052
{
31-
parent::__construct("Class '".$class."' cannot extend concrete class '".$parentClass."'.", $previous);
53+
return $this->className;
3254
}
55+
56+
/**
57+
* @return string
58+
*/
59+
public function parentClass()
60+
{
61+
return $this->parentClass;
62+
}
63+
64+
private $className;
65+
private $parentClass;
3366
}

src/Eloquent/Enumeration/Exception/LogicException.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Eloquent/Enumeration/Exception/UndefinedInstanceException.php

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,65 @@
1111

1212
namespace Eloquent\Enumeration\Exception;
1313

14+
use Exception;
15+
use LogicException;
16+
1417
/**
1518
* The requested member instance was not found.
1619
*/
17-
final class UndefinedInstanceException extends LogicException
20+
final class UndefinedInstanceException extends LogicException implements UndefinedInstanceExceptionInterface
1821
{
1922
/**
2023
* Construct a new UndefinedInstanceException instance.
2124
*
22-
* @param string $class The class from which the member was requested.
25+
* @param string $className The class from which the member was requested.
2326
* @param string $property The name of the property used to search for the member.
24-
* @param string $value The value of the property used to search for the member.
25-
* @param \Exception $previous The previous exception, if any.
27+
* @param mixed $value The value of the property used to search for the member.
28+
* @param Exception $previous The previous exception, if any.
29+
*/
30+
public function __construct($className, $property, $value, Exception $previous = null)
31+
{
32+
$this->className = $className;
33+
$this->property = $property;
34+
$this->value = $value;
35+
36+
parent::__construct(
37+
sprintf(
38+
"No instance with %s equal to %s defined in class '%s'.",
39+
$this->property(),
40+
var_export($this->value(), true),
41+
$this->className()
42+
),
43+
0,
44+
$previous
45+
);
46+
}
47+
48+
/**
49+
* @return string
50+
*/
51+
public function className()
52+
{
53+
return $this->className;
54+
}
55+
56+
/**
57+
* @return string
2658
*/
27-
public function __construct($class, $property, $value, \Exception $previous = null)
59+
public function property()
2860
{
29-
$message =
30-
"No instance with ".
31-
$property.
32-
" equal to ".
33-
var_export($value, true).
34-
" defined in class '".
35-
$class.
36-
"'."
37-
;
38-
39-
parent::__construct($message, $previous);
61+
return $this->property;
4062
}
63+
64+
/**
65+
* @return mixed
66+
*/
67+
public function value()
68+
{
69+
return $this->value;
70+
}
71+
72+
private $className;
73+
private $property;
74+
private $value;
4175
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Enumeration package.
5+
*
6+
* Copyright © 2012 Erin Millard
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
// @codeCoverageIgnoreStart
13+
14+
namespace Eloquent\Enumeration\Exception;
15+
16+
/**
17+
* Interface used to mark all exceptions thrown when an undefined instance is requested.
18+
*/
19+
interface UndefinedInstanceExceptionInterface
20+
{
21+
/**
22+
* @return string
23+
*/
24+
public function className();
25+
26+
/**
27+
* @return string
28+
*/
29+
public function property();
30+
31+
/**
32+
* @return mixed
33+
*/
34+
public function value();
35+
}

src/Eloquent/Enumeration/Multiton.php

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static final function multitonInstances()
3838
* @param string $key The string key associated with the member instance.
3939
*
4040
* @return Multiton The member instance associated with the given string key.
41-
* @throws UndefinedInstanceException If no associated instance is found.
41+
* @throws Exception\UndefinedInstanceException If no associated instance is found.
4242
*/
4343
public static final function instanceByKey($key)
4444
{
@@ -47,7 +47,7 @@ public static final function instanceByKey($key)
4747
return $instances[$key];
4848
}
4949

50-
throw new Exception\UndefinedInstanceException(get_called_class(), 'key', $key);
50+
throw static::createUndefinedInstanceException(get_called_class(), 'key', $key);
5151
}
5252

5353
/**
@@ -57,15 +57,16 @@ public static final function instanceByKey($key)
5757
* @param mixed $value The value to match.
5858
*
5959
* @return Multiton The first member instance for which $instance->{$property}() === $value.
60-
* @throws UndefinedInstanceException If no associated instance is found.
60+
* @throws Exception\UndefinedInstanceException If no associated instance is found.
6161
*/
6262
public static final function instanceBy($property, $value) {
6363
foreach (static::multitonInstances() as $instance) {
6464
if ($instance->{$property}() === $value) {
6565
return $instance;
6666
}
6767
}
68-
throw new Exception\UndefinedInstanceException(get_called_class(), $property, $value);
68+
69+
throw static::createUndefinedInstanceException(get_called_class(), $property, $value);
6970
}
7071

7172
/**
@@ -74,15 +75,16 @@ public static final function instanceBy($property, $value) {
7475
* @param callback $predicate The predicate applies to the multiton instance to find a match.
7576
*
7677
* @return Multiton The first member instance for which $predicate($instance) evaluates to boolean true.
77-
* @throws UndefinedInstanceException If no associated instance is found.
78+
* @throws Exception\UndefinedInstanceException If no associated instance is found.
7879
*/
7980
public static final function instanceByPredicate($predicate) {
8081
foreach (static::multitonInstances() as $instance) {
8182
if ($predicate($instance)) {
8283
return $instance;
8384
}
8485
}
85-
throw new Exception\UndefinedInstanceException(get_called_class(), '<callback>', '<callback>');
86+
87+
throw static::createUndefinedInstanceException(get_called_class(), '<callback>', '<callback>');
8688
}
8789

8890
/**
@@ -92,7 +94,7 @@ public static final function instanceByPredicate($predicate) {
9294
* @param array $arguments Ignored.
9395
*
9496
* @return Multiton The member instance associated with the given string key.
95-
* @throws UndefinedInstanceException If no associated instance is found.
97+
* @throws Exception\UndefinedInstanceException If no associated instance is found.
9698
*/
9799
public static final function __callStatic($key, array $arguments)
98100
{
@@ -131,6 +133,26 @@ public function __toString()
131133
*/
132134
protected static function initializeMultiton() {}
133135

136+
/**
137+
* Override this method in child classes to implement custom undefined
138+
* instance exceptions for a multiton class.
139+
*
140+
* @param string $className
141+
* @param string $property
142+
* @param mixed $value
143+
* @param Exception|null $previous
144+
*
145+
* @return Exception\UndefinedInstanceExceptionInterface
146+
*/
147+
protected static function createUndefinedInstanceException(
148+
$className,
149+
$property,
150+
$value,
151+
Exception $previous = null
152+
) {
153+
return new Exception\UndefinedInstanceException($className, $property, $value, $previous);
154+
}
155+
134156
/**
135157
* Construct and register a new multiton member instance.
136158
*
@@ -141,7 +163,7 @@ protected static function initializeMultiton() {}
141163
*
142164
* @param string $key The string key to associate with this member instance.
143165
*
144-
* @throws ExtendsConcreteException If the constructed instance has an invalid inheritance hierarchy.
166+
* @throws Exception\ExtendsConcreteException If the constructed instance has an invalid inheritance hierarchy.
145167
*/
146168
protected function __construct($key)
147169
{
@@ -158,7 +180,7 @@ protected function __construct($key)
158180
* also handle registration of the instance.
159181
*
160182
* @param Multiton $instance The instance to register.
161-
* @throws ExtendsConcreteException If the supplied instance has an invalid inheritance hierarchy.
183+
* @throws Exception\ExtendsConcreteException If the supplied instance has an invalid inheritance hierarchy.
162184
*/
163185
private static function registerMultiton(self $instance)
164186
{

test/suite/Eloquent/Enumeration/EnumerationTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313

1414
use Eloquent\Enumeration\Test\Fixture\InvalidEnumeration;
1515
use Eloquent\Enumeration\Test\Fixture\ValidEnumeration;
16+
use PHPUnit_Framework_TestCase;
17+
use ReflectionClass;
1618

17-
/**
18-
* @covers Eloquent\Enumeration\Enumeration
19-
*/
20-
class EnumerationTest extends \PHPUnit_Framework_TestCase
19+
class EnumerationTest extends PHPUnit_Framework_TestCase
2120
{
2221
protected function setUp()
2322
{
2423
parent::setUp();
2524

26-
$reflector = new \ReflectionClass(__NAMESPACE__.'\Multiton');
25+
$reflector = new ReflectionClass(__NAMESPACE__.'\Multiton');
2726
$instancesProperty = $reflector->getProperty('instances');
2827
$instancesProperty->setAccessible(true);
2928
$instancesProperty->setValue(null, array());

0 commit comments

Comments
 (0)