Skip to content

Commit

Permalink
Library extension (#44)
Browse files Browse the repository at this point in the history
* Extended the functionality of the library in order to support lookup by name identifier.

* Composer update and php-cs-fixer update.

* phpunit tests and corrections

* Updated the docs.

* Changed the name of GB to the initial one. Removed the extra whitespace. Changed the order of the class constants as well as the order in the keys array.

* Removed .idea from the project .gitignore and added it to the global.
  • Loading branch information
npispas authored and alcohol committed Jan 2, 2018
1 parent 81c6a2c commit ec7edf7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
/.php_cs
/.php_cs.cache
/composer.lock
/phpunit.xml
/phpunit.xml
6 changes: 6 additions & 0 deletions docs/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ title: Using league/iso3166

The following methods are provided by the `League\ISO3166\ISO3166DataProvider` interface.

**Lookup data by name:**

``` php
$data = (new League\ISO3166\ISO3166)->name($name);
```

**Lookup data by alpha2 code:**

``` php
Expand Down
16 changes: 16 additions & 0 deletions src/Guards.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@

final class Guards
{
/**
* Assert that input looks like a name key.
*
* @param string $name
*
* @throws \League\ISO3166\Exception\InvalidArgumentException if input is not a string
*/
public static function guardAgainstInvalidName($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException(
sprintf('Expected $name to be of type string, got: %s', gettype($name))
);
}
}

/**
* Assert that input looks like an alpha2 key.
*
Expand Down
16 changes: 15 additions & 1 deletion src/ISO3166.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ final class ISO3166 implements \Countable, \IteratorAggregate, ISO3166DataProvid
* @var string
*/
const KEY_NUMERIC = 'numeric';
/**
* @var string
*/
const KEY_NAME = 'name';
/**
* @var string[]
*/
private $keys = [self::KEY_ALPHA2, self::KEY_ALPHA3, self::KEY_NUMERIC];
private $keys = [self::KEY_ALPHA2, self::KEY_ALPHA3, self::KEY_NUMERIC, self::KEY_NAME];

/**
* @param array[] $countries replace default dataset with given array
Expand All @@ -41,6 +45,16 @@ public function __construct(array $countries = [])
}
}

/**
* {@inheritdoc}
*/
public function name($name)
{
Guards::guardAgainstInvalidName($name);

return $this->lookup(self::KEY_NAME, $name);
}

/**
* {@inheritdoc}
*/
Expand Down
14 changes: 14 additions & 0 deletions src/ISO3166DataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@

interface ISO3166DataProvider
{
/**
* Lookup ISO3166-1 data by name identifier.
*
* @api
*
* @param string $name
*
* @throws \League\ISO3166\Exception\InvalidArgumentException if input is not a string
* @throws \League\ISO3166\Exception\OutOfBoundsException if input does not exist in dataset
*
* @return array
*/
public function name($name);

/**
* Lookup ISO3166-1 data by alpha2 identifier.
*
Expand Down

0 comments on commit ec7edf7

Please sign in to comment.