Skip to content

Commit

Permalink
Added more examples, fixed old ones (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolò Pignatelli authored Dec 4, 2019
1 parent 9865f66 commit b55a18f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 21 deletions.
34 changes: 18 additions & 16 deletions examples/either.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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();
}
33 changes: 33 additions & 0 deletions examples/http_controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Careship\Functional\Either\Reason;
use Careship\Functional\Result\Result;
use function Careship\Functional\Result\handle_result;

interface OrderService {
function setOrderToShipped(string $orderId): Result;
}

final class ShipOrderController
{
/** @var OrderService */
private $orderService;

public function __construct(OrderService $orderService)
{
$this->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());
}
);
}
}
8 changes: 5 additions & 3 deletions examples/read_from_a_service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<Maybe<Order>> object, which we can easily unwrap into an Order object using get_some_or_fail().
* The returned value is a Result<Maybe<Order>> 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;
}
Expand All @@ -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'
);
Expand Down
4 changes: 2 additions & 2 deletions examples/write_operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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'
);
Expand Down
9 changes: 9 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ function (ExceptionStack $exceptionStack) use ($failMessage) {
}
);
}

/**
* @template T
* @psalm-param Result<T> $result
*/
function success_or_fail(Result $result, string $failMessage)
{
extract_or_fail($result, $failMessage);
}

0 comments on commit b55a18f

Please sign in to comment.