Permalink
Browse files

add namespaced exceptions (extending spl)

  • Loading branch information...
1 parent 22e60fc commit 422a709986ef89ae7476cc39c10be6e806b3250c @alcohol alcohol committed Nov 1, 2016
@@ -0,0 +1,5 @@
+<?php
+
+namespace League\ISO3166\Exception;
+
+final class DomainException extends \DomainException {}
@@ -0,0 +1,5 @@
+<?php
+
+namespace League\ISO3166\Exception;
+
+final class InvalidArgumentException extends \InvalidArgumentException {}
@@ -0,0 +1,5 @@
+<?php
+
+namespace League\ISO3166\Exception;
+
+final class OutOfBoundsException extends \OutOfBoundsException {}
View
@@ -9,24 +9,31 @@
namespace League\ISO3166;
+use League\ISO3166\Exception\DomainException;
+use League\ISO3166\Exception\InvalidArgumentException;
+
final class Guards
{
/**
* Assert that input looks like an alpha2 key.
*
* @param string $alpha2
*
- * @throws \InvalidArgumentException if input is not a string.
- * @throws \DomainException if input does not look like an alpha2 key.
+ * @throws \League\ISO3166\Exception\InvalidArgumentException if input is not a string.
+ * @throws \League\ISO3166\Exception\DomainException if input does not look like an alpha2 key.
*/
public static function guardAgainstInvalidAlpha2($alpha2)
{
if (!is_string($alpha2)) {
- throw new \InvalidArgumentException(sprintf('Expected $alpha2 to be of type string, got: %s', gettype($alpha2)));
+ throw new InvalidArgumentException(
+ sprintf('Expected $alpha2 to be of type string, got: %s', gettype($alpha2))
+ );
}
if (!preg_match('/^[a-zA-Z]{2}$/', $alpha2)) {
- throw new \DomainException(sprintf('Not a valid alpha2 key: %s', $alpha2));
+ throw new DomainException(
+ sprintf('Not a valid alpha2 key: %s', $alpha2)
+ );
}
}
@@ -35,17 +42,21 @@ public static function guardAgainstInvalidAlpha2($alpha2)
*
* @param string $alpha3
*
- * @throws \InvalidArgumentException if input is not a string.
- * @throws \DomainException if input does not look like an alpha3 key.
+ * @throws \League\ISO3166\Exception\InvalidArgumentException if input is not a string.
+ * @throws \League\ISO3166\Exception\DomainException if input does not look like an alpha3 key.
*/
public static function guardAgainstInvalidAlpha3($alpha3)
{
if (!is_string($alpha3)) {
- throw new \InvalidArgumentException(sprintf('Expected $alpha3 to be of type string, got: %s', gettype($alpha3)));
+ throw new InvalidArgumentException(
+ sprintf('Expected $alpha3 to be of type string, got: %s', gettype($alpha3))
+ );
}
if (!preg_match('/^[a-zA-Z]{3}$/', $alpha3)) {
- throw new \DomainException(sprintf('Not a valid alpha3 key: %s', $alpha3));
+ throw new DomainException(
+ sprintf('Not a valid alpha3 key: %s', $alpha3)
+ );
}
}
@@ -54,17 +65,21 @@ public static function guardAgainstInvalidAlpha3($alpha3)
*
* @param string $numeric
*
- * @throws \InvalidArgumentException if input is not a string.
- * @throws \DomainException if input does not look like a numeric key.
+ * @throws \League\ISO3166\Exception\InvalidArgumentException if input is not a string.
+ * @throws \League\ISO3166\Exception\DomainException if input does not look like a numeric key.
*/
public static function guardAgainstInvalidNumeric($numeric)
{
if (!is_string($numeric)) {
- throw new \InvalidArgumentException(sprintf('Expected $numeric to be of type string, got: %s', gettype($numeric)));
+ throw new InvalidArgumentException(
+ sprintf('Expected $numeric to be of type string, got: %s', gettype($numeric))
+ );
}
if (!preg_match('/^[0-9]{3}$/', $numeric)) {
- throw new \DomainException(sprintf('Not a valid numeric key: %s', $numeric));
+ throw new DomainException(
+ sprintf('Not a valid numeric key: %s', $numeric)
+ );
}
}
}
View
@@ -9,6 +9,9 @@
namespace League\ISO3166;
+use League\ISO3166\Exception\DomainException;
+use League\ISO3166\Exception\OutOfBoundsException;
+
final class ISO3166 implements \Countable, \IteratorAggregate, ISO3166DataProvider
{
/** @var string */
@@ -20,6 +23,9 @@
/** @var string */
const KEY_NUMERIC = 'numeric';
+ /** @var array */
+ const KEYS = [self::KEY_ALPHA2, self::KEY_ALPHA3, self::KEY_NUMERIC];
+
/**
* @param array $countries Replace default dataset with given array.
*/
@@ -71,17 +77,17 @@ public function all()
/**
* @param string $key
*
- * @throws \DomainException if an invalid key is specified.
+ * @throws \League\ISO3166\Exception\DomainException if an invalid key is specified.
*
* @return \Generator
*/
public function iterator($key = self::KEY_ALPHA2)
{
- if (!in_array($key, $keys = [self::KEY_ALPHA2, self::KEY_ALPHA3, self::KEY_NUMERIC], true)) {
- throw new \DomainException(sprintf(
+ if (!in_array($key, self::KEYS, true)) {
+ throw new DomainException(sprintf(
'Invalid value for $indexBy, got "%s", expected one of: %s',
$key,
- implode(', ', $keys)
+ implode(', ', self::KEYS)
));
}
@@ -124,7 +130,7 @@ public function getIterator()
* @param string $key
* @param string $value
*
- * @throws \OutOfBoundsException if key does not exist in dataset.
+ * @throws \League\ISO3166\Exception\OutOfBoundsException if key does not exist in dataset.
*
* @return array
*/
@@ -136,7 +142,9 @@ private function lookup($key, $value)
}
}
- throw new \OutOfBoundsException(sprintf('No "%s" key found matching: %s', $key, $value));
+ throw new OutOfBoundsException(
+ sprintf('No "%s" key found matching: %s', $key, $value)
+ );
}
/**
@@ -18,9 +18,9 @@
*
* @param string $alpha2
*
- * @throws \InvalidArgumentException if input is not a string.
- * @throws \DomainException if input does not look like an alpha2 key.
- * @throws \OutOfBoundsException if input does not exist in dataset.
+ * @throws \League\ISO3166\Exception\InvalidArgumentException if input is not a string.
+ * @throws \League\ISO3166\Exception\DomainException if input does not look like an alpha2 key.
+ * @throws \League\ISO3166\Exception\OutOfBoundsException if input does not exist in dataset.
*
* @return array
*/
@@ -33,9 +33,9 @@ public function alpha2($alpha2);
*
* @param string $alpha3
*
- * @throws \InvalidArgumentException if input is not a string.
- * @throws \DomainException if input does not look like an alpha3 key.
- * @throws \OutOfBoundsException if input does not exist in dataset.
+ * @throws \League\ISO3166\Exception\InvalidArgumentException if input is not a string.
+ * @throws \League\ISO3166\Exception\DomainException if input does not look like an alpha3 key.
+ * @throws \League\ISO3166\Exception\OutOfBoundsException if input does not exist in dataset.
*
* @return array
*/
@@ -48,9 +48,9 @@ public function alpha3($alpha3);
*
* @param string $numeric
*
- * @throws \InvalidArgumentException if input is not a string.
- * @throws \DomainException if input does not look like a numeric key.
- * @throws \OutOfBoundsException if input does not exist in dataset.
+ * @throws \League\ISO3166\Exception\InvalidArgumentException if input is not a string.
+ * @throws \League\ISO3166\Exception\DomainException if input does not look like a numeric key.
+ * @throws \League\ISO3166\Exception\OutOfBoundsException if input does not exist in dataset.
*
* @return array
*/
@@ -9,6 +9,8 @@
namespace League\ISO3166;
+use League\ISO3166\Exception\DomainException;
+
final class ISO3166DataValidator
{
/**
@@ -28,24 +30,24 @@ public function validate(array $data)
/**
* @param array $entry
*
- * @throws \DomainException if given data entry does not have all the required keys.
+ * @throws \League\ISO3166\Exception\DomainException if given data entry does not have all the required keys.
*/
private function assertEntryHasRequiredKeys(array $entry)
{
if (!isset($entry[ISO3166::KEY_ALPHA2])) {
- throw new \DomainException('Each data entry must have a valid alpha2 key.');
+ throw new DomainException('Each data entry must have a valid alpha2 key.');
}
Guards::guardAgainstInvalidAlpha2($entry[ISO3166::KEY_ALPHA2]);
if (!isset($entry[ISO3166::KEY_ALPHA3])) {
- throw new \DomainException('Each data entry must have a valid alpha3 key.');
+ throw new DomainException('Each data entry must have a valid alpha3 key.');
}
Guards::guardAgainstInvalidAlpha3($entry[ISO3166::KEY_ALPHA3]);
if (!isset($entry[ISO3166::KEY_NUMERIC])) {
- throw new \DomainException('Each data entry must have a valid numeric key.');
+ throw new DomainException('Each data entry must have a valid numeric key.');
}
Guards::guardAgainstInvalidNumeric($entry[ISO3166::KEY_NUMERIC]);
@@ -9,6 +9,8 @@
namespace League\ISO3166;
+use League\ISO3166\Exception\DomainException;
+
class ISO3166DataValidatorTest extends \PHPUnit_Framework_TestCase
{
/** @var ISO3166DataValidator */
@@ -44,15 +46,15 @@ public function testcases()
return [
[
[[ISO3166::KEY_ALPHA3 => 'FOO', ISO3166::KEY_NUMERIC => '001']],
- \DomainException::class,
+ DomainException::class,
'{^Each data entry must have a valid alpha2 key.$}',
], [
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_NUMERIC => '001']],
- \DomainException::class,
+ DomainException::class,
'{^Each data entry must have a valid alpha3 key.$}',
], [
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_ALPHA3 => 'FOO']],
- \DomainException::class,
+ DomainException::class,
'{^Each data entry must have a valid numeric key.$}',
], [
[[ISO3166::KEY_ALPHA2 => 'FO', ISO3166::KEY_ALPHA3 => 'FOO', ISO3166::KEY_NUMERIC => '001']],
View
@@ -9,6 +9,10 @@
namespace League\ISO3166;
+use League\ISO3166\Exception\DomainException;
+use League\ISO3166\Exception\InvalidArgumentException;
+use League\ISO3166\Exception\OutOfBoundsException;
+
class ISO3166Test extends \PHPUnit_Framework_TestCase
{
/** @var array */
@@ -59,11 +63,11 @@ public function invalidAlpha2Provider()
$noMatch = sprintf('{^No "%s" key found matching: .*$}', ISO3166::KEY_ALPHA2);
return [
- ['A', \DomainException::class, $invalidNumeric],
- ['ABC', \DomainException::class, $invalidNumeric],
- [1, \InvalidArgumentException::class, $expectedString],
- [123, \InvalidArgumentException::class, $expectedString],
- ['AB', \OutOfBoundsException::class, $noMatch],
+ ['A', DomainException::class, $invalidNumeric],
+ ['ABC', DomainException::class, $invalidNumeric],
+ [1, InvalidArgumentException::class, $expectedString],
+ [123, InvalidArgumentException::class, $expectedString],
+ ['AB', OutOfBoundsException::class, $noMatch],
];
}
@@ -101,11 +105,11 @@ public function invalidAlpha3Provider()
$noMatch = sprintf('{^No "%s" key found matching: .*$}', ISO3166::KEY_ALPHA3);
return [
- ['AB', \DomainException::class, $invalidNumeric],
- ['ABCD', \DomainException::class, $invalidNumeric],
- [12, \InvalidArgumentException::class, $expectedString],
- [1234, \InvalidArgumentException::class, $expectedString],
- ['ABC', \OutOfBoundsException::class, $noMatch],
+ ['AB', DomainException::class, $invalidNumeric],
+ ['ABCD', DomainException::class, $invalidNumeric],
+ [12, InvalidArgumentException::class, $expectedString],
+ [1234, InvalidArgumentException::class, $expectedString],
+ ['ABC', OutOfBoundsException::class, $noMatch],
];
}
@@ -143,15 +147,15 @@ public function invalidNumericProvider()
$noMatch = sprintf('{^No "%s" key found matching: .*$}', ISO3166::KEY_NUMERIC);
return [
- ['00', \DomainException::class, $invalidNumeric],
- ['0000', \DomainException::class, $invalidNumeric],
- ['AB', \DomainException::class, $invalidNumeric],
- ['ABC', \DomainException::class, $invalidNumeric],
- ['ABCD', \DomainException::class, $invalidNumeric],
- [12, \InvalidArgumentException::class, $expectedString],
- [123, \InvalidArgumentException::class, $expectedString],
- [1234, \InvalidArgumentException::class, $expectedString],
- ['000', \OutOfBoundsException::class, $noMatch],
+ ['00', DomainException::class, $invalidNumeric],
+ ['0000', DomainException::class, $invalidNumeric],
+ ['AB', DomainException::class, $invalidNumeric],
+ ['ABC', DomainException::class, $invalidNumeric],
+ ['ABCD', DomainException::class, $invalidNumeric],
+ [12, InvalidArgumentException::class, $expectedString],
+ [123, InvalidArgumentException::class, $expectedString],
+ [1234, InvalidArgumentException::class, $expectedString],
+ ['000', OutOfBoundsException::class, $noMatch],
];
}
@@ -195,7 +199,7 @@ public function testListBy()
// void
}
} catch (\Exception $e) {
- $this->assertInstanceOf('DomainException', $e);
+ $this->assertInstanceOf('League\ISO3166\Exception\DomainException', $e);
$this->assertRegExp('{Invalid value for \$indexBy, got "\w++", expected one of:(?: \w++,?)+}', $e->getMessage());
} finally {
$this->assertTrue(isset($e));

0 comments on commit 422a709

Please sign in to comment.