diff --git a/README.md b/README.md index 794a236..2385328 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,18 @@ I needed to get the diffrence of time, and while the [very good Carbon](https:// You can install this package via composer: -``` bash +```bash $ composer require jpmurray/laravel-countdown ``` -Unless you are using Laravel 5.5 (in which case, package auto-discovery will do it's magic), you will have to add the service provider and facade to your `config/app.php` file. +## Usage with Laravel <= 5.5 + +You should be using versions of this package that are <= 3.0.0. + +If you are using a version of Laravel that doesn't support package autodiscovery, you will have to add the service provider and facade to your `config/app.php` file. Edit file: `config/app.php` + ```php 'providers' => [ // ... @@ -36,7 +41,7 @@ Edit file: `config/app.php` ## Usage -``` php +```php use jpmurray\LaravelCountdown\Countdown; @@ -63,7 +68,7 @@ $countdown->seconds; // 23 $countdown = Countdown::from($now) ->to($now->copy()->addYears(5)) ->get(); - + // To return to humans string $countdown->toHuman(); // 18 years, 33 weeks, 2 days, 18 hours, 4 minutes and 35 seconds @@ -85,7 +90,9 @@ class User extends Authenticatable //... } ``` + #### Example to use Trait: + ```php // This enables the following: // You should have casted your attributes to dates beforehand @@ -102,7 +109,7 @@ composer run test ## Change log -Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. +Please see [CHANGELOG](changelog.MD) for more information on what has changed recently. ## Credits diff --git a/changelog.MD b/changelog.MD index cfe9b99..9ee8625 100644 --- a/changelog.MD +++ b/changelog.MD @@ -4,22 +4,30 @@ All Notable changes to `laravel-countdown` will be documented in this file. Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. +## 3.0.0 + +- Dropping support for Laravel <= 5. Those users should be using earlier versions of the package. + ## 2.0.0 - 2020-04-07 + - Updated dependencies to work for more recent version of Laravel. - Updated Carbon dependency to `^2.0`. ## 1.3.0 - 2018-06-29 + - Should insure compatibility with Laravel up to 5.6. ## 1.2.0 - 2017-08-02 ### Added + - Added a `toHuman` method to return a string that is human readable (@juniorb2ss) - The `toHuman` method accepts a custom string to parse so the returned readable string can be customized / localized (@juniorb2ss) ## 1.1.0 -2017-08-01 ### Added + - Tests! Thank you [Junior](https://github.com/juniorb2ss)! ## 1.0.1 - 2017-07-31 @@ -31,9 +39,15 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip First release! ## Please ignore below this line! + ## Unreleased - YYYY-MM-DD + ### Added + ### Deprecated + ### Fixed + ### Removed + ### Security diff --git a/composer.json b/composer.json index 4fda7b8..76f824f 100644 --- a/composer.json +++ b/composer.json @@ -1,64 +1,64 @@ { - "name": "jpmurray/laravel-countdown", - "type": "library", - "description": "Provide an easy class easy way to get the time difference between two dates, with an extra bonus trait for eloquent", - "keywords": [ - "jpmurray", - "countdown", - "time", - "dates", - "elapsed", - "until" - ], - "homepage": "https://github.com/jpmurray/laravel-countdown", - "license": "MIT", - "authors": [ - { - "name": "Jean-Philippe Murray", - "email": "himself@jpmurray.net", - "homepage": "https://github.com/jpmurray/", - "role": "Developer" - } - ], - "require": { - "php": "^7.0", - "ext-bcmath": "*", - "nesbot/carbon": "^2.0", - "illuminate/support": "5.* || 6.* || 7.*" - }, - "require-dev": { - "orchestra/testbench": "~3.0", - "phpunit/phpunit" : "~4.0||~5.0", - "squizlabs/php_codesniffer": "^2.3", - "nesbot/carbon": "^2.0", - "mockery/mockery": "dev-master@dev" - }, - "autoload": { - "psr-4": { - "jpmurray\\LaravelCountdown\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "Tests\\": "tests/" - } - }, - "minimum-stability": "dev", - "extra": { - "laravel": { - "providers": [ - "jpmurray\\LaravelCountdown\\CountdownServiceProvider" - ], - "aliases": { - "Countdown": "jpmurray\\LaravelCountdown\\Facades\\CountdownFacade" - } - } - }, - "scripts": { - "test": "phpunit", - "coverage": "phpunit --coverage-text --coverage-clover=coverage.clover", - "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", - "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", - "build": "composer run test && composer run check-style && composer run fix-style && composer run coverage" + "name": "jpmurray/laravel-countdown", + "type": "library", + "description": "Provide an easy class easy way to get the time difference between two dates, with an extra bonus trait for eloquent", + "keywords": [ + "jpmurray", + "countdown", + "time", + "dates", + "elapsed", + "until" + ], + "homepage": "https://github.com/jpmurray/laravel-countdown", + "license": "MIT", + "authors": [ + { + "name": "Jean-Philippe Murray", + "email": "himself@jpmurray.net", + "homepage": "https://github.com/jpmurray/", + "role": "Developer" } + ], + "require": { + "php": "^7.0", + "ext-bcmath": "*", + "nesbot/carbon": "^2.0", + "illuminate/support": "6.* || 7.* || 8.*" + }, + "require-dev": { + "orchestra/testbench": "~3.0", + "phpunit/phpunit": "~4.0||~5.0", + "squizlabs/php_codesniffer": "^2.3", + "nesbot/carbon": "^2.0", + "mockery/mockery": "dev-master@dev" + }, + "autoload": { + "psr-4": { + "jpmurray\\LaravelCountdown\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Tests\\": "tests/" + } + }, + "minimum-stability": "dev", + "extra": { + "laravel": { + "providers": [ + "jpmurray\\LaravelCountdown\\CountdownServiceProvider" + ], + "aliases": { + "Countdown": "jpmurray\\LaravelCountdown\\Facades\\CountdownFacade" + } + } + }, + "scripts": { + "test": "phpunit", + "coverage": "phpunit --coverage-text --coverage-clover=coverage.clover", + "check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", + "fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests", + "build": "composer run test && composer run check-style && composer run fix-style && composer run coverage" + } } diff --git a/src/Countdown.php b/src/Countdown.php index 3dd7912..faec543 100644 --- a/src/Countdown.php +++ b/src/Countdown.php @@ -5,8 +5,6 @@ use Exception; use Carbon\Carbon; use DateTimeInterface; -use Illuminate\Foundation\Application; -use jpmurray\LaravelCountdown\Exceptions\InvalidArgumentToCountdown; use jpmurray\LaravelCountdown\Exceptions\InvalidDateFormatToCountdown; use jpmurray\LaravelCountdown\Exceptions\InvalidPropertyStringForHumanException; @@ -22,7 +20,7 @@ class Countdown const SECONDS_PER_WEEK = 604800; const SECONDS_PER_YEAR = 31449600; const STRING_FOR_HUMAN = '{hours} years, {weeks} weeks, {days} days,' - . ' {hours} hours, {minutes} minutes and {seconds} seconds'; + . ' {hours} hours, {minutes} minutes and {seconds} seconds'; private $from = null; private $to = null; @@ -51,7 +49,7 @@ public function __construct(string $timezone, Carbon $carbon) * * @return self */ - public function from($time) : self + public function from($time): self { $this->from = $this->asDateTime($time); @@ -65,7 +63,7 @@ public function from($time) : self * * @return self */ - public function to($time = null) : self + public function to($time = null): self { $time ?: $this->carbon; @@ -90,13 +88,13 @@ public function get() } $this->delta = $this->from->diffInSeconds($this->to); - + $this->computeYears() - ->computeWeeks() - ->computeDays() - ->computeHours() - ->computeMinutes() - ->computeSeconds(); + ->computeWeeks() + ->computeDays() + ->computeHours() + ->computeMinutes() + ->computeSeconds(); return $this; } @@ -151,7 +149,7 @@ protected function asDateTime($value) * * @return bool */ - protected function isStandardDateFormat(string $value) : int + protected function isStandardDateFormat(string $value): int { return preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $value); } @@ -161,7 +159,7 @@ protected function isStandardDateFormat(string $value) : int * * @return self */ - private function computeSeconds() : self + private function computeSeconds(): self { $this->seconds = intval(bcmod(intval($this->delta), self::SECONDS_PER_MINUTE)); @@ -173,7 +171,7 @@ private function computeSeconds() : self * * @return self */ - private function computeMinutes() : self + private function computeMinutes(): self { $this->minutes = intval(bcmod((intval($this->delta) / self::SECONDS_PER_MINUTE), self::MINUTES_PER_HOUR)); @@ -185,7 +183,7 @@ private function computeMinutes() : self * * @return self */ - private function computeHours() : self + private function computeHours(): self { $this->hours = intval(bcmod((intval($this->delta) / self::SECONDS_PER_HOUR), self::HOURS_PER_DAY)); @@ -197,7 +195,7 @@ private function computeHours() : self * * @return self */ - private function computeDays() : self + private function computeDays(): self { $this->days = intval(bcmod((intval($this->delta) / self::SECONDS_PER_DAY), self::DAYS_PER_WEEK)); @@ -209,7 +207,7 @@ private function computeDays() : self * * @return self */ - private function computeWeeks() : self + private function computeWeeks(): self { $this->weeks = intval(bcmod((intval($this->delta) / self::SECONDS_PER_WEEK), self::WEEKS_PER_YEAR)); @@ -221,7 +219,7 @@ private function computeWeeks() : self * * @return self */ - private function computeYears() : self + private function computeYears(): self { $this->years = intval(intval($this->delta) / self::SECONDS_PER_YEAR); @@ -235,7 +233,7 @@ private function computeYears() : self * @throws \jpmurray\LaravelCountdown\Exceptions\InvalidPropertyStringForHumanException * @return string */ - private function getStringForHumanRead(string $string) : string + private function getStringForHumanRead(string $string): string { // search regex preg_match_all( @@ -267,7 +265,7 @@ private function getStringForHumanRead(string $string) : string * @param string $custom Custom string to parse * @return string */ - public function toHuman(string $custom = null) : string + public function toHuman(string $custom = null): string { $sentence = ($custom ?: static::STRING_FOR_HUMAN); diff --git a/src/Traits/CalculateTimeDiff.php b/src/Traits/CalculateTimeDiff.php index b9f1604..fff7a44 100644 --- a/src/Traits/CalculateTimeDiff.php +++ b/src/Traits/CalculateTimeDiff.php @@ -3,8 +3,6 @@ namespace jpmurray\LaravelCountdown\Traits; use Carbon\Carbon; -use Illuminate\Database\Eloquent\Model; -use jpmurray\LaravelCountdown\Countdown; trait CalculateTimeDiff { @@ -21,7 +19,7 @@ public function elapsed($attribute) $now = Carbon::now(); return $countdown->from($attribute) - ->to($now)->get(); + ->to($now)->get(); } /** @@ -37,6 +35,6 @@ public function until($attribute) $now = Carbon::now(); return $countdown->from($now) - ->to($attribute)->get(); + ->to($attribute)->get(); } }