- Typed functions and properties (php 7.4)

This commit is contained in:
Dave Mc Nicoll 2019-12-19 09:49:39 -05:00
parent e84718faa7
commit ffe5ddb783
2 changed files with 103 additions and 55 deletions

View File

@ -17,18 +17,18 @@ class Cronard {
'hour' => "*", 'hour' => "*",
'day' => "*", 'day' => "*",
'month' => "*", 'month' => "*",
'week' => "*" 'week' => "*",
]; ];
protected $output = true; public function __construct(? string $tab = null)
{
public function __construct(? string $tab = null) {
if ( $tab ) { if ( $tab ) {
$this->setTab($tab); $this->setTab($tab);
} }
} }
public function setTab(string $tab) { public function setTab(string $tab) : self
{
list($min, $hour, $day, $month, $week) = explode(' ', $tab); list($min, $hour, $day, $month, $week) = explode(' ', $tab);
$this->tab = [ $this->tab = [
@ -49,22 +49,21 @@ class Cronard {
'hour' => "*", 'hour' => "*",
'day' => "*", 'day' => "*",
'month' => "*", 'month' => "*",
'week' => "*" 'week' => "*",
]; ];
unset($this->callback); 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 ( $this->validateCron($this->tab) ) {
if ( $callback = $callback ?: $this->callback ) { if ( null !== ( $callback = $callback ?? $this->callback ?? null ) ) {
$callback->call($this, ...$varbag); $retval = $callback->call($this, ...$arguments);
} }
else { 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(); $this->reset();
@ -72,14 +71,16 @@ class Cronard {
return $retval ?? false; return $retval ?? false;
} }
public function validate_cron($cron) { public function validateCron($cron) : bool
{
$now = new \DateTime(); $now = new \DateTime();
is_string($cron) && ( $cron = array_combine(array_keys(static::CRON_DATE), array_slice(explode(' ', $cron, 6), 0, 5)) ); is_string($cron) && ( $cron = array_combine(array_keys(static::CRON_DATE), array_slice(explode(' ', $cron, 6), 0, 5)) );
foreach($cron as $key => $value) { 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; return false;
} }
} }
@ -91,32 +92,45 @@ class Cronard {
return $set !== null ? $this->callback = $set : $this->callback; return $set !== null ? $this->callback = $set : $this->callback;
} }
public function min($value) { public function min($value) : self
{
$this->tab['min'] = $value; $this->tab['min'] = $value;
return $this; return $this;
} }
public function hour($value) { public function hour($value) : self
{
$this->tab['hour'] = $value; $this->tab['hour'] = $value;
return $this; return $this;
} }
public function day($value) { public function day($value) : self
{
$this->tab['day'] = $value; $this->tab['day'] = $value;
return $this; return $this;
} }
public function week($value) { public function week($value) : self
{
$this->tab['week'] = $value; $this->tab['week'] = $value;
return $this; return $this;
} }
public function month($value) { public function month($value) : self
{
$this->tab['month'] = $value; $this->tab['month'] = $value;
return $this; 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 * Running given function every x minute
@ -124,17 +138,21 @@ class Cronard {
* @param mixed $range * @param mixed $range
* @return $this * @return $this
*/ */
public function every_min($value = "*", $range = "*") { public function everyMin($value = "*", $range = "*") : self
{
$this->tab['min'] = "$range/$value"; $this->tab['min'] = "$range/$value";
return $this; return $this;
} }
public function every_even_min() { public function everyEvenMin() : self
return $this->every_min(2); {
return $this->everyMin(2);
} }
public function every_odd_min() { public function everyOddMin() : self
return $this->every_min("2", "1-59"); {
return $this->everyMin("2", "1-59");
} }
/** /**
@ -143,17 +161,21 @@ class Cronard {
* @param mixed $range * @param mixed $range
* @return $this * @return $this
*/ */
public function every_hour($value = "*", $range = "*") { public function everyHour($value = "*", $range = "*") : self
{
$this->tab['hour'] = "$range/$value"; $this->tab['hour'] = "$range/$value";
return $this; return $this;
} }
public function every_even_hour() { public function everyEvenHour() : self
return $this->every_hour(2); {
return $this->everyHour(2);
} }
public function every_odd_hour() { public function everyOddHour() : self
return $this->every_hour(2, "1-23"); {
return $this->everyHour(2, "1-23");
} }
/** /**
@ -162,17 +184,21 @@ class Cronard {
* @param mixed $range * @param mixed $range
* @return $this * @return $this
*/ */
public function every_day($value = "*", $range = "*") { public function everyDay($value = "*", $range = "*") : self
{
$this->tab['day'] = "$range/$value"; $this->tab['day'] = "$range/$value";
return $this; return $this;
} }
public function every_even_day() { public function everyEvenDay() : self
return $this->every_day(2); {
return $this->everyDay(2);
} }
public function every_odd_day() { public function everyOddDay() : self
return $this->every_day(2, "1-31"); {
return $this->everyDay(2, "1-31");
} }
/** /**
@ -181,76 +207,96 @@ class Cronard {
* @param mixed $range * @param mixed $range
* @return $this * @return $this
*/ */
public function every_month($value = "*", $range = "*") { public function everyMonth($value = "*", $range = "*") : self
{
$this->tab['day'] = "$range/$value"; $this->tab['day'] = "$range/$value";
return $this; return $this;
} }
public function every_even_month() { public function everyEvenMonth() : self
return $this->every_month(2); {
return $this->everyMonth(2);
} }
public function every_odd_month() { public function everyOddMonth() : self
return $this->every_month(2, "1-11"); {
return $this->everyMonth(2, "1-11");
} }
/* Filter by days of the week */ /* Filter by days of the week */
public function sunday() { public function sunday() : self
{
$this->tab['week'][] = 0; $this->tab['week'][] = 0;
return $this; return $this;
} }
public function monday() { public function monday() : self
{
$this->tab['week'][] = 1; $this->tab['week'][] = 1;
return $this; return $this;
} }
public function tuesday() { public function tuesday() : self
{
$this->tab['week'][] = 2; $this->tab['week'][] = 2;
return $this; return $this;
} }
public function wednesday() { public function wednesday() : self
{
$this->tab['week'][] = 3; $this->tab['week'][] = 3;
return $this; return $this;
} }
public function thursday() { public function thursday() : self
{
$this->tab['week'][] = 4; $this->tab['week'][] = 4;
return $this; return $this;
} }
public function friday() { public function friday() : self
{
$this->tab['week'][] = 5; $this->tab['week'][] = 5;
return $this; return $this;
} }
public function saturday() { public function saturday() : self
{
$this->tab['week'][] = 6; $this->tab['week'][] = 6;
return $this; return $this;
} }
public function weekend() { public function weekend() : self
{
$this->tab['week'] = array_merge($this->tab['week'], [0, 6]); $this->tab['week'] = array_merge($this->tab['week'], [0, 6]);
return $this; return $this;
} }
public function weekday() { public function weekday() : self
{
$this->tab['week'] = array_merge($this->tab['week'], [1, 2, 3, 4, 5]); $this->tab['week'] = array_merge($this->tab['week'], [1, 2, 3, 4, 5]);
return $this; return $this;
} }
/** /**
* Evaluate given range from CRON value * 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 # Allows every values
if ( $range === "*" ) { if ( $range === "*" ) {
list($min, $max) = explode('-', static::CRON_RANGE[$key]); list($min, $max) = explode('-', static::CRON_RANGE[$key]);
return range($min, $max); return range($min, $max);
} }
# Already an array # Already an array
@ -267,6 +313,7 @@ class Cronard {
# Normal patterns # Normal patterns
else { else {
$retval = []; $retval = [];
foreach(explode(',' , $range) as $item) { foreach(explode(',' , $range) as $item) {
if( strpos($item, '-') !== false ) { if( strpos($item, '-') !== false ) {
list($min , $max) = explode('-' , $item); list($min , $max) = explode('-' , $item);

View File

@ -23,6 +23,7 @@ trait CronardTrait {
} }
$this->crontabs = include($filepath); $this->crontabs = include($filepath);
return $this; return $this;
} }
} }