PHP code example of shalvah / ensure

1. Go to this page and download the library: Download shalvah/ensure 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/ */

    

shalvah / ensure example snippets


use function Shalvah\Ensure\when;
use function Shalvah\Ensure\ensure;

ensure($pilot->isInUniform())
  ->orElseDeny('Please put on your uniform', $pilot->uniform);

when(!$pilot->isLicensed())
  ->ensure($flight->isTestFlight())
  ->orElseDeny('You are only allowed to fly test flights.');
   
when(!$flightPlanHasBeenSubmitted)
  ->ensure(
    $whitelistedAirports->

ensure($day == 'Saturday')->orElseDeny('Visiting hours are Saturdays only.');
ensure(function () use ($passenger) {
  $flight = Flight::getFlight($passenger->flightName);
  return $flight && $flight->passengers->

when($couponCodeWasApplied)
  ->ensure($product->isEligibleForDiscount())
  ->orElseDeny('Sorry, this product is not eligible for promotions.');

try {
  ensure($user->isAllowedToView($product))
    ->orElseDeny('This product is not available in your region.', $this->suggestSimilarProducts($product, $user));
} catch (\Shalvah\Ensure\RequirementFailedException $e) {
    return response()->json([
      'message' => $e->getMessage(),
      'similar_products' => $e->getData()
    ], 400);
}

// or you could use set_exception_handller()
// or whatever mechanism your framework uses