Skip to content

Added ArgumentIsNotNumericException as wrapper for InvalidArgumentExсeption #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"type": "library",
"description": "Implementation of the Luhn algorithm in PHP. Used in validation of credit card numbers and some national identification numbers.",
"prefer-stable": true,
"prefer-lowest": true,
"minimum-stability": "stable",
"keywords": [
"luhn",
Expand Down
40 changes: 40 additions & 0 deletions src/ArgumentIsNotNumericException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Niklas Ekman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

declare(strict_types=1);

namespace Nekman\LuhnAlgorithm;

class ArgumentIsNotNumericException extends \InvalidArgumentException
{
/**
* ArgumentIsNotNumericException constructor.
* @param string $number
*/
public function __construct(string $number)
{
parent::__construct("Expects \$number to be a number, \"{$number}\" given.", 0, null);
}
}
2 changes: 1 addition & 1 deletion src/LuhnAlgorithm.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function isValid(NumberInterface $number): bool
public function calcCheckDigit(NumberInterface $number): int
{
$checksum = $this->calcChecksum($number);

// Get the last digit of the checksum.
$checkDigit = $checksum % 10;

Expand Down
13 changes: 8 additions & 5 deletions src/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Number implements NumberInterface
public function __construct(string $number, int $checkDigit = null)
{
if (!is_numeric($number)) {
throw new \InvalidArgumentException("Expects \$number to be a number, \"{$number}\" given.");
throw new ArgumentIsNotNumericException($number);
}

$this->number = $number;
Expand All @@ -72,14 +72,16 @@ public static function fromString(string $input): self
$input = preg_replace('/[^\d]/', '', $input);

if (!is_numeric($input)) {
throw new \InvalidArgumentException("Expects \$input to be a number, \"{$input}\" given.");
throw new ArgumentIsNotNumericException($input);
}

$lastIndex = strlen($input) - 1;

// Get the last digit.
$checkDigit = (int) $input[strlen($input) - 1];
$checkDigit = (int) $input[$lastIndex];

// Remove the last digit.
$number = substr($input, 0, strlen($input) - 1);
$number = substr($input, 0, $lastIndex);

return new self($number, $checkDigit);
}
Expand All @@ -100,11 +102,12 @@ public function getCheckDigit()
return $this->checkDigit;
}


/**
* {@inheritdoc}
*/
public function __toString()
{
return (string) $this->number . (string) $this->checkDigit;
return $this->number . $this->checkDigit;
}
}