diff --git a/.gitignore b/.gitignore index 005d2e3..c6b2090 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -.idea -composer.lock \ No newline at end of file +/.idea +composer.lock +/vendor diff --git a/composer.json b/composer.json index 2e34e87..ad50162 100644 --- a/composer.json +++ b/composer.json @@ -3,11 +3,6 @@ "description": "A Laravel package for advanced date filtering and manipulation", "type": "library", "license": "MIT", - "autoload": { - "psr-4": { - "OmarElnaghy\\LaraDateFilters\\": "src/" - } - }, "authors": [ { "name": "omarelnaghy", @@ -15,6 +10,20 @@ } ], "tag": "1.0.0", + "require": { + "php": "^8.1", + "illuminate/database": "^9.0|^10.0", + "illuminate/support": "^9.0|^10.0", + "spatie/laravel-package-tools": "^1.16" + }, + "require-dev": { + "laravel/pint": "^1.13" + }, + "autoload": { + "psr-4": { + "OmarElnaghy\\LaraDateFilters\\": "src/" + } + }, "extra": { "laravel": { "providers": [ @@ -22,5 +31,9 @@ ] } }, - "require": {} + "scripts": { + "format": "vendor/bin/pint", + "format-dirty": "vendor/bin/pint --dirty", + "format-dry-run": "vendor/bin/pint --test" + } } diff --git a/composer.lock b/composer.lock deleted file mode 100644 index e140d02..0000000 --- a/composer.lock +++ /dev/null @@ -1,18 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", - "This file is @generated automatically" - ], - "content-hash": "b39bc6e6fc3541cce38b073ba6fbdc41", - "packages": [], - "packages-dev": [], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": [], - "platform-dev": [], - "plugin-api-version": "2.3.0" -} diff --git a/src/config/lara_date_filter.php b/config/lara_date_filter.php similarity index 86% rename from src/config/lara_date_filter.php rename to config/lara_date_filter.php index 38a3d96..413ad35 100644 --- a/src/config/lara_date_filter.php +++ b/config/lara_date_filter.php @@ -1,4 +1,7 @@ [ 'get{duration}{unit}Records', diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..017b235 --- /dev/null +++ b/pint.json @@ -0,0 +1,10 @@ +{ + "preset": "laravel", + "rules": { + "declare_strict_types": true, + "single_trait_insert_per_statement": true, + "new_with_braces" : { + "anonymous_class": false + } + } +} diff --git a/src/Enums/DateRange.php b/src/Enums/DateRange.php index 8fa33e5..3a6e295 100644 --- a/src/Enums/DateRange.php +++ b/src/Enums/DateRange.php @@ -1,7 +1,8 @@ mergeConfigFrom( - __DIR__ . '/config/lara_date_filter.php', 'lara_date_filter' - ); - - } - - public function boot() + public function configurePackage(Package $package): void { - $this->publishes([ - __DIR__ . '/config/lara_date_filter.php'=> config_path('lara_date_filter.php') - ]); + $package + ->name('lara-date-filter') + ->hasConfigFile('lara_date_filter'); } } diff --git a/src/Traits/Builder/PackageBuilder.php b/src/Traits/Builder/PackageBuilder.php index 3134eec..beb84f2 100644 --- a/src/Traits/Builder/PackageBuilder.php +++ b/src/Traits/Builder/PackageBuilder.php @@ -1,5 +1,7 @@ getModel()->dateColumn ?? "created_at"; + return $this->getModel()->dateColumn ?? 'created_at'; } - public function FilterByDateRange(int $duration, string $dateUnit, Carbon $date, SearchDirection $direction = SearchDirection::AFTER, DateRange $range = DateRange::INCLUSIVE) + public function FilterByDateRange(int $duration, string $dateUnit, Carbon|SupportCarbon $date, SearchDirection $direction = SearchDirection::AFTER, DateRange $range = DateRange::INCLUSIVE) { $end = clone $date; $start = clone $date; - $addToDateMethod = 'add' . ucfirst($dateUnit); - $subFromDateMethod = 'sub' . ucfirst($dateUnit); - + $addToDateMethod = 'add'.ucfirst($dateUnit); + $subFromDateMethod = 'sub'.ucfirst($dateUnit); if ($direction->value === 'after') { $end->$addToDateMethod($duration); $date = $range->value === 'exclusive' ? $date->$addToDateMethod(1) : $date; $end = $range->value === 'exclusive' ? $end->$subFromDateMethod(1) : $end; + return $this->whereBetween($this->getClassVars(), [$date, $end]); } @@ -43,57 +45,55 @@ public function FilterByDateRange(int $duration, string $dateUnit, Carbon $date, return $this->whereBetween($this->getClassVars(), [$start, $date]); } - DateException::invalidValue(); + throw DateException::invalidValue(); } - public function FilterByDateHoursRange(int $duration, SearchDirection $direction, Carbon $date, DateRange $range = DateRange::INCLUSIVE) + public function FilterByDateHoursRange(int $duration, SearchDirection $direction, Carbon|SupportCarbon $date, DateRange $range = DateRange::INCLUSIVE) { return $this->FilterByDateRange($duration, 'hour', $date, $direction, $range); } - public function FilterByDateMinutesRange(int $duration, SearchDirection $direction, Carbon $date, DateRange $range = DateRange::INCLUSIVE) + public function FilterByDateMinutesRange(int $duration, SearchDirection $direction, Carbon|SupportCarbon $date, DateRange $range = DateRange::INCLUSIVE) { return $this->FilterByDateRange($duration, 'minute', $date, $direction, $range); } - public function FilterByDateSecondsRange(int $duration, SearchDirection $direction, Carbon $date, DateRange $range = DateRange::INCLUSIVE) + public function FilterByDateSecondsRange(int $duration, SearchDirection $direction, Carbon|SupportCarbon $date, DateRange $range = DateRange::INCLUSIVE) { return $this->FilterByDateRange($duration, 'second', $date, $direction, $range); } - public function FilterByDateDaysRange(int $duration, SearchDirection $direction, Carbon $date, DateRange $range = DateRange::INCLUSIVE) + public function FilterByDateDaysRange(int $duration, SearchDirection $direction, Carbon|SupportCarbon $date, DateRange $range = DateRange::INCLUSIVE) { return $this->FilterByDateRange($duration, 'day', $date, $direction, $range); } - public function FilterByDateWeeksRange(int $duration, SearchDirection $direction, Carbon $date, DateRange $range = DateRange::INCLUSIVE) + public function FilterByDateWeeksRange(int $duration, SearchDirection $direction, Carbon|SupportCarbon $date, DateRange $range = DateRange::INCLUSIVE) { return $this->FilterByDateRange($duration, 'week', $date, $direction, $range); } - public function FilterByDateMonthsRange(int $duration, SearchDirection $direction, Carbon $date, DateRange $range = DateRange::INCLUSIVE) + public function FilterByDateMonthsRange(int $duration, SearchDirection $direction, Carbon|SupportCarbon $date, DateRange $range = DateRange::INCLUSIVE) { return $this->FilterByDateRange($duration, 'month', $date, $direction, $range); } - /** - * @throws ConventionException - */ public function __call($method, $parameters) { $matches = []; $conventions = config('lara_date_filter.custom_date_filter_convention', []); $conventions = array_merge($conventions, ['filterByDate{duration}{unit}Range']); - if (!empty($conventions)) { + if (! empty($conventions)) { foreach ($conventions as $convention) { $pattern = str_replace(['{duration}', '{unit}'], ['(\d+)', '([A-Za-z]+)'], $convention); $patternWithoutNumeric = explode('(\d+)', $pattern); - $patternWithSlash = $patternWithoutNumeric[0] . '/'; + $patternWithSlash = $patternWithoutNumeric[0].'/'; if (preg_match("/^$pattern$/", $method, $matches) || preg_match("/^$patternWithSlash", $method, $matches)) { - if (isset($matches[1], $matches[2])){ + if (isset($matches[1], $matches[2])) { try { $this->validateConvention($matches[1], $matches[2]); + return $this->filterByDateRange($matches[1], $matches[2], ...$parameters); } catch (Exception $exception) { return parent::__call($method, $parameters); @@ -102,6 +102,7 @@ public function __call($method, $parameters) } } } + return parent::__call($method, $parameters); } @@ -110,17 +111,15 @@ public function __call($method, $parameters) */ private function validateConvention($duration, $unit): void { - if (!isset($duration) || !is_numeric($duration)) { - throw ConventionException::missingDuration(); + if (! isset($duration) || ! is_numeric($duration)) { + throw ConventionException::missingDuration(); } - if (!isset($unit) || !is_numeric($duration)) { - throw ConventionException::missingDateUnit(); + if (! isset($unit) || ! is_numeric($duration)) { + throw ConventionException::missingDateUnit(); } - if (!in_array($unit, $this->dateUnits)) { - throw ConventionException::invalidDateUnit(); + if (! in_array($unit, $this->dateUnits)) { + throw ConventionException::invalidDateUnit(); } } - - }