Skip to content

Commit

Permalink
Throw internal exception while fetching not decoded identifier from c…
Browse files Browse the repository at this point in the history
…ollection
  • Loading branch information
Krzysztof Grabania committed Nov 2, 2020
1 parent 3e89abe commit 81fc55b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
38 changes: 38 additions & 0 deletions src/Exceptions/MissingIdentifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace NGT\Barcode\GS1Decoder\Exceptions;

use InvalidArgumentException;

class MissingIdentifier extends InvalidArgumentException
{
/**
* The missing identifier code.
*
* @var string
*/
protected $identifierCode;

/**
* The exception constructor.
*
* @param string $identifierCode
*/
public function __construct(string $identifierCode)
{
parent::__construct("Missing identifier with \"{$identifierCode}\" code.");

$this->identifierCode = $identifierCode;
}

/**
* Get the missing identifier code.
*
* @return string
*/
public function getIdentifierCode(): string
{
return $this->identifierCode;
}
}
6 changes: 3 additions & 3 deletions src/IdentifierCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

namespace NGT\Barcode\GS1Decoder;

use InvalidArgumentException;
use NGT\Barcode\GS1Decoder\Contracts\Identifier;
use NGT\Barcode\GS1Decoder\Exceptions\MissingIdentifier;

class IdentifierCollection
{
Expand Down Expand Up @@ -53,15 +53,15 @@ public function has(string $code): bool
*
* @param string $code
* @return \NGT\Barcode\GS1Decoder\Contracts\Identifier
* @throws InvalidArgumentException
* @throws \NGT\Barcode\GS1Decoder\Exceptions\MissingIdentifier
*/
public function get(string $code): Identifier
{
if ($this->has($code)) {
return $this->items[$code];
}

throw new InvalidArgumentException("Missing identifier with code \"{$code}\".");
throw new MissingIdentifier($code);
}

/**
Expand Down

0 comments on commit 81fc55b

Please sign in to comment.