koen / opening-hours-library
Provides logic to implement Opening Hours on your website
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-04-19 22:21:21 UTC
README
The OpeningHours library is used to implement opening hours logic and validation for your project.
Requirements
- phpunit/phpunit:^9
Installation
Using Composer
composer require koen/opening-hours-library
Usage
Initialize Opening Hours
$openingHours = new OpeningHours(); $openingHours->setOpeningTime($openAt); $openingHours->setClosingTime($closedAt);
Initialize Opening Hours with breaks
The OpeningHoursWithBreaks
class adds the functionality to include breaks to your opening hours. It is possible to add none, one or multiple breaks.
$pause = new Pause($pauseStart, $pauseEnd); $openingHours = new OpeningHoursWithBreaks(); $openingHours->setOpeningTime($openAt); $openingHours->setClosingTime($closedAt); $openingHours->addPause($pause, $pause, ...);
Validation
The validation classes will validate for correct input dates.
- Opening time cannot occur after closing time and vice versa.
- Breaks cannot overlap.
- Breaks cannot occur before being opened or after being closed.
Inject in your classes using dependency injection.
class Controller { private $validator; public function __construct( ValidateOpeningHoursInterface $validator ) { $this->validator = $validator; } public function yourControllerLogic() { $openingHours = new OpeningHours(); ... $this->validator->validate($openingHours); } }
Instantiate validator classes directly.
$openingHours = new OpeningHoursWithBreaks(); $validator = new ValidateOpeningHours(); $validator->validate($openingHours); $validator = new PauseValidator(); $validator->validate($openingHours);
Contributing
Pull requests are welcome. This project is build for academic purposes. For major changes, please provide information about what you have changed and why to discuss your changes.