PHP code example of ganglio / please

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

    

ganglio / please example snippets


$inverse = function ($v) {
  if (empty($v)) {
    throw new \Exception("Division by zero");
  }

  return 1/$v;
}

try {
  $result = $inverse(0);
} catch (\Exception $e) {
  error_log("Error: " . $e->getMessage());
}

$result = new Please($inverse, 0);

if ($result->isSuccess) {
  echo "The result is: " . $result->get();
}

$result->onSuccess(function ($v) {
  echo "The result is: " . $v;
});

$exception = $result->get();

$result->onFailure(function ($e) {
  error_log("Error: " . $e->getMessage());
});

$result->on(
  function ($v) {
    echo "Result: " . $v;
  },
  function ($e) {
    echo "Error: " . $e->getMessage();
  }
);

echo (new Please($inverse, $unknown_divisor))
  ->on(
    function ($v) {
      return "Result: " . $v;
    },
    function ($e) {
      return "Error: " . $e->getMessage;
    }
  )->get();

$strings = [
  'not json',
  '{"a":3,"b":4}',
  'still not json',
  '{"c":5}',
];

$json_decoder_exception = function ($string) {
  $out = json_decode($string);
  if (json_last_error() != JSON_ERROR_NONE) {
    throw new \Exception("Invalid JSON");
  }
  return $out;
};

$results = array_filter(
  array_map(function ($s) use ($json_decoder_exception) {
    return new Please($json_decoder_exception, $s);
  }, $strings),
  function ($e) {
    return $e->isSuccess;
  }
);