GithubHelp home page GithubHelp logo

Comments (5)

kylekatarnls avatar kylekatarnls commented on June 8, 2024

Hello, you can build your $exceptions list with PHP native DatePeriod:

$exceptions = [];

foreach (new DatePeriod(new DateTimeImmutable('2016-11-11'), new DateInterval('P1D'), new DateTimeImmutable('2016-11-25')) as $day) {
    $exceptions[$day->format('Y-m-d')] = ['09:00-12:00'];
}

//...
'exceptions' => $exceptions,

from opening-hours.

andimg93 avatar andimg93 commented on June 8, 2024

Hey @kylekatarnls of course I can and this is what I would do as workaround but the point is I want to save such opening hours in database and it is unnecessary extra data for the related ranges therefore I asked.

So saving e.g.: '2016-11-11-2016-11-25' => ['09:00-12:00'], would be much less as:
'2016-11-11' =>
['09:00-12:00'],
'2016-11-12' =>
['09:00-12:00'],
'2016-11-13' =>
['09:00-12:00'],
'2016-11-14' =>
['09:00-12:00'],
'2016-11-15' =>
['09:00-12:00'],
'2016-11-16' =>
['09:00-12:00'],
'2016-11-17' =>
['09:00-12:00'],
'2016-11-18' =>
['09:00-12:00'],
'2016-11-19' =>
['09:00-12:00'],
'2016-11-20' =>
['09:00-12:00'],
'2016-11-21' =>
['09:00-12:00'],
'2016-11-22' =>
['09:00-12:00'],
'2016-11-23' =>
['09:00-12:00'],
'2016-11-24' =>
['09:00-12:00'],

also this is a simple example, imagine several opening hours with a ton of exceptions - therefore you get a ton of unnecessary data to save in db.

from opening-hours.

kylekatarnls avatar kylekatarnls commented on June 8, 2024

Hello, I suggest you just add a customization step:

I also suggest to use some explicit separator, here in the example I used ' to ' so it's more readable and does not overlap - which is already used by date format.

$openingHoursDescription = [
    'monday to friday'  => ['09:00-12:00', '13:00-18:00'],
    'saturday to sunday'=> [],
    'exceptions' => [
        '2016-11-11 to 2016-11-25' => ['09:00-12:00'],
    ],
];

function parseOpeningHoursDescription(array $description): array
{
    foreach ($description as $key => $value) {
        if (is_array($value)) {
            $value = parseOpeningHoursDescription($value);
        }

        $parts = explode(' to ', $key);

        if (count($parts) === 2) {
            unset($description[$key]);
            $isDayName = preg_match('/^[a-z]+$/i', $parts[0]);
            $start = new DateTimeImmutable($parts[0]);
            $end = $start->modify($parts[1])->modify('+12 hours');
            // If using PHP 8.2, you can use DatePeriod::INCLUDE_END_DATE instead of ->modify('+12 hours')

            foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $day) {
                $description[$day->format($isDayName ? 'l' : 'Y-m-d')] = $value;
            }

            continue;
        }

        $description[$key] = $value;
    }

    return $description;
}

$openingHours = OpeningHours::create(parseOpeningHoursDescription($openingHoursDescription));

Then you can save $openingHoursDescription (before the transformation) in your database.

We might think about supporting this syntax out of the box, but need to think about the consequences of supporting multiple syntax for the same result, what if a day appears in multiple ranges and other edge cases.

from opening-hours.

andimg93 avatar andimg93 commented on June 8, 2024

Hello @kylekatarnls

Monday to Friday and stuff like that so for the common weekdays is not something what I would recommend to support for the related range feature request from my side. The reason for it is that there the related overflow is not relevant.

I just talk about the exceptions as there you often go with holidays and stuff like that which take longer than the usual opening hours.

So I end up more like something like the following as fix on my side, but anyway I see it more as a feature request where exactly this is out of the box possible, for the exceptions - so simple said exceptions supporting (date) ranges:

$openingHoursDescription = [
    'monday'  => ['09:00-12:00', '13:00-18:00'],
    'tuesday' => ['09:00-12:00', '13:00-18:00'],
    'wednesday' => ['09:00-12:00', '13:00-18:00'],
    'thursday' => ['09:00-12:00', '13:00-18:00'],
    'friday' => ['09:00-12:00', '13:00-18:00'],
    'saturday' => [],
    'sunday' => [],
    'exceptions' => [
        '2016-11-11 to 2016-11-25' => ['09:00-12:00'],
        // Add other exception ranges as needed
    ],
];

function parseExceptionRanges(array $description): array
{
    $exceptions = $description['exceptions'] ?? [];
    unset($description['exceptions']);

    foreach ($exceptions as $key => $value) {
        $parts = explode(' to ', $key);

        if (count($parts) === 2) {
            $start = new DateTimeImmutable($parts[0]);
            $end = new DateTimeImmutable($parts[1])->modify('+1 day'); // End date is exclusive

            foreach (new DatePeriod($start, new DateInterval('P1D'), $end) as $day) {
                $description[$day->format('Y-m-d')] = $value;
            }
        }
    }

    return $description;
}

$openingHours = OpeningHours::create(parseExceptionRanges($openingHoursDescription));

from opening-hours.

kylekatarnls avatar kylekatarnls commented on June 8, 2024

The reason for it is that there the related overflow is not relevant.

Responsibility of spatie/opening-hours is not to ensure the passed payload can be stored below a certain size. The size of you data will depend on your case, and where you store it and how big it can be will also vary from one app to another. How to compress it is application responsibility, the library should remain agnostic.

If we implement a new feature/syntax, it has to be as generic and consistent as possible, not to solve this one specific issue but to cover various needs where ranges support can be convenient.

from opening-hours.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.