-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nextOpen() and nextClose() don't return the correct values when called simultaneously #73
Comments
Hi, I would add the fact that the method modify the date is unexpected. It should be copied. Then this should be reassigned here: https://github.com/spatie/opening-hours/blob/master/src/OpeningHours.php#L181 to work with immutable date. |
An other problem is $timeRange iteration is not reset between 2 calls: |
We work around this bug using: |
Thanks a lot for the tips @kylekatarnls As I had to decorate the whole object anyway for business purposes, I used the decoration to temporary fix the different issues I encountered. As I'm only using few methods, this solution - even if not the prettiest thing I coded - makes the job final class MyOpeningHours
{
/**
* @var OpeningHours
*/
private $openingHours;
/**
* @var string
*/
private $tz;
public function __construct(array $data, string $tz = 'UTC')
{
$this->openingHours = OpeningHours::create($data);
$this->openingHours->setTimezone($tz);
$this->tz = $tz;
}
public function nextOpen(DateTimeInterface $dateTime) : DateTime
{
$clonedOpeningHours = OpeningHours::create($this->toArray());
$clonedOpeningHours->setTimezone($this->tz);
$clonedDateTime = new DateTime($dateTime->format(DateTime::ATOM));
return $clonedOpeningHours->nextOpen($clonedDateTime);
}
/**
* Return the array that can be used to rebuild another OpeningHours object
*
* @return array In a format acceptable by the __construct and factory method
*
* [
* 'monday' => [...]
* 'tuesday' => [...]
* ...
* ]
*/
public function toArray() : array { //... }
} I may use your workaround if our needs evolve before the correction is merged :) thanks anyway |
Fix #73 and support immutable dates
Hello,
The following test does not pass :
DateTime
$mondayAfternoon
is modified when passed tonextOpen()
andnextClose()
methods. After the first assertion,$mondayAfternoon
value is the same asnextOpen()
return.The problem persists even when I replace
$mondayAfternoon
param by a clone.$mondayAfternoon
is not modified anymore, butnextClose()
will return a result based on a previousnextXXX()
call.The following test
fails with the following message
Finally I cannot pass a
new DateTimeImmutable
object tonextOpen()
andnextClose()
methods (even their signatures acceptDateTimeInterface
) because of the return type hint (DateTime
) but it may be another ticket.The text was updated successfully, but these errors were encountered: