From 1ffede9bddf421885a63e29232af7b977b4df2c9 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 11 Mar 2022 11:25:31 +0100 Subject: [PATCH 1/2] Implement new __serialize() and __unserialize() functions --- src/Number.php | 160 ++++++++++++++++++++++++++----------------------- 1 file changed, 85 insertions(+), 75 deletions(-) diff --git a/src/Number.php b/src/Number.php index 6dba49a..7aa8de0 100644 --- a/src/Number.php +++ b/src/Number.php @@ -38,79 +38,89 @@ */ class Number implements NumberInterface, Serializable { - private string $number; - private ?int $checkDigit; - - /** - * @param string $number The number. - * @param int|null $checkDigit [Optional] The check digit for the number. - * @throws ArgumentIsNotNumericException If the number input does not consist entirely of numbers. - */ - public function __construct(string $number, int $checkDigit = null) - { - if (!string_is_numeric($number)) { - throw new ArgumentIsNotNumericException($number); - } - - $this->number = $number; - $this->checkDigit = $checkDigit; - } - - /** - * Create a new number from an input that contains the check digit already - * @param string $input The input that contains the check digit already. - * @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers. - * @throws LuhnAlgorithmExceptionInterface - * @return self - * - */ - public static function fromString(string $input): self - { - $input = preg_replace('/[^\d]/', '', $input); - - if (!string_is_numeric($input)) { - throw new ArgumentIsNotNumericException($input); - } - - $lastIndex = strlen($input) - 1; - - // Get the last digit. - $checkDigit = (int)$input[$lastIndex]; - - // Remove the last digit. - $number = substr($input, 0, $lastIndex); - - return new self($number, $checkDigit); - } - - /** - * {@inheritdoc} - */ - public function getNumber(): string - { - return $this->number; - } - - /** - * {@inheritdoc} - */ - public function getCheckDigit(): ?int - { - return $this->checkDigit; - } - - public function __toString(): string - { - return $this->number . $this->checkDigit; - } - - public function serialize(): string - { - return serialize([$this->number, $this->checkDigit]); - } - - public function unserialize($serialized): void - { - [$this->number, $this->checkDigit] = unserialize($serialized); - } + private string $number; + private ?int $checkDigit; + + /** + * @param string $number The number. + * @param int|null $checkDigit [Optional] The check digit for the number. + * @throws ArgumentIsNotNumericException If the number input does not consist entirely of numbers. + */ + public function __construct(string $number, int $checkDigit = null) + { + if (!string_is_numeric($number)) { + throw new ArgumentIsNotNumericException($number); + } + + $this->number = $number; + $this->checkDigit = $checkDigit; + } + + /** + * Create a new number from an input that contains the check digit already + * @param string $input The input that contains the check digit already. + * @return self + * + * @throws LuhnAlgorithmExceptionInterface + * @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers. + */ + public static function fromString(string $input): self + { + $input = preg_replace('/[^\d]/', '', $input); + + if (!string_is_numeric($input)) { + throw new ArgumentIsNotNumericException($input); + } + + $lastIndex = strlen($input) - 1; + + // Get the last digit. + $checkDigit = (int)$input[$lastIndex]; + + // Remove the last digit. + $number = substr($input, 0, $lastIndex); + + return new self($number, $checkDigit); + } + + public function __toString(): string + { + return $this->number . $this->checkDigit; + } + + /** + * {@inheritdoc} + */ + public function getCheckDigit(): ?int + { + return $this->checkDigit; + } + + /** + * {@inheritdoc} + */ + public function getNumber(): string + { + return $this->number; + } + + public function serialize(): string + { + return serialize($this->__serialize()); + } + + public function __serialize(): array + { + return [$this->number, $this->checkDigit]; + } + + public function unserialize($data): void + { + $this->__unserialize(unserialize($data)); + } + + public function __unserialize(array $data): void + { + [$this->number, $this->checkDigit] = $data; + } } From 76264443aedb85e8a3cc07a6d7a8cd0bd60df2f6 Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 11 Mar 2022 10:26:12 +0000 Subject: [PATCH 2/2] [CircleCI] Fix coding standards --- src/Number.php | 170 ++++++++++++++++++++++++------------------------- 1 file changed, 85 insertions(+), 85 deletions(-) diff --git a/src/Number.php b/src/Number.php index 7aa8de0..e95ad31 100644 --- a/src/Number.php +++ b/src/Number.php @@ -38,89 +38,89 @@ */ class Number implements NumberInterface, Serializable { - private string $number; - private ?int $checkDigit; - - /** - * @param string $number The number. - * @param int|null $checkDigit [Optional] The check digit for the number. - * @throws ArgumentIsNotNumericException If the number input does not consist entirely of numbers. - */ - public function __construct(string $number, int $checkDigit = null) - { - if (!string_is_numeric($number)) { - throw new ArgumentIsNotNumericException($number); - } - - $this->number = $number; - $this->checkDigit = $checkDigit; - } - - /** - * Create a new number from an input that contains the check digit already - * @param string $input The input that contains the check digit already. - * @return self - * - * @throws LuhnAlgorithmExceptionInterface - * @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers. - */ - public static function fromString(string $input): self - { - $input = preg_replace('/[^\d]/', '', $input); - - if (!string_is_numeric($input)) { - throw new ArgumentIsNotNumericException($input); - } - - $lastIndex = strlen($input) - 1; - - // Get the last digit. - $checkDigit = (int)$input[$lastIndex]; - - // Remove the last digit. - $number = substr($input, 0, $lastIndex); - - return new self($number, $checkDigit); - } - - public function __toString(): string - { - return $this->number . $this->checkDigit; - } - - /** - * {@inheritdoc} - */ - public function getCheckDigit(): ?int - { - return $this->checkDigit; - } - - /** - * {@inheritdoc} - */ - public function getNumber(): string - { - return $this->number; - } - - public function serialize(): string - { - return serialize($this->__serialize()); - } - - public function __serialize(): array - { - return [$this->number, $this->checkDigit]; - } - - public function unserialize($data): void - { - $this->__unserialize(unserialize($data)); - } - - public function __unserialize(array $data): void - { - [$this->number, $this->checkDigit] = $data; - } + private string $number; + private ?int $checkDigit; + + /** + * @param string $number The number. + * @param int|null $checkDigit [Optional] The check digit for the number. + * @throws ArgumentIsNotNumericException If the number input does not consist entirely of numbers. + */ + public function __construct(string $number, int $checkDigit = null) + { + if (!string_is_numeric($number)) { + throw new ArgumentIsNotNumericException($number); + } + + $this->number = $number; + $this->checkDigit = $checkDigit; + } + + /** + * Create a new number from an input that contains the check digit already + * @param string $input The input that contains the check digit already. + * @return self + * + * @throws LuhnAlgorithmExceptionInterface + * @throws ArgumentIsNotNumericException If the input does not consist entirely of numbers. + */ + public static function fromString(string $input): self + { + $input = preg_replace('/[^\d]/', '', $input); + + if (!string_is_numeric($input)) { + throw new ArgumentIsNotNumericException($input); + } + + $lastIndex = strlen($input) - 1; + + // Get the last digit. + $checkDigit = (int)$input[$lastIndex]; + + // Remove the last digit. + $number = substr($input, 0, $lastIndex); + + return new self($number, $checkDigit); + } + + public function __toString(): string + { + return $this->number . $this->checkDigit; + } + + /** + * {@inheritdoc} + */ + public function getCheckDigit(): ?int + { + return $this->checkDigit; + } + + /** + * {@inheritdoc} + */ + public function getNumber(): string + { + return $this->number; + } + + public function serialize(): string + { + return serialize($this->__serialize()); + } + + public function __serialize(): array + { + return [$this->number, $this->checkDigit]; + } + + public function unserialize($data): void + { + $this->__unserialize(unserialize($data)); + } + + public function __unserialize(array $data): void + { + [$this->number, $this->checkDigit] = $data; + } }