1. Go to this page and download the library: Download shipsaas/never-throw library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shipsaas / never-throw example snippets
use NeverThrow\SuccessResult;
use NeverThrow\ErrorResult;
// Success
class BookShipperOkResult extends SuccessResult
{
public function __construct(
public string $bookingId
) {}
}
enum BookingErrors {
case NO_SHIPPER_AVAILABLE;
case OVERWEIGHT_PACKAGE;
case INVALID_ADDRESS;
}
// Error
class BookShipperErrorResult extends ErrorResult
{
public function __construct(
public BookingErrors $outcome
) {}
}
use NeverThrow\Result;
class BookShipperResult extends Result
{
public function getOkResult(): BookShipperOkResult
{
return parent::getOkResult();
}
public function getErrorResult(): BookShipperErrorResult
{
return parent::getErrorResult();
}
}
public function createBooking(User $user, BookingOrder $order): BookShipperResult
{
$hasAnyShipperAvailable = $this->shipperService->hasAnyShipperAvailable();
if (!$hasAnyShipperAvailable) {
return new BookShipperResult(
new BookShipperErrorResult(
BookingErrors::NO_SHIPPER_AVAILABLE
)
);
}
$isOverweight = !$this->weightService->isValid($order);
if ($isOverweight) {
return new BookShipperResult(
new BookShipperErrorResult(
BookingErrors::OVERWEIGHT_PACKAGE
)
);
}
$bookingId = $this->book($user, $order);
return new BookShipperResult(new BookShipperOkResult($bookingId));
}
$bookingResult = $this->service->createBooking($user, $order);
if ($bookingResult->isError()) {
$errorResult = $bookingResult->getErrorResult();
// handle error
return showError(match ($errorResult->outcome) {
BookingErrors::NO_SHIPPER_AVAILABLE => 'No shipper available at the moment. Please wait',
BookingErrors::OVERWEIGHT_PACKAGE => 'Your package is overweight',
});
}
return showBooking($bookingResult->getOkResult()->bookingId);
function transfer(): Transaction
{
if (!$hasEnoughBalance) {
thrown new InsufficientBalanceError();
}
if (!$invalidRecipient) {
throw new InvalidRecipientError();
}
if (!$invalidMoney) {
throw new InvalidTransferMoneyError();
}
$transaction = $this->transferService->transfer(...);
if (!$transaction) {
throw new TransferFailedError();
}
return $transaction;
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.