diff --git a/src/Exceptions/HashTableFullException.php b/src/Exceptions/HashTableFullException.php index 39b5157..9d73b44 100644 --- a/src/Exceptions/HashTableFullException.php +++ b/src/Exceptions/HashTableFullException.php @@ -17,7 +17,5 @@ public function __construct( if (empty($message)) { $this->message = self::ERROR_UNMATCHED_CLASS; } - - parent::__construct($message, $code, $previous); } } diff --git a/src/Exceptions/UnmatchedClassException.php b/src/Exceptions/UnmatchedClassException.php index 9d06bed..2b16ee1 100644 --- a/src/Exceptions/UnmatchedClassException.php +++ b/src/Exceptions/UnmatchedClassException.php @@ -10,9 +10,9 @@ class UnmatchedClassException extends \Exception const ERROR_UNMATCHED_CLASS = 'Unmatched class type on deserialization'; public function __construct( - #[LanguageLevelTypeAware(['8.0' => 'string'], default: self::ERROR_UNMATCHED_CLASS)] $message = self::ERROR_UNMATCHED_CLASS, - #[LanguageLevelTypeAware(['8.0' => 'int'], default: '')] $code = 0, - #[LanguageLevelTypeAware(['8.0' => 'Throwable|null'], default: 'Throwable')] $previous = null + $message = self::ERROR_UNMATCHED_CLASS, + $code = 0, + $previous = null ) { parent::__construct($message, $code, $previous); } diff --git a/src/Exceptions/UnmatchedVersionException.php b/src/Exceptions/UnmatchedVersionException.php index e5167f4..92fa333 100644 --- a/src/Exceptions/UnmatchedVersionException.php +++ b/src/Exceptions/UnmatchedVersionException.php @@ -10,9 +10,9 @@ class UnmatchedVersionException extends \Exception const ERROR_WRONG_VERSION = 'Unmatched version on deserialization'; public function __construct( - #[LanguageLevelTypeAware(['8.0' => 'string'], default: self::ERROR_WRONG_VERSION)] $message = self::ERROR_WRONG_VERSION, - #[LanguageLevelTypeAware(['8.0' => 'int'], default: '')] $code = 0, - #[LanguageLevelTypeAware(['8.0' => 'Throwable|null'], default: 'Throwable')] $previous = null + $message = self::ERROR_WRONG_VERSION, + $code = 0, + $previous = null ) { parent::__construct($message, $code, $previous); } diff --git a/src/Traits/CLITrait.php b/src/Traits/CLITrait.php index 1b7b867..6c2123a 100644 --- a/src/Traits/CLITrait.php +++ b/src/Traits/CLITrait.php @@ -4,8 +4,6 @@ /** * Collection of usable CLI functions. - * - * @method bool isCli() */ trait CLITrait { @@ -14,7 +12,7 @@ trait CLITrait * * @return bool */ - public function isCli() + public function isCli() : bool { return php_sapi_name() === 'cli'; } diff --git a/src/Traits/PerformanceTrait.php b/src/Traits/PerformanceTrait.php index 6af0b3f..5eb9183 100644 --- a/src/Traits/PerformanceTrait.php +++ b/src/Traits/PerformanceTrait.php @@ -2,29 +2,35 @@ namespace PHPAlchemist\Traits; +/** + * @deprecated Will be removed in v4.0.0 + */ trait PerformanceTrait { + const int ONE_KILOBYTE_IN_BYTES = 1024; + const int ONE_MEGABYTE_IN_BYTES = 1048576; + /** * get the performance data (Peak Memory Usage) for a given script or * class. * * @return string */ - public function getPerformance() + public function getPerformance() : string { - $mem_usage = memory_get_peak_usage(); - if ($mem_usage < 1024) { + $memUsage = memory_get_peak_usage(); + if ($memUsage < self::ONE_KILOBYTE_IN_BYTES) { // These two are ignored as the nature of phpUnit testing will not allow me to come in under a meg // and run coverage // @codeCoverageIgnoreStart - $whoa = $mem_usage.' bytes'; - } elseif ($mem_usage < 1048576) { - $whoa = round($mem_usage / 1024, 2).' kilobytes'; + $displayData = $memUsage.' bytes'; + } elseif ($memUsage < self::ONE_MEGABYTE_IN_BYTES) { + $displayData = round($memUsage / self::ONE_KILOBYTE_IN_BYTES, 2).' kilobytes'; // @codeCoverageIgnoreEnd } else { - $whoa = round($mem_usage / 1048576, 2).' megabytes'; + $displayData = round($memUsage / self::ONE_MEGABYTE_IN_BYTES, 2).' megabytes'; } - return $whoa; + return $displayData; } } diff --git a/src/Utilities/CSVUtil.php b/src/Utilities/CSVUtil.php index c8e5d8a..f2fd27c 100644 --- a/src/Utilities/CSVUtil.php +++ b/src/Utilities/CSVUtil.php @@ -8,14 +8,15 @@ class CSVUtil * Nice String output replacement form fputcsv * code taken from: http://www.php.net/manual/en/function.fputcsv.php#96937. * - * @param $row + * + * @param array $row * @param string $delimiter * @param string $enclosure * @param string $eol * * @return bool|string */ - public static function sputcsv($row, $delimiter = ',', $enclosure = '"', $eol = PHP_EOL) + public static function sputcsv(array $row, string $delimiter = ',', string $enclosure = '"', string $eol = PHP_EOL) : string { static $fp = false; if ($fp === false) { @@ -26,7 +27,7 @@ public static function sputcsv($row, $delimiter = ',', $enclosure = '"', $eol = } if (fputcsv($fp, $row, $delimiter, $enclosure, '\\', PHP_EOL) === false) { - return false; + return ''; } rewind($fp); diff --git a/src/ValueObject/Abstract/AbstractNumber.php b/src/ValueObject/Abstract/AbstractNumber.php new file mode 100644 index 0000000..94182eb --- /dev/null +++ b/src/ValueObject/Abstract/AbstractNumber.php @@ -0,0 +1,32 @@ +value = $value; + } + + public function getValue() : int|float + { + return $this->value; + } + + public function equals(VONumberInterface $number) : bool + { + if ( + $number === $this + && $number->getValue() === $this->getValue() + ) { + return true; + } + + return false; + } +} diff --git a/src/ValueObject/Abstract/AbstractString.php b/src/ValueObject/Abstract/AbstractString.php new file mode 100644 index 0000000..9121c56 --- /dev/null +++ b/src/ValueObject/Abstract/AbstractString.php @@ -0,0 +1,95 @@ +getValue()) ?: ''; + } + + public function getValue() : ?string + { + return $this->value; + } + + public function contains(mixed $needle, bool $caseInsensitive = false) : bool + { + if ($caseInsensitive) { + return str_contains(mb_strtolower($this->value), mb_strtolower($needle)); + } + + return str_contains($this->value, $needle); + } + + /** @inheritDoc */ + public function endsWith(mixed $needle) : bool + { + return str_ends_with($this->value, $needle); + } + + /** @inheritDoc */ + public function equals(VOStringInterface $comparitive) : bool + { + return $comparitive->getValue() === $this->getValue(); + } + + /** + * @inheritDoc + */ + public function hasValue() : bool + { + return !is_null($this->value); + } + + /** @inheritDoc */ + public function indexOf(string $needle, int $startIndex = 0) : int|false + { + return strpos($this->value, $needle, $startIndex); + } + + public function lastIndexOf(string $needle, int $startIndex = 0) : int|false + { + return strrpos($this->value, $needle, $startIndex); + } + + /** + * @inheritDoc + */ + public function length() : VONumberInterface + { + $length = strlen($this->value); + + return new Number($length); + } + + /** + * Convert string to lower case. + * + * @return string + */ + public function lower() : string + { + return mb_strtolower($this->getValue()); + } + + /** @inheritDoc */ + public function startsWith(mixed $needle) : bool + { + return str_starts_with($this->value, $needle); + } + + /** @inheritDoc */ + public function substring(int $offset, ?int $length = null) : VOStringInterface + { + return new Twine(substr($this->value, $offset, $length)); + } +} diff --git a/src/ValueObject/Contract/VONumberInterface.php b/src/ValueObject/Contract/VONumberInterface.php new file mode 100644 index 0000000..565b9ff --- /dev/null +++ b/src/ValueObject/Contract/VONumberInterface.php @@ -0,0 +1,10 @@ +value = $value; + } + + public function getUser() : Twine + { + $parts = explode('@', $this->value, 2); + + return new Twine(array_shift($parts)); + } + + public function getDomain() : Twine + { + $parts = explode('@', $this->value, 2); + + return new Twine(array_pop($parts)); + } + + public function getTLD() : Twine + { + $parts = explode('.', $this->value); + + return new Twine(array_pop($parts)); + } +} diff --git a/src/ValueObject/Model/Id.php b/src/ValueObject/Model/Id.php new file mode 100644 index 0000000..7f23162 --- /dev/null +++ b/src/ValueObject/Model/Id.php @@ -0,0 +1,9 @@ + 'Alabama', + 'AK' => 'Alaska', + 'AS' => 'American Samoa', // Territory + 'AZ' => 'Arizona', + 'AR' => 'Arkansas', + 'CA' => 'California', + 'CO' => 'Colorado', + 'CT' => 'Connecticut', + 'DE' => 'Delaware', + 'FL' => 'Florida', + 'GA' => 'Georgia', + 'GU' => 'Guam', // Territory + 'HI' => 'Hawaii', + 'ID' => 'Idaho', + 'IL' => 'Illinois', + 'IN' => 'Indiana', + 'IA' => 'Iowa', + 'KS' => 'Kansas', + 'KY' => 'Kentucky', + 'LA' => 'Louisiana', + 'ME' => 'Maine', + 'MD' => 'Maryland', + 'MA' => 'Massachusetts', + 'MI' => 'Michigan', + 'MN' => 'Minnesota', + 'MS' => 'Mississippi', + 'MO' => 'Missouri', + 'MT' => 'Montana', + 'NE' => 'Nebraska', + 'NV' => 'Nevada', + 'NH' => 'New Hampshire', + 'NJ' => 'New Jersey', + 'NM' => 'New Mexico', + 'NY' => 'New York', + 'MP' => 'Northern Mariana Islands', // Territory + 'NC' => 'North Carolina', + 'ND' => 'North Dakota', + 'OH' => 'Ohio', + 'OK' => 'Oklahoma', + 'OR' => 'Oregon', + 'PA' => 'Pennsylvania', + 'PR' => 'Puerto Rico', // Territory + 'RI' => 'Rhode Island', + 'SC' => 'South Carolina', + 'SD' => 'South Dakota', + 'TN' => 'Tennessee', + 'TX' => 'Texas', + 'UT' => 'Utah', + 'VT' => 'Vermont', + 'VA' => 'Virginia', + 'WA' => 'Washington', + 'WV' => 'West Virginia', + 'WI' => 'Wisconsin', + 'WY' => 'Wyoming', + 'VI' => 'US Virgin Islands', // Territory + ]; + + public function __construct(string $value) + { + if (!in_array($value, $this->validOptions, true) && !array_key_exists($value, $this->validOptions)) { + throw new InvalidArgumentException('Invalid US State value.'); + } + + if (strlen($value) !== self::CODE_LENGTH) { + $code = array_search($value, $this->validOptions, true); + } elseif (strlen($value) === self::CODE_LENGTH) { + $code = $value; + $value = $this->validOptions[$value]; + } + + $this->code = $code; + $this->value = $value; + } + + public function getName() : string + { + return $this->value; + } + + public function getCode() : string + { + return $this->code; + } +} diff --git a/tests/Unit/AccessorTraitTest.php b/tests/Unit/AccessorTraitTest.php index a6ee6a7..c548c40 100644 --- a/tests/Unit/AccessorTraitTest.php +++ b/tests/Unit/AccessorTraitTest.php @@ -157,7 +157,6 @@ public function testMethodNotFoundExceptions() } } - #[CoversMethod([AccessorTrait::class, 'getMethodVerb'])] public function testIsAndCatchAll() { $example = new MagicAccessorExample(); diff --git a/tests/ValueObject/Unit/EmailTest.php b/tests/ValueObject/Unit/EmailTest.php new file mode 100644 index 0000000..763cde8 --- /dev/null +++ b/tests/ValueObject/Unit/EmailTest.php @@ -0,0 +1,70 @@ +expectExceptionMessage('Invalid email address.'); + $invalidEmailValue = 'stuff@things'; + new Email($invalidEmailValue); + } + + public function testValidEmail() : void + { + $emailObject = new Email(self::VALID_EMAIL_VALUE); + + $this->assertEquals(self::VALID_EMAIL_VALUE, $emailObject->getValue()); + } + + public function testEquals() : void + { + $emailObject = new Email(self::VALID_EMAIL_VALUE); + $comparitiveEmail = new Email(self::VALID_EMAIL_VALUE); + + $this->assertTrue($emailObject->equals($comparitiveEmail)); + } + + public function testLength() : void + { + $expectedLength = 16; + $emailObject = new Email(self::VALID_EMAIL_VALUE); + + $this->assertInstanceOf(Number::class, $emailObject->length()); + $this->assertequals($expectedLength, $emailObject->length()->getValue()); + } + + public function testGetUser() : void + { + $emailObject = new Email(self::VALID_EMAIL_VALUE); + $this->assertEquals('stuff', $emailObject->getUser()); + } + + public function testGetDomain() : void + { + $emailObject = new Email(self::VALID_EMAIL_VALUE); + $this->assertEquals('things.net', $emailObject->getDomain()); + } + + public function testGetTLD() : void + { + $emailObject = new Email(self::VALID_EMAIL_VALUE); + $tld = $emailObject->getTld(); + $this->assertEquals('net', $tld); + $this->assertInstanceOf(Twine::class, $tld); + } +} diff --git a/tests/ValueObject/Unit/USStateTest.php b/tests/ValueObject/Unit/USStateTest.php new file mode 100644 index 0000000..027b29b --- /dev/null +++ b/tests/ValueObject/Unit/USStateTest.php @@ -0,0 +1,52 @@ +expectExceptionMessage('Invalid US State value.'); + $invalidStatelValue = 'ZA'; + new USState($invalidStatelValue); + } + + public function testValidStateCode() : void + { + $validStateValue = 'Nebraska'; + $validStateCode = 'NE'; + $emailObject = new USState($validStateCode); + + $this->assertEquals($validStateValue, $emailObject->getValue()); + $this->assertEquals($validStateCode, $emailObject->getCode()); + } + + public function testEquals() : void + { + $validStateValue = 'Utah'; + $emailObject = new USState($validStateValue); + $comparitiveState = new USState($validStateValue); + + $this->assertTrue($emailObject->equals($comparitiveState)); + } + + public function testLength() : void + { + $validStateCode = 'LA'; + $expectedLength = 9; + $state = new USState($validStateCode); + + $this->assertInstanceOf(Number::class, $state->length()); + $this->assertequals($expectedLength, $state->length()->getValue()); + } +}