From ffe5ddb783da0d93ebd9eb88058d3817dd2d028e Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 19 Dec 2019 09:49:39 -0500 Subject: [PATCH] - Typed functions and properties (php 7.4) --- src/Cronard.php | 157 ++++++++++++++++++++++++++++--------------- src/CronardTrait.php | 1 + 2 files changed, 103 insertions(+), 55 deletions(-) diff --git a/src/Cronard.php b/src/Cronard.php index 47abcad..3e46c08 100644 --- a/src/Cronard.php +++ b/src/Cronard.php @@ -17,18 +17,18 @@ class Cronard { 'hour' => "*", 'day' => "*", 'month' => "*", - 'week' => "*" + 'week' => "*", ]; - protected $output = true; - - public function __construct(? string $tab = null) { + public function __construct(? string $tab = null) + { if ( $tab ) { $this->setTab($tab); } } - public function setTab(string $tab) { + public function setTab(string $tab) : self + { list($min, $hour, $day, $month, $week) = explode(' ', $tab); $this->tab = [ @@ -49,22 +49,21 @@ class Cronard { 'hour' => "*", 'day' => "*", 'month' => "*", - 'week' => "*" + 'week' => "*", ]; unset($this->callback); } - public function run(array $varbag = [], Closure $callback = null) + public function run(array $arguments = [], Closure $callback = null) { - if ( $this->validate_cron($this->tab) ) { - if ( $callback = $callback ?: $this->callback ) { - $callback->call($this, ...$varbag); + if ( $this->validateCron($this->tab) ) { + if ( null !== ( $callback = $callback ?? $this->callback ?? null ) ) { + $retval = $callback->call($this, ...$arguments); } else { - throw new \Error("A callback must be provided to run the task"); + throw new \InvalidArgumentException("A proper callback must be provided to run the task, be it in the function call directly or at class-level."); } - } $this->reset(); @@ -72,14 +71,16 @@ class Cronard { return $retval ?? false; } - public function validate_cron($cron) { + public function validateCron($cron) : bool + { $now = new \DateTime(); + is_string($cron) && ( $cron = array_combine(array_keys(static::CRON_DATE), array_slice(explode(' ', $cron, 6), 0, 5)) ); foreach($cron as $key => $value) { - $item_range = $this->_get_range_values($key, $value); + $item_range = $this->getRangeValue($key, $value); - if ( ! in_array((int)$now->format(static::CRON_DATE[$key]), $item_range, false) ){ + if ( ! in_array( (int) $now->format(static::CRON_DATE[$key]), $item_range, false) ){ return false; } } @@ -91,32 +92,45 @@ class Cronard { return $set !== null ? $this->callback = $set : $this->callback; } - public function min($value) { + public function min($value) : self + { $this->tab['min'] = $value; + return $this; } - public function hour($value) { + public function hour($value) : self + { $this->tab['hour'] = $value; + return $this; } - public function day($value) { + public function day($value) : self + { $this->tab['day'] = $value; + return $this; } - public function week($value) { + public function week($value) : self + { $this->tab['week'] = $value; + return $this; } - public function month($value) { + public function month($value) : self + { $this->tab['month'] = $value; + return $this; } - public function every_minute($value = "*", $range = "*") { return $this->every_min($value, $range); } + public function everyMinute($value = "*", $range = "*") : self + { + return $this->everyMin($value, $range); + } /** * Running given function every x minute @@ -124,17 +138,21 @@ class Cronard { * @param mixed $range * @return $this */ - public function every_min($value = "*", $range = "*") { + public function everyMin($value = "*", $range = "*") : self + { $this->tab['min'] = "$range/$value"; + return $this; } - public function every_even_min() { - return $this->every_min(2); + public function everyEvenMin() : self + { + return $this->everyMin(2); } - public function every_odd_min() { - return $this->every_min("2", "1-59"); + public function everyOddMin() : self + { + return $this->everyMin("2", "1-59"); } /** @@ -143,17 +161,21 @@ class Cronard { * @param mixed $range * @return $this */ - public function every_hour($value = "*", $range = "*") { + public function everyHour($value = "*", $range = "*") : self + { $this->tab['hour'] = "$range/$value"; + return $this; } - public function every_even_hour() { - return $this->every_hour(2); + public function everyEvenHour() : self + { + return $this->everyHour(2); } - public function every_odd_hour() { - return $this->every_hour(2, "1-23"); + public function everyOddHour() : self + { + return $this->everyHour(2, "1-23"); } /** @@ -162,17 +184,21 @@ class Cronard { * @param mixed $range * @return $this */ - public function every_day($value = "*", $range = "*") { + public function everyDay($value = "*", $range = "*") : self + { $this->tab['day'] = "$range/$value"; + return $this; } - public function every_even_day() { - return $this->every_day(2); + public function everyEvenDay() : self + { + return $this->everyDay(2); } - public function every_odd_day() { - return $this->every_day(2, "1-31"); + public function everyOddDay() : self + { + return $this->everyDay(2, "1-31"); } /** @@ -181,76 +207,96 @@ class Cronard { * @param mixed $range * @return $this */ - public function every_month($value = "*", $range = "*") { + public function everyMonth($value = "*", $range = "*") : self + { $this->tab['day'] = "$range/$value"; + return $this; } - public function every_even_month() { - return $this->every_month(2); + public function everyEvenMonth() : self + { + return $this->everyMonth(2); } - public function every_odd_month() { - return $this->every_month(2, "1-11"); + public function everyOddMonth() : self + { + return $this->everyMonth(2, "1-11"); } /* Filter by days of the week */ - public function sunday() { + public function sunday() : self + { $this->tab['week'][] = 0; + return $this; } - public function monday() { + public function monday() : self + { $this->tab['week'][] = 1; + return $this; } - public function tuesday() { + public function tuesday() : self + { $this->tab['week'][] = 2; + return $this; } - public function wednesday() { + public function wednesday() : self + { $this->tab['week'][] = 3; + return $this; } - public function thursday() { + public function thursday() : self + { $this->tab['week'][] = 4; + return $this; } - public function friday() { + public function friday() : self + { $this->tab['week'][] = 5; + return $this; } - public function saturday() { + public function saturday() : self + { $this->tab['week'][] = 6; + return $this; } - public function weekend() { + public function weekend() : self + { $this->tab['week'] = array_merge($this->tab['week'], [0, 6]); + return $this; } - public function weekday() { + public function weekday() : self + { $this->tab['week'] = array_merge($this->tab['week'], [1, 2, 3, 4, 5]); + return $this; } /** * Evaluate given range from CRON value - * - * @param type $key - * @param type $range - * @return type */ - protected function _get_range_values($key, $range) { + protected function getRangeValue($key, $range) : array + { # Allows every values if ( $range === "*" ) { list($min, $max) = explode('-', static::CRON_RANGE[$key]); + return range($min, $max); } # Already an array @@ -267,6 +313,7 @@ class Cronard { # Normal patterns else { $retval = []; + foreach(explode(',' , $range) as $item) { if( strpos($item, '-') !== false ) { list($min , $max) = explode('-' , $item); diff --git a/src/CronardTrait.php b/src/CronardTrait.php index 4bda0f9..1073159 100644 --- a/src/CronardTrait.php +++ b/src/CronardTrait.php @@ -23,6 +23,7 @@ trait CronardTrait { } $this->crontabs = include($filepath); + return $this; } }