Created
June 24, 2019 09:13
-
-
Save nicolopignatelli/d0862634b6eb9c94fdb6e2bbc6082884 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$maybeDelayedMessage = $this->delayedMessages->getNext(); | |
if (!$maybeDelayedMessage instanceof Some) { | |
$this->connection->rollBack(); | |
sleep(10); | |
return; | |
} | |
/** @var DelayedMessage $delayedMessage */ | |
$delayedMessage = $maybeDelayedMessage->extract(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$maybeOverlappingAppointment = $this->appointments->getOverlapping($date); | |
$maybeOverlappingAppointment->ifSome(function (Appointment $overlappingAppointment) { | |
throw new CannotCreateAppointment( | |
\sprintf( | |
'Schedule of appointment with ID %s overlaps with appointment with ID %s.', | |
$appointmentId1, | |
$appointmentId2 | |
) | |
); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function none(): Maybe | |
{ | |
return new class() implements None { | |
public function ifSome(callable $f): Maybe | |
{ | |
return none(); | |
} | |
}; | |
} | |
function some($value): Maybe | |
{ | |
return new class($value) implements Some { | |
private $value; | |
public function __construct($value) | |
{ | |
$this->value = $value; | |
} | |
public function extract() | |
{ | |
return $this->value; | |
} | |
public function ifSome(callable $f): Maybe | |
{ | |
return $f($this->extract()) ?? $this; | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Maybe | |
{ | |
public function ifSome(callable $f): self; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface None extends Maybe | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
interface Some extends Maybe | |
{ | |
public function extract(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment