cmixin / season
Carbon mixin to handle business days and opening hours
Fund package maintenance!
Open Collective
Tidelift
Requires
- php: >=8.2
- psr/container: *
Requires (Dev)
- nesbot/carbon: ^2.68.1
- phpunit/phpunit: ^10.2.3
README
DateTime
modifiers such as startOfSeason
, isInSummer
Season
can be used as a service which can work with anyDateTime
orDateTimeImmutable
object or date strings (which includes any subclass such asCarbon
orChronos
).- Or it can be used as a mixin to call the methods directly on
Carbon
objects. - Mixin get automatically enabled on Laravel if auto-discovery is on.
How to use
The simple way
(new \Season\Season)->isInSummer('2022-06-25') (new \Season\Season)->isInSummer(new DateTimeImmutable('2022-06-25')) (new \Season\Season)->isInSummer(Carbon::now())
Methods are available from the class \Season\Season
which is cheap to create,
so you can just call methods from a new class everytime.
As a good practice, it's recommended you import the class with use Season\Season;
at the beginning of the file:
use Season\Season; (new Season)->isInSummer('2022-06-25'); (new Season)->isInSummer(new DateTimeImmutable('2022-06-25'));
And also to keep the same instance to re-use multiple times:
use Season\Season; $season = new Season(); $season->isInSummer('2022-06-25'); $season->isInSummer(new DateTimeImmutable('2022-06-25'));
As a service
You can use dependency injection with your framework:
use Season\Season; use Psr\Clock\ClockInterface; class ProductController { public function new(Season $season, ClockInterface $clock) { $seasonName = $season->getSeason($clock->now())->getName(); } }
With Laravel it will be provided by default.
With Symfony, you'll have to register \Season\Season
as a service and
so edit config/services.yaml the following way:
services: _defaults: # ensure you get the tag 'controller.service_arguments' # if you need services to be available as controller # methods arguments: tags: [ 'controller.service_arguments' ] # then add the class name as a service Season\Season:
Learn more from Symfony documentation: Configuring Services in the Container
As Carbon methods (mixin)
use Carbon\Carbon; use Cmixin\SeasonMixin; // On Laravel, the mixin will be loaded by default. // On other applications/framework, you can enable it with: Carbon::mixin(SeasonMixin::class); Carbon::parse('2022-06-25')->isInSummer();
You can use mixin on CarbonImmutable
, Carbon
or any of
their subclasses.
Configuration
Disable mixin in Laravel
If you use Laravel but don't want to enable Season
mixin
globally for Carbon
, you can remove it from auto-discovery using:
"extra": { "laravel": { "dont-discover": [ "cmixin/season" ] } },
Customize days
By default, Season
is created with the following config:
[ 3 => 20, // spring 6 => 21, // summer 9 => 22, // fall 12 => 21, // winter ]
mapping the month (as key) with the day (as value) for each season start.
But you can pass a custom config with other days as long as the keys remain.
use Season\Season; $season = new Season([ 3 => 21, // spring 6 => 21, // summer 9 => 21, // fall 12 => 21, // winter ]);
In Laravel, you can set the config in config/season.php which will apply to both the mixin and the service:
<?php return [ 3 => 21, // spring 6 => 21, // summer 9 => 21, // fall 12 => 21, // winter ];
When using Carbon
mixin alone, you can still call setSeasonConfig()
to
change the config globally:
Carbon::setSeasonConfig([ 3 => 21, // spring 6 => 21, // summer 9 => 21, // fall 12 => 21, // winter ]);
Or apply a config on specific call:
echo Carbon::now()->getSeason([ 3 => 21, // spring 6 => 21, // summer 9 => 21, // fall 12 => 21, // winter ])->getName();
With Symfony you can edit config/services.yaml to configure the service:
services: Season\Season: arguments: $config: 3: 20 6: 12 9: 22 12: 21