From c7947781a8a7236a2638dede7589e2092442ec82 Mon Sep 17 00:00:00 2001 From: fernandolcx Date: Mon, 16 Sep 2019 21:04:17 -0300 Subject: [PATCH] suporte preliminar a semanas (weeks) --- src/Duration.php | 46 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/Duration.php b/src/Duration.php index 0c6c0bf..ca505fb 100755 --- a/src/Duration.php +++ b/src/Duration.php @@ -4,12 +4,17 @@ class Duration { + public $years; + public $weeks; + public $days; public $hours; public $minutes; public $seconds; public $hoursPerDay; + public $hoursPerWeek; + public $hoursPerYear; private $output; private $daysRegex; @@ -22,16 +27,21 @@ class Duration * * @param int|float|string|null $duration */ - public function __construct($duration = null, $hoursPerDay = 24) + public function __construct($duration = null, $hoursPerDay = 24, $hoursPerWeek = 168, $hoursPerYear = 8760) { $this->reset(); + $this->yearsRegex = '/([0-9\.]+)\s?(?:y|Y)/'; + $this->weeksRegex = '/([0-9\.]+)\s?(?:w|W)/'; + $this->daysRegex = '/([0-9\.]+)\s?(?:d|D)/'; $this->hoursRegex = '/([0-9\.]+)\s?(?:h|H)/'; $this->minutesRegex = '/([0-9]{1,2})\s?(?:m|M)/'; $this->secondsRegex = '/([0-9]{1,2}(\.\d+)?)\s?(?:s|S)/'; $this->hoursPerDay = $hoursPerDay; + $this->hoursPerWeek = $hoursPerWeek; + $this->hoursPerYear = $hoursPerYear; if (null !== $duration) { $this->parse($duration); @@ -77,6 +87,11 @@ public function parse($duration) $this->hours = (int)($this->hours - ($this->days * $this->hoursPerDay)); } + if ($this->weeks >= $this->hoursPerWeek) { + $this->days = (int)floor($this->hours / $this->hoursPerDay); + $this->hours = (int)($this->hours - ($this->days * $this->hoursPerDay)); + } + return $this; } @@ -97,10 +112,24 @@ public function parse($duration) return $this; } - if (preg_match($this->daysRegex, $duration) || + if (preg_match($this->yearsRegex, $duration) || + preg_match($this->weeksRegex, $duration) || + preg_match($this->daysRegex, $duration) || preg_match($this->hoursRegex, $duration) || preg_match($this->minutesRegex, $duration) || preg_match($this->secondsRegex, $duration)) { + if (preg_match($this->yearsRegex, $duration, $matches)) { + $num = $this->numberBreakdown((float) $matches[1]); + $this->weeks += (int)$num[0]; + $this->days += $num[1] * $this->hoursPerDay; + } + + if (preg_match($this->weeksRegex, $duration, $matches)) { + $num = $this->numberBreakdown((float) $matches[1]); + $this->weeks += (int)$num[0]; + $this->days += $num[1] * $this->hoursPerDay; + } + if (preg_match($this->daysRegex, $duration, $matches)) { $num = $this->numberBreakdown((float) $matches[1]); $this->days += (int)$num[0]; @@ -141,7 +170,7 @@ public function toSeconds($duration = null, $precision = false) if (null !== $duration) { $this->parse($duration); } - $this->output = ($this->days * $this->hoursPerDay * 60 * 60) + ($this->hours * 60 * 60) + ($this->minutes * 60) + $this->seconds; + $this->output = ($this->weeks * $this->hoursPerWeek * 60 * 60) + ($this->days * $this->hoursPerDay * 60 * 60) + ($this->hours * 60 * 60) + ($this->minutes * 60) + $this->seconds; return $precision !== false ? round($this->output, $precision) : $this->output; } @@ -259,6 +288,15 @@ public function humanize($duration = null) $this->output = $this->days . 'd ' . $this->output; } + if ($this->weeks > 0) { + $this->output = $this->weeks . 'w ' . $this->output; + } + + if ($this->years > 0) { + $this->output = $this->years . 'y ' . $this->output; + } + + return trim($this->output()); } @@ -299,6 +337,8 @@ private function reset() $this->minutes = 0; $this->hours = 0; $this->days = 0; + $this->weeks = 0; + $this->years = 0; } /**