From b55a18f7c1c2845a7a10431384172555842e2e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Pignatelli?= Date: Wed, 4 Dec 2019 16:56:59 +0100 Subject: [PATCH] Added more examples, fixed old ones (#3) --- examples/either.php | 34 +++++++++++++++++--------------- examples/http_controller.php | 33 +++++++++++++++++++++++++++++++ examples/read_from_a_service.php | 8 +++++--- examples/write_operation.php | 4 ++-- src/functions.php | 9 +++++++++ 5 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 examples/http_controller.php diff --git a/examples/either.php b/examples/either.php index b2001d7..228276f 100644 --- a/examples/either.php +++ b/examples/either.php @@ -7,16 +7,16 @@ */ use Careship\Functional\Either\Either; -use Careship\Functional\Either\No; use Careship\Functional\Either\Reason; -use Careship\Functional\Either\Yes; use Careship\Functional\Maybe\Maybe; +use Careship\Functional\Result\ExceptionStack; use Careship\Functional\Result\Result; +use function Careship\Functional\Either\handle_either; use function Careship\Functional\Either\no; use function Careship\Functional\Either\yes; use function Careship\Functional\extract_some_or_fail; +use function Careship\Functional\Result\handle_result; use function Careship\Functional\Result\result; -use function Careship\Functional\success_or_fail; final class Customer { /** @var bool */ @@ -68,18 +68,20 @@ public function verifyEmail(string $customerId): Result $customerService = new CustomerService(); -/** @var Either $wasCustomerVerified */ -$wasCustomerVerified = success_or_fail( +return handle_result( $customerService->verifyEmail('some_customer_id'), - 'Cannot verify customer email' + function (Either $wasCustomerVerified) { + return handle_either( + $wasCustomerVerified, + function() { + return 'Customer successfully verified'; + }, + function (Reason $reason) { + return $reason->toString(); + } + ); + }, + function (ExceptionStack $exceptionStack) { + $exceptionStack->toString(); + } ); - -switch (true) { - case $wasCustomerVerified instanceof Yes: - echo 'customer successfully verified'; - break; - case $wasCustomerVerified instanceof No: - /** @var Reason $reason */ - $reason = $wasCustomerVerified->extract(); - echo $reason->toString(); -} diff --git a/examples/http_controller.php b/examples/http_controller.php new file mode 100644 index 0000000..d29bea4 --- /dev/null +++ b/examples/http_controller.php @@ -0,0 +1,33 @@ +orderService = $orderService; + } + + public function __invoke(string $orderId): JsonResponse + { + return handle_result( + $this->orderService->setOrderToShipped($orderId), + function () { + return JsonResponse('ok'); + }, + function (Reason $reason) { + return JsonResponse('error: ' . $reason->toString()); + } + ); + } +} diff --git a/examples/read_from_a_service.php b/examples/read_from_a_service.php index 9a66ed8..1cd44e5 100644 --- a/examples/read_from_a_service.php +++ b/examples/read_from_a_service.php @@ -4,15 +4,17 @@ * This example combines Result and Maybe to cover all potential cases during a read operation. * Different things could go wrong here (e.g. db connection could not be successful). * This is why we wrap OrderService::findOrderById in a result(), in order to catch any exception. - * The returned value is a Result> object, which we can easily unwrap into an Order object using get_some_or_fail(). + * The returned value is a Result> object, which we can easily unwrap into an Order object using extract_some_or_fail(). * In case we couldn't find any Order by that orderId (i.e. the Maybe is a None) the function would throw an exception. */ use Careship\Functional\Maybe\Maybe; use Careship\Functional\Result\Result; -use function Careship\Functional\get_some_or_fail; +use function Careship\Functional\extract_some_or_fail; use function Careship\Functional\Result\result; +interface Address {} + interface Order { function getCustomerAddress(): Address; } @@ -39,7 +41,7 @@ public function findOrderById(string $orderId): Result $orderService = new OrderService(); -$order = get_some_or_fail( +$order = extract_some_or_fail( $orderService->findOrderById('some_order_id'), 'Cannot find order' ); diff --git a/examples/write_operation.php b/examples/write_operation.php index ef6fecb..c218c77 100644 --- a/examples/write_operation.php +++ b/examples/write_operation.php @@ -10,8 +10,8 @@ use Careship\Functional\Maybe\Maybe; use Careship\Functional\Result\Result; -use function Careship\Functional\extract_some_or_fail; use function Careship\Functional\Result\result; +use function Careship\Functional\some_or_fail; use function Careship\Functional\success_or_fail; interface Order { @@ -35,7 +35,7 @@ final class OrderService { public function setOrderToShipped(string $orderId): Result { return result(function() use ($orderId) { - $order = extract_some_or_fail( + $order = some_or_fail( $this->orderRepository->load($orderId), 'Cannot set order to shipped' ); diff --git a/src/functions.php b/src/functions.php index 582774e..80e4dea 100644 --- a/src/functions.php +++ b/src/functions.php @@ -62,3 +62,12 @@ function (ExceptionStack $exceptionStack) use ($failMessage) { } ); } + +/** + * @template T + * @psalm-param Result $result + */ +function success_or_fail(Result $result, string $failMessage) +{ + extract_or_fail($result, $failMessage); +}