Skip to content

Commit b55a18f

Browse files
author
Nicolò Pignatelli
authored
Added more examples, fixed old ones (#3)
1 parent 9865f66 commit b55a18f

File tree

5 files changed

+67
-21
lines changed

5 files changed

+67
-21
lines changed

examples/either.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
*/
88

99
use Careship\Functional\Either\Either;
10-
use Careship\Functional\Either\No;
1110
use Careship\Functional\Either\Reason;
12-
use Careship\Functional\Either\Yes;
1311
use Careship\Functional\Maybe\Maybe;
12+
use Careship\Functional\Result\ExceptionStack;
1413
use Careship\Functional\Result\Result;
14+
use function Careship\Functional\Either\handle_either;
1515
use function Careship\Functional\Either\no;
1616
use function Careship\Functional\Either\yes;
1717
use function Careship\Functional\extract_some_or_fail;
18+
use function Careship\Functional\Result\handle_result;
1819
use function Careship\Functional\Result\result;
19-
use function Careship\Functional\success_or_fail;
2020

2121
final class Customer {
2222
/** @var bool */
@@ -68,18 +68,20 @@ public function verifyEmail(string $customerId): Result
6868

6969
$customerService = new CustomerService();
7070

71-
/** @var Either $wasCustomerVerified */
72-
$wasCustomerVerified = success_or_fail(
71+
return handle_result(
7372
$customerService->verifyEmail('some_customer_id'),
74-
'Cannot verify customer email'
73+
function (Either $wasCustomerVerified) {
74+
return handle_either(
75+
$wasCustomerVerified,
76+
function() {
77+
return 'Customer successfully verified';
78+
},
79+
function (Reason $reason) {
80+
return $reason->toString();
81+
}
82+
);
83+
},
84+
function (ExceptionStack $exceptionStack) {
85+
$exceptionStack->toString();
86+
}
7587
);
76-
77-
switch (true) {
78-
case $wasCustomerVerified instanceof Yes:
79-
echo 'customer successfully verified';
80-
break;
81-
case $wasCustomerVerified instanceof No:
82-
/** @var Reason $reason */
83-
$reason = $wasCustomerVerified->extract();
84-
echo $reason->toString();
85-
}

examples/http_controller.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Careship\Functional\Either\Reason;
4+
use Careship\Functional\Result\Result;
5+
use function Careship\Functional\Result\handle_result;
6+
7+
interface OrderService {
8+
function setOrderToShipped(string $orderId): Result;
9+
}
10+
11+
final class ShipOrderController
12+
{
13+
/** @var OrderService */
14+
private $orderService;
15+
16+
public function __construct(OrderService $orderService)
17+
{
18+
$this->orderService = $orderService;
19+
}
20+
21+
public function __invoke(string $orderId): JsonResponse
22+
{
23+
return handle_result(
24+
$this->orderService->setOrderToShipped($orderId),
25+
function () {
26+
return JsonResponse('ok');
27+
},
28+
function (Reason $reason) {
29+
return JsonResponse('error: ' . $reason->toString());
30+
}
31+
);
32+
}
33+
}

examples/read_from_a_service.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@
44
* This example combines Result and Maybe to cover all potential cases during a read operation.
55
* Different things could go wrong here (e.g. db connection could not be successful).
66
* This is why we wrap OrderService::findOrderById in a result(), in order to catch any exception.
7-
* The returned value is a Result<Maybe<Order>> object, which we can easily unwrap into an Order object using get_some_or_fail().
7+
* The returned value is a Result<Maybe<Order>> object, which we can easily unwrap into an Order object using extract_some_or_fail().
88
* In case we couldn't find any Order by that orderId (i.e. the Maybe is a None) the function would throw an exception.
99
*/
1010

1111
use Careship\Functional\Maybe\Maybe;
1212
use Careship\Functional\Result\Result;
13-
use function Careship\Functional\get_some_or_fail;
13+
use function Careship\Functional\extract_some_or_fail;
1414
use function Careship\Functional\Result\result;
1515

16+
interface Address {}
17+
1618
interface Order {
1719
function getCustomerAddress(): Address;
1820
}
@@ -39,7 +41,7 @@ public function findOrderById(string $orderId): Result
3941

4042
$orderService = new OrderService();
4143

42-
$order = get_some_or_fail(
44+
$order = extract_some_or_fail(
4345
$orderService->findOrderById('some_order_id'),
4446
'Cannot find order'
4547
);

examples/write_operation.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
use Careship\Functional\Maybe\Maybe;
1212
use Careship\Functional\Result\Result;
13-
use function Careship\Functional\extract_some_or_fail;
1413
use function Careship\Functional\Result\result;
14+
use function Careship\Functional\some_or_fail;
1515
use function Careship\Functional\success_or_fail;
1616

1717
interface Order {
@@ -35,7 +35,7 @@ final class OrderService {
3535
public function setOrderToShipped(string $orderId): Result
3636
{
3737
return result(function() use ($orderId) {
38-
$order = extract_some_or_fail(
38+
$order = some_or_fail(
3939
$this->orderRepository->load($orderId),
4040
'Cannot set order to shipped'
4141
);

src/functions.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,12 @@ function (ExceptionStack $exceptionStack) use ($failMessage) {
6262
}
6363
);
6464
}
65+
66+
/**
67+
* @template T
68+
* @psalm-param Result<T> $result
69+
*/
70+
function success_or_fail(Result $result, string $failMessage)
71+
{
72+
extract_or_fail($result, $failMessage);
73+
}

0 commit comments

Comments
 (0)