diff --git a/composer.json b/composer.json index a0d3534..7d7f193 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,9 @@ "laravel/framework": "^9" }, "require-dev": { - "phpunit/phpunit": "^9.5", - "orchestra/testbench": "^7" + "phpunit/phpunit": "^9", + "orchestra/testbench": "^7", + "larastan/larastan": "^2.0" }, "autoload": { "psr-4": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..b5fa3e6 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,8 @@ +includes: + - vendor/larastan/larastan/extension.neon + +parameters: + paths: + - src + level: 6 + checkGenericClassInNonGenericObjectType: false diff --git a/src/AntondateServiceProvider.php b/src/AntondateServiceProvider.php index ca708c9..8d52ef3 100644 --- a/src/AntondateServiceProvider.php +++ b/src/AntondateServiceProvider.php @@ -3,6 +3,7 @@ namespace Ottosmops\Antondate; use Illuminate\Support\ServiceProvider; +use Ottosmops\Antondate\ValueObjects\AntonDate; class AntondateServiceProvider extends ServiceProvider { @@ -34,17 +35,17 @@ public function register(): void //$this->mergeConfigFrom(__DIR__.'/../config/antondate.php', 'antondate'); // Register the service the package provides. - $this->app->singleton('antondate', function ($app) { - return new Antondate; - }); + //$this->app->singleton('antondate', function ($app) { + // return new AntonDate; + //}); } /** * Get the services provided by the provider. * - * @return array + * @return array */ - public function provides() + public function provides() : array { return ['antondate']; } diff --git a/src/Casts/AntonDateEndCast.php b/src/Casts/AntonDateEndCast.php index 1739cd3..982320d 100644 --- a/src/Casts/AntonDateEndCast.php +++ b/src/Casts/AntonDateEndCast.php @@ -14,10 +14,10 @@ class AntonDateEndCast implements CastsAttributes * @param \Illuminate\Database\Eloquent\Model $model * @param string $key * @param mixed $value - * @param array $attributes - * @return Anton\ValueObjects\AntonDate; + * @param array $attributes + * @return AntonDate; */ - public function get($model, $key, $value, $attributes) + public function get($model, $key, $value, $attributes) : AntonDate { return AntonDate::createFromString( $attributes['date_end'] ?? '0000', @@ -30,17 +30,17 @@ public function get($model, $key, $value, $attributes) * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key - * @param AntonDate $value - * @param array $attributes - * @return array + * @param ?AntonDate $value + * @param array $attributes + * @return array */ - public function set($model, $key, $value, $attributes) + public function set($model, $key, $value, $attributes) : array { /* * We'll need this to handle nullable columns */ if (is_null($value)) { - return; + return []; } if (!$value instanceof AntonDate) { diff --git a/src/Casts/AntonDateIntervalCast.php b/src/Casts/AntonDateIntervalCast.php index b6b5b34..bbcd19e 100644 --- a/src/Casts/AntonDateIntervalCast.php +++ b/src/Casts/AntonDateIntervalCast.php @@ -12,17 +12,17 @@ class AntonDateIntervalCast implements CastsAttributes /** * Cast the given value. * - * @param \Illuminate\Database\Eloquent\Model $model + * @param mixed $model * @param string $key * @param mixed $value - * @param array $attributes + * @param array $attributes * @return AntonDateInterval; */ - public function get($model, $key, $value, $attributes) + public function get($model, $key, $value, $attributes) : AntonDateInterval { $anton_date_interval = new AntonDateInterval( - AntonDate::createFromString($attributes['date_start'], $attributes['date_start_ca']), - AntonDate::createFromString($attributes['date_end'],$attributes['date_end_ca']) + AntonDate::createFromString((string) $attributes['date_start'], (bool) $attributes['date_start_ca']), + AntonDate::createFromString((string) $attributes['date_end'], (bool) $attributes['date_end_ca']) ); return $anton_date_interval; @@ -31,13 +31,13 @@ public function get($model, $key, $value, $attributes) /** * Prepare the given value for storage. * - * @param \Illuminate\Database\Eloquent\Model $model + * @param mixed $model * @param string $key - * @param Anton\AntonDateInterval $value - * @param array $attributes - * @return array + * @param AntonDateInterval $value + * @param array $attributes + * @return array */ - public function set($model, $key, $value, $attributes) + public function set($model, $key, $value, $attributes) : array { if (!$value instanceof AntonDateInterval) { throw new InvalidArgumentException( diff --git a/src/Casts/AntonDateStartCast.php b/src/Casts/AntonDateStartCast.php index b0f37dd..45a5a20 100644 --- a/src/Casts/AntonDateStartCast.php +++ b/src/Casts/AntonDateStartCast.php @@ -3,6 +3,7 @@ namespace Ottosmops\Antondate\Casts; use InvalidArgumentException; +use Illuminate\Database\Eloquent\Model; use Ottosmops\Antondate\ValueObjects\AntonDate; use Illuminate\Contracts\Database\Eloquent\CastsAttributes; @@ -11,13 +12,13 @@ class AntonDateStartCast implements CastsAttributes /** * Cast the given value. * - * @param \Illuminate\Database\Eloquent\Model $model + * @param mixed $model * @param string $key * @param mixed $value - * @param array $attributes + * @param array $attributes * @return AntonDate; */ - public function get($model, $key, $value, $attributes) + public function get(mixed $model, string $key, mixed $value, array $attributes) : AntonDate { return AntonDate::createFromString( $attributes['date_start'] ?? '0000', @@ -30,17 +31,17 @@ public function get($model, $key, $value, $attributes) * * @param \Illuminate\Database\Eloquent\Model $model * @param string $key - * @param AntonDate $value - * @param array $attributes - * @return array + * @param ?AntonDate $value + * @param array $attributes + * @return array */ - public function set($model, $key, $value, $attributes) + public function set($model, $key, $value, $attributes) : array { /* * We'll need this to handle nullable columns */ if (is_null($value)) { - return; + return []; } if (!$value instanceof AntonDate) { diff --git a/src/DateHelper.php b/src/DateHelper.php index 79ce4db..ec283f3 100644 --- a/src/DateHelper.php +++ b/src/DateHelper.php @@ -82,15 +82,15 @@ public static function renderDate( return $html; } - public static function formatDate($str, $nullable = false) + public static function formatDate(string $string, bool $nullable = false): ?string { - if ('0000-00-00 00:00:00' != $str - && '-0001-11-30 00:00:00' != $str - && null != $str - && '0000-00-00' != $str - && '-0001' != $str) { - //$html = $str .' :: '; - $html = AntonDate::compose($str)->formatted(); + if ('0000-00-00 00:00:00' != $string + && '-0001-11-30 00:00:00' != $string + && null != $string + && '0000-00-00' != $string + && '-0001' != $string) { + //$html = $string .' :: '; + $html = AntonDate::compose($string)->formatted(); } else { if (!$nullable) { $html = trans('messages.no_date'); @@ -120,35 +120,35 @@ public static function antonDate2carbonDate($date) /** * [year description] * @param string $date 2003-01-03 - * @return string 2003 + * @return ?string 2003 */ - public static function year($date) + public static function year(string $date) : ?string { if (self::checkIsoDate($date)) { return date("Y", strtotime($date)); } - return false; + return null; } - public static function cleanDate($datestring) + public static function cleanDate(string $datestring): string { $datestring = preg_replace('/(-00$)/', '', $datestring); $datestring = preg_replace('/(-00$)/', '', $datestring); return $datestring; } - public static function composeDate($year = 0, $month = 0, $day = 0) + public static function composeDate(int $year = 0, int $month = 0, int $day = 0): string { $date = ''; $year = ($year >= 1 && $year <= 2100) ? $year : 0; - $date .= str_pad($year, 4, "0", STR_PAD_LEFT); + $date .= str_pad((string) $year, 4, "0", STR_PAD_LEFT); $month = ($month >= 1 && $month <= 12) ? $month : 0; - $date .= '-' . str_pad($month, 2, "0", STR_PAD_LEFT); + $date .= '-' . str_pad((string) $month, 2, "0", STR_PAD_LEFT); $day = ($day >= 1 && $day <= 31) ? $day : 0; - $date .= '-' . str_pad($day, 2, "0", STR_PAD_LEFT); + $date .= '-' . str_pad((string) $day, 2, "0", STR_PAD_LEFT); return $date; } @@ -165,16 +165,15 @@ public static function composeDate($year = 0, $month = 0, $day = 0) * @author ak * @version 1.0 | 2014-01-15 * - * @param string $isodate zu ueberpruefendes ISO Datum + * @param string $isoDate zu ueberpruefendes ISO Datum * @return bool * */ - public static function checkIsoDate($isoDate) + public static function checkIsoDate(string $isoDate) : bool { $return = false; - if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $isoDate, $parts)) { - if (checkdate($parts[2], $parts[3], $parts[1])) { + if (checkdate((int) $parts[2], (int) $parts[3], (int) $parts[1])) { $return = true; } } diff --git a/src/ValueObjects/AntonDate.php b/src/ValueObjects/AntonDate.php index e2c61dd..87d79fe 100644 --- a/src/ValueObjects/AntonDate.php +++ b/src/ValueObjects/AntonDate.php @@ -17,13 +17,13 @@ final class AntonDate implements ValueObjectInterface /** @const Regex for a AntonDateString */ const ANTON_DATE_PATTERN = '(?:ca. )?(?:(-?\d{3,4})(?:-(\d{2}))?(?:-(\d{2}))?)'; - private $year = 0; + private int $year = 0; - private $month = 0; + private int $month = 0; - private $day = 0; + private int $day = 0; - private $ca = 0; + private bool|int|null $ca = 0; // ******************** @@ -33,12 +33,13 @@ final class AntonDate implements ValueObjectInterface /** * Returns a new AntonDate from a date-string * - * @param string|null $sDate in AntonDateFormat 'Y-m-d', 'Y-m', 'Y' or '0000' + * @param ?string $sDate in AntonDateFormat 'Y-m-d', 'Y-m', 'Y' or '0000' * with or wthout a 'ca. ' in front of the date - * @param boolean $ca if $sDate starts with 'ca. ' or $ca is true the AntonDate contains $ca == 1 - * @return object AntonDate + * @param bool|int $ca if $sDate starts with 'ca. ' or $ca is true the AntonDate contains $ca == 1 + * + * @return static */ - public static function createFromString($sDate, $ca = 0) : AntonDate + public static function createFromString(?string $sDate, bool|int $ca = 0) : static { $sDate = $sDate ?: '0000-00-00'; self::checkBool($ca); @@ -66,11 +67,12 @@ public static function createFromString($sDate, $ca = 0) : AntonDate /** * Returns a new AntonDate from a date-string which is freely formatted + * This need a major refactoring (invalid datestrings are not really handled and not tested) * * @param string $value - * @return object AntonDate + * @return static */ - public static function guessFromString(string $value) : AntonDate + public static function guessFromString(string $value) : static { $ca = 0; $value = trim($value); @@ -96,6 +98,11 @@ public static function guessFromString(string $value) : AntonDate $month = $k; } } + + if (!isset($month)) { + throw new \InvalidArgumentException('Could not determine month from string: ' . $value); + } + $year = $matches[3]; $value = date('Y-m-d', strtotime("$day-$month-$year")); } @@ -106,12 +113,13 @@ public static function guessFromString(string $value) : AntonDate /** * Returns a new AntonDate from a year, month, day, and ca * - * @param str $year 4 digits - * @param str $month 2 digits (1-12) - * @param str $day 2 digits (1-31) - * @return obj returns an AntonDate-Object + * @param ?string $year 4 digits + * @param ?string $month 2 digits (1-12) + * @param ?string $day 2 digits (1-31) + * @param boolean|int $ca + * @return static returns an AntonDate-Object */ - public static function compose($year = '0000', $month = '00', $day = '00', $ca = 0) : AntonDate + public static function compose(?string $year = '0000', ?string $month = '00', ?string $day = '00', $ca = false) : static { self::checkBool($ca); $aDate['year'] = (int) $year; //str_pad($year, 4, "0", STR_PAD_LEFT); @@ -124,9 +132,9 @@ public static function compose($year = '0000', $month = '00', $day = '00', $ca = /** * Returns a new AntonDate for today. - * @return obj returns an AntonDate-Object + * @return static returns an AntonDate-Object */ - public static function today() : AntonDate + public static function today() : static { $today = date('Y-m-d'); return self::createFromString($today); @@ -149,10 +157,10 @@ public static function today() : AntonDate * Valid: 000-00-01, 0000-02-01 * Invalid: 0000-00, 0000-01, 1934-00-01 * - * @param string $date - * @return boolean + * @param string $sDate + * @return bool */ - public static function isValidString(string $sDate) + public static function isValidString(string $sDate) : bool { $date = trim($sDate); @@ -170,7 +178,7 @@ public static function isValidString(string $sDate) return false; } - $year = $match[1]; + $year = (int) $match[1]; $month = (int) ($match[2] ?? 0); $day = (int) ($match[3] ?? 0); @@ -201,7 +209,9 @@ public function toString() : string /** * Returns an associative array. - * @return array ['year'=> , 'month'=> , 'day'=> , 'ca'=>] + * + * @param bool $with_ca + * @return array ['year'=> , 'month'=> , 'day'=> , 'ca'=>] */ public function toArray(bool $with_ca = true) : array { @@ -221,8 +231,13 @@ public function toArray(bool $with_ca = true) : array /** * Returns a formatted string. * https://momentjs.com/ + * default: '%b. ' (short month), '%B ' (long month) + * + * @param string $locale + * @param string $format_y + * @param string $format_m + * @param string $format_d * - * @param string $format_month default: '%b. ' (short month), '%B ' (long month) * @return string "4. Mar 1971" */ public function formatted( @@ -241,7 +256,7 @@ public function formatted( list($y, $m, $d) = $arr; - $format = self::getDateFormat($this->toMysqlDate()); + $format = self::getDateFormat(); switch ($format) { case 'Y': @@ -262,15 +277,14 @@ public function formatted( /** * Returns a date for storage in a mysql-db * - * @param string $date 1947 or 1947-02-11 * @return string date in iso-format or 0000-00-00 */ public function toMysqlDate() : string { $str = self::cleanDate( - str_pad($this->getYear(), 4, "0", STR_PAD_LEFT) . '-' . - str_pad($this->getMonth(), 2, "0", STR_PAD_LEFT) . '-' . - str_pad($this->getDay(), 2, "0", STR_PAD_LEFT) + str_pad((string) $this->getYear(), 4, "0", STR_PAD_LEFT) . '-' . + str_pad((string) $this->getMonth(), 2, "0", STR_PAD_LEFT) . '-' . + str_pad((string) $this->getDay(), 2, "0", STR_PAD_LEFT) ); return (string) self::addZeros($str); } @@ -285,9 +299,9 @@ public function __toString() : string $str = ''; $str .= self::translateCa($this->ca); $str .= self::cleanDate( - str_pad($this->getYear(), 4, "0", STR_PAD_LEFT) . '-' . - str_pad($this->getMonth(), 2, "0", STR_PAD_LEFT) . '-' . - str_pad($this->getDay(), 2, "0", STR_PAD_LEFT) + str_pad((string) $this->getYear(), 4, "0", STR_PAD_LEFT) . '-' . + str_pad((string) $this->getMonth(), 2, "0", STR_PAD_LEFT) . '-' . + str_pad((string) $this->getDay(), 2, "0", STR_PAD_LEFT) ); return (string) $str; @@ -334,8 +348,8 @@ public function getDay() : int /** * @param AntonDate $date - * @param boolean $strict: $this->ca is evaluated - * @return boolean + * @param bool $strict: $this->ca is evaluated + * @return bool */ public function isEqualTo(AntonDate $date, bool $strict = false) : bool { @@ -347,7 +361,7 @@ public function isEqualTo(AntonDate $date, bool $strict = false) : bool /** * @param AntonDate $date - * @return boolean + * @return bool */ public function isGreaterThan(AntonDate $date) : bool { @@ -359,7 +373,7 @@ public function isGreaterThan(AntonDate $date) : bool /** * @param AntonDate $date - * @return boolean + * @return bool */ public function isLessThan(AntonDate $date) : bool { @@ -370,17 +384,13 @@ public function isLessThan(AntonDate $date) : bool return $this->toInteger() < $date->toInteger(); } - // ****************** - // private functions - // ****************** - /** * Create a new Date * [year => JJJJ * month => mm * day => dd * ca => 1] - * @param object $date new validated AntonDate + * @param array $date */ public function __construct(array $date) { @@ -393,11 +403,11 @@ public function __construct(array $date) /** * Validate AntonDate - * @param string $value expected to be an AntonDate + * * @throws \InvalidArgumentException * @return void */ - private function validate() + private function validate() : void { if (!AntonDate::isValidString($this->toString())) { throw new \InvalidArgumentException(sprintf('AntonDate is not valid: (%s, %s, %s, %s).', @@ -411,7 +421,7 @@ private function validate() * * @return string 'Y-m-d', 'Y-m' or'Y' */ - private function getDateFormat() + private function getDateFormat() : string { if ($this->getYear() > 0 && $this->getMonth() == 0 && $this->getDay() == 0) { return 'Y'; @@ -433,7 +443,7 @@ private function getDateFormat() * @param string $datestring * @return string */ - private static function cleanDate($datestring) + private static function cleanDate($datestring) : string { $datestring = trim($datestring); $datestring = preg_replace('/(-00$)/', '', $datestring); @@ -446,8 +456,9 @@ private static function cleanDate($datestring) * AntonDate to MysqlDate (1973 --> 1973-00-00) * * @param string $datestring [description] + * @return string */ - private static function addZeros($datestring) + private static function addZeros($datestring) : string { $with_zeros = $datestring; @@ -463,47 +474,58 @@ private static function addZeros($datestring) } /** - * @return string "19710304" + * @return int 19710304 */ - private function toInteger() + private function toInteger() : int { return (int) str_replace('-', '', $this->toMysqlDate()); } - private static function translateCa($ca) + private static function translateCa(mixed $ca) : string { return ($ca == 1) ? trans('antondate::antondate.ca').' ' : ''; } - private static function checkBool($ca) + /** + * @param mixed $ca + * @throws \InvalidArgumentException + */ + private static function checkBool(mixed $ca) : void { - if (in_array($ca, ['1', '0', 1, 0, true, false])) { - return true; + if (!in_array($ca, ['1', '0', 1, 0, true, false])) { + throw new \InvalidArgumentException('AntonDate is not valid: ' . $ca . ' is not boolean.'); } - throw new \InvalidArgumentException('AntonDate is not valid: ' . $ca .' is not boolean.'); } - public function formatDate($nullable = false) + /** + * Returns a rendered date + */ + public function formatDate(bool $nullable = false) : string { if ($this->toString() !== '0000') { - $html = $this->formatted(); + $string = $this->formatted(); } else { if (!$nullable) { - $html = trans('antondate::antondate.no_date'); + $string = trans('antondate::antondate.no_date'); } else { return ''; } } - return $html; + return $string; } - private static function getMonths() + /** + * Returns an array with the months in german + * + * @return array + */ + private static function getMonths() : array { - $months = []; for ( $i = 1; $i <= 12; $i++ ) { - $months[$i] = date_format(date_create('2000-'.str_pad($i, 2, 0, STR_PAD_LEFT).'-01'), 'F'); + $months[$i] = date_format(date_create('2000-'.str_pad((string) $i, 2, '0', STR_PAD_LEFT).'-01'), 'F'); } + return $months; } } diff --git a/src/ValueObjects/AntonDateInterval.php b/src/ValueObjects/AntonDateInterval.php index ad73bf2..29ac374 100644 --- a/src/ValueObjects/AntonDateInterval.php +++ b/src/ValueObjects/AntonDateInterval.php @@ -11,19 +11,18 @@ final class AntonDateInterval implements ValueObjectInterface { //const DATE_PATTERN = '((?:ca. )?(?:\d{3,4}(?:-\d{2})?(?:-\d{2})?))'; const DATE_INTERVAL_PATTERN = AntonDate::ANTON_DATE_PATTERN .'[/-]'.AntonDate::ANTON_DATE_PATTERN; - /* @var Antondate */ - private $date_start; - /* @var Antondate */ - private $date_end; + public AntonDate $AntonDateStart; + + public AntonDate $AntonDateEnd; /** * [createFromString description] * @param string $string eg. 'ca. 1984/1986-12-03' * @param string $separator '/' by default - * @return obj AntonDateInterval + * @return static AntonDateInterval */ - public static function createFromString($string = '', $separator = '/') + public static function createFromString($string = '', $separator = '/') : static { $dateIntervalString = trim($string); // work with $datestring @@ -44,7 +43,7 @@ public static function createFromString($string = '', $separator = '/') throw new \InvalidArgumentException("Could not parse string to AntonDate (could not initiate a AntonStartDate) $string"); } - if (isset($date_end)) { + if ($date_end) { if (AntonDate::isValidString($date_end)) { $AntonDateEnd = AntonDate::createFromString($date_end); } else { @@ -78,9 +77,9 @@ public static function compose(string $date_start, bool $date_start_ca, string $ * isValid checks if a given dateInterval is valid * @param string $value dateInterval * @param string $separator '/' by default - * @return boolean + * @return bool */ - public static function isValidString($value, $separator = '/') + public static function isValidString($value, $separator = '/') : bool { if (!preg_match('#' . self::DATE_INTERVAL_PATTERN . '#', $value)) { return false; @@ -90,6 +89,9 @@ public static function isValidString($value, $separator = '/') } else { list($date_start, $date_end) = explode('/', $value); } + if (!$date_end) { + return false; + } if (AntonDate::isValidString($date_start)) { $antonDateStart = AntonDate::createFromString($date_start); @@ -97,19 +99,15 @@ public static function isValidString($value, $separator = '/') return false; } - if (isset($date_end)) { - if (AntonDate::isValidString($date_end)) { - $antonDateEnd = AntonDate::createFromString($date_end); - } else { - return false; - } + if (AntonDate::isValidString($date_end)) { + $antonDateEnd = AntonDate::createFromString($date_end); } else { - $date_end = '0000-00-00'; + return false; } $isValid = $antonDateStart->isLessThan($antonDateEnd) || $antonDateStart->isEqualTo($antonDateEnd) - || (AntonDate::isValidString($date_start) && $date_end == '0000-00-00'); + || $date_end == '0000-00-00'; return $isValid; } @@ -129,44 +127,46 @@ public function __toString() return $str; } - public function mysqlDateArray($option = '') - { - if ($option == 'nullable') { - return array_merge($this->dateInterval, ['date_end'=> null, 'date_end_ca'=> 0]); - } - return $this->dateInterval; - } - - public function dateStartCa() + public function dateStartCa() : int { return $this->AntonDateStart->getCa(); } - public function dateEndCa() + public function dateEndCa() : int { return $this->AntonDateEnd->getCa(); } - public function mysqlDateStart() + public function mysqlDateStart() : string { return $this->AntonDateStart->toMysqlDate(); } - public function mysqlDateEnd($option = '') + public function mysqlDateEnd(string $option = '') : string|null { - if (($option == 'nullable') && ($this->dateInterval['date_end'] == '0000-00-00')) { + if (($option == 'nullable') && ($this->AntonDateEnd->toMysqlDate() == '0000-00-00')) { return null; } return $this->AntonDateEnd->toMysqlDate(); } + /** + * AntonDateInterval constructor. + * + * @param AntonDate $AntonDateStart + * @param AntonDate $AntonDateEnd + */ public function __construct(AntonDate $AntonDateStart, AntonDate $AntonDateEnd) { $this->AntonDateStart = $AntonDateStart; $this->AntonDateEnd = $AntonDateEnd; } - public function toArray() + /** + * Returns the dateInterval as an array + * @return array + */ + public function toArray() : array { return [ 'date_start' => $this->AntonDateStart->toMysqlDate(), @@ -183,7 +183,7 @@ public function toArray() * @param boolean $nullable returns an empty string if the date is null/0 etc. * @return string */ - public function renderDate(bool $only_year = false, bool $nullable = false) + public function renderDate(bool $only_year = false, bool $nullable = false) : string { $date_start_ca = 0 == $this->AntonDateStart->getCa() ? '' : trans('antondate::antondate.ca') . ' '; $date_end_ca = 0 == $this->AntonDateEnd->getCa() ? '' : trans('antondate::antondate.ca') . ' '; diff --git a/src/ValueObjects/ValueObjectInterface.php b/src/ValueObjects/ValueObjectInterface.php index e442184..6b1f09c 100644 --- a/src/ValueObjects/ValueObjectInterface.php +++ b/src/ValueObjects/ValueObjectInterface.php @@ -7,13 +7,13 @@ interface ValueObjectInterface /** * Create a object from the PHP native value. * - * @return obj + * @return static */ public static function createFromString(string $value); /** * Validates the string representation of the value. - * @param mixed $value + * @param string $value * @return boolean */ public static function isValidString(string $value); @@ -21,7 +21,7 @@ public static function isValidString(string $value); /** * Returns the value of the object * - * @return mixed + * @return string */ public function toString(); diff --git a/tests/AntonDateTest.php b/tests/AntonDateTest.php index f0f5e44..678ecd8 100644 --- a/tests/AntonDateTest.php +++ b/tests/AntonDateTest.php @@ -7,26 +7,26 @@ class AntonDateTest extends TestCase { - public $validDates = ['0000', '973', '0000-00-00', '1973', '1973-00-00', + public array $validDates = ['0000', '973', '0000-00-00', '1973', '1973-00-00', '1973-01', '1902-12', '1973-01-00', '1973-01-05', 'ca. 1973', '0000-00-03', '973', '-200']; - public $invalidDates = ['1973-13', '73-04-01', '1973.00', '02','1977-00-01']; + public array $invalidDates = ['1973-13', '73-04-01', '1973.00', '02','1977-00-01']; - public function testAntonDatesAreValid() + public function testAntonDatesAreValid() : void { foreach ($this->validDates as $date) { $this->assertTrue(AntonDate::isValidString($date)); } } - public function testNonAntonDatesAreInvalid() + public function testNonAntonDatesAreInvalid() : void { foreach ($this->invalidDates as $date) { $this->assertFalse(AntonDate::isValidString($date)); } } - public function testComposeAntonDateToArray() + public function testComposeAntonDateToArray() : void { $actual = AntonDate::compose('1973', '12', '01', 1)->toArray(); $expected = ['year' => '1973', 'month' => '12', 'day' => '1', 'ca' => 1]; @@ -34,13 +34,13 @@ public function testComposeAntonDateToArray() $this->assertEquals($expected, $actual); } - public function testTodayIsAntonDate() + public function testTodayIsAntonDate() : void { $actual = AntonDate::today(); $this->assertInstanceOf(\Ottosmops\Antondate\ValueObjects\AntonDate::class, $actual); } - public function testComposeAntonDateToString() + public function testComposeAntonDateToString() : void { $actual = AntonDate::compose('1973', '12', '01', 1)->toString(); $expected = 'ca. 1973-12-01'; @@ -50,49 +50,49 @@ public function testComposeAntonDateToString() * @expectException InvalidArgumentException * @expectExceptionMessage AntonDate is not valid: (1973, 13, 01, 1) */ - public function testComposeInvalidAntonDate() + public function testComposeInvalidAntonDate() :void { $this->expectException('InvalidArgumentException'); $this->expectExceptionMessage('AntonDate is not valid: (1973, 13, 1, 1)'); AntonDate::compose('1973', '13', '01', 1); } - public function testComposeYearToString() + public function testComposeYearToString() : void { $actual = AntonDate::compose('1973')->toString(); $expected = '1973'; $this->assertEquals($expected, $actual); } - public function testComposeZeroToFormatted() + public function testComposeZeroToFormatted() : void { $actual = AntonDate::compose('0000')->formatted(); $expected = trans('antondate::antondate.no_date'); $this->assertEquals($expected, $actual); } - public function testComposeYearToFormatted() + public function testComposeYearToFormatted() :void { $actual = AntonDate::compose('1971')->formatted('', 'YY'); $expected = '71'; $this->assertEquals($expected, $actual); } - public function testComposeYearMonthToFormatted() + public function testComposeYearMonthToFormatted() :void { $actual = AntonDate::compose('1971', '03')->formatted('en', '', 'MMMM YYYY'); $expected = 'March 1971'; $this->assertEquals($expected, $actual); } - public function testComposeYearMonthToFormattedWithFebruar() + public function testComposeYearMonthToFormattedWithFebruar() :void { $actual = AntonDate::compose('1971', '02')->formatted('en', '', 'MMMM YYYY'); $expected = 'February 1971'; $this->assertEquals($expected, $actual); } - public function testComposeYearMonthDayToFormattedFrench() + public function testComposeYearMonthDayToFormattedFrench() : void { $this->setLocale(LC_ALL, 'fr_FR'); $actual = AntonDate::compose('1971', '03', '01')->formatted('fr'); @@ -107,7 +107,7 @@ public function testComposeYearMonthDayToFormattedFrench() //$this->assertEquals($expected, $actual); } - public function testComposeYearMonthDayToFormattedEnglish() + public function testComposeYearMonthDayToFormattedEnglish() : void { setlocale(LC_ALL, 'en_US'); $actual = AntonDate::compose('1971', '03', '01')->formatted('en'); @@ -115,7 +115,7 @@ public function testComposeYearMonthDayToFormattedEnglish() $this->assertEquals($expected, $actual); } - public function testComposeYearMonthDayToFormattedGerman() + public function testComposeYearMonthDayToFormattedGerman() : void { \App::setLocale('de'); $actual = AntonDate::compose('1971', '03', '01')->formatted(); @@ -135,6 +135,13 @@ public function testFormatDate() $this->assertEquals($expected, $actual); } + public function testNullDate() + { + $actual = AntonDate::createFromString('0000-00-00')->toMySqlDate(); + $this->assertEquals('0000-00-00', $actual); + + } + public function testComposeYearToMysql() { $actual = AntonDate::compose('1973')->toMysqlDate(); @@ -149,28 +156,28 @@ public function testIsEqualTo() $this->assertTrue($date1->isEqualTo($date2)); } - public function testIsEqualToWithCa() + public function testIsEqualToWithCa() : void { $date1 = AntonDate::createFromString('1973-03-01'); $date2 = AntonDate::compose('1973', '03', '01', 1); $this->assertFalse($date1->isEqualTo($date2, true)); } - public function testIsGreaterThan() + public function testIsGreaterThan() : void { $date1 = AntonDate::createFromString('1773-03-01'); $date2 = AntonDate::createFromString('1771'); $this->assertTrue($date1->isGreaterThan($date2)); } - public function testIsGreaterThan2() + public function testIsGreaterThan2() : void { $date1 = AntonDate::createFromString('303-03-01'); $date2 = AntonDate::createFromString('301'); $this->assertTrue($date1->isGreaterThan($date2)); } - public function testgetYear() + public function testgetYear() : void { $date1 = AntonDate::createFromString('1773-03-01'); $this->assertEquals(1773, $date1->getYear()); @@ -180,7 +187,7 @@ public function testgetYear() $this->assertEquals(301, $date2->getYear()); } - public function testIsGreaterReturnsTrueForZero() + public function testIsGreaterReturnsTrueForZero() : void { $date1 = AntonDate::createFromString('0000-00-00'); $this->assertTrue($date1->isGreaterThan(AntonDate::createFromString('1773-03-01'))); @@ -189,7 +196,7 @@ public function testIsGreaterReturnsTrueForZero() $this->assertTrue($date2->isGreaterThan(AntonDate::createFromString('0000-00-00'))); } - public function testIsLessReturnsTrueForZero() + public function testIsLessReturnsTrueForZero() : void { $date1 = AntonDate::createFromString('0000-00-00'); $this->assertTrue($date1->isLessThan(AntonDate::createFromString('1773-03-01'))); @@ -198,7 +205,7 @@ public function testIsLessReturnsTrueForZero() $this->assertTrue($date2->isLessThan(AntonDate::createFromString('0000-00-00'))); } - public function testGuessDateFromString() + public function testGuessDateFromString() : void { $actual = AntonDate::guessFromString('2. April 2014'); $expected = '2014-04-02';