Skip to content

Instantly share code, notes, and snippets.

@nicolopignatelli
Created June 24, 2019 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolopignatelli/d0862634b6eb9c94fdb6e2bbc6082884 to your computer and use it in GitHub Desktop.
Save nicolopignatelli/d0862634b6eb9c94fdb6e2bbc6082884 to your computer and use it in GitHub Desktop.
<?php
$maybeDelayedMessage = $this->delayedMessages->getNext();
if (!$maybeDelayedMessage instanceof Some) {
$this->connection->rollBack();
sleep(10);
return;
}
/** @var DelayedMessage $delayedMessage */
$delayedMessage = $maybeDelayedMessage->extract();
<?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
)
);
});
<?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;
}
};
}
<?php
interface Maybe
{
public function ifSome(callable $f): self;
}
<?php
interface None extends Maybe
{
}
<?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