From edb2667838d9572481f270af0df278027f43e149 Mon Sep 17 00:00:00 2001 From: Lexx YungCarter Date: Wed, 17 Nov 2021 02:33:55 +0300 Subject: [PATCH] $defaultLimit Added `setDefaults()` method --- .../Services/Traits/OsmoseDatesTrait.php | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/Library/Services/Traits/OsmoseDatesTrait.php b/src/Library/Services/Traits/OsmoseDatesTrait.php index fb586ad..8342f84 100644 --- a/src/Library/Services/Traits/OsmoseDatesTrait.php +++ b/src/Library/Services/Traits/OsmoseDatesTrait.php @@ -14,7 +14,12 @@ public function rangeTimespan ($range) return config("osmose.ranges.".request($range)); } - public function limitTimespan ($limits) + /** + * get the timeline limits + * @param array $timeLimits + * @param string $defaultLimit [d,m,w,y] + */ + public function limitTimespan ($limits, $defaultLimit = 'd') { $fromDate = request($limits['from']) ? Carbon::parse(request($limits['from']))->toDateTimeString() @@ -24,10 +29,63 @@ public function limitTimespan ($limits) ? Carbon::parse(request($limits['to']))->endOfDay()->toDateTimeString() : today()->endOfDay()->toDateTimeString(); - if (!$fromDate) return null; + if (! $fromDate) { + + if(! $defaultLimit) + return null; + + $defaults = $this->setDefaults($defaultLimit); + $fromDate = $defaults[0]; + $toDate = $defaults[1]; + } return [ $fromDate, $toDate ]; } + + + /** + * @param string $limit determines whether blind date is set in a month, week or day + * @return array + * Sets the defaults based on carbon's current date + */ + public function setDefaults($limit) + { + if(! in_array($limit, ['d', 'w', 'm', 'y'])) { + abort(500, 'Unknown Date Filter!'); + } + + if($limit == "d") + { + return [ + now()->startOfDay(), + now()->endOfDay(), + ]; + } + + if($limit == "w") + { + return [ + now()->startOfWeek(), + now()->endOfWeek(), + ]; + } + + if($limit == "m") + { + return [ + now()->startOfMonth(), + now()->endOfMonth(), + ]; + } + + if($limit == "y") + { + return [ + now()->startOfYear(), + now()->endOfYear(), + ]; + } + } }