1. Go to this page and download the library: Download ekiwok/option 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/ */
ekiwok / option example snippets
/**
* @return string|null
*/
public function get(string $parameter)
/**
* @throws ParameterNotFoundException
*/
public function get(string $parameter): string
public function get(string $parameter): OptionString
$uuid = $request->get('id');
if ($uuid === null) {
return new NotFoundResponse();
}
$product = $this->products->findOneById($uuid);
if ($product === null) {
return new NotFoundResponse();
}
return new JsonResponse($product);
return Optional::Some("I love strict types")->orElse(new \stdClass());
// TypeError: Argument 1 passed to class@anonymous::orElse() must be of the type string, object given, called in ...
interface Option<T>
{
public function equals(Option $another): bool;
public function isPresent(): bool;
public function map(callable $mapper, string $typeToWrap = null): Option;
public function get(): T;
public function orElse(T $value): T;
public function orElseGet(callable $supplier): T;
public function orElseThrow(callable $supplier): T;
static public function of(T $value): Option<T>
static public function Some($value): Some
static public function None(): None
}
return OptionString::of("test")
->orElse(34.5);
// Fatal error: Uncaught TypeError: Argument 1 passed to class@anonymous::orElse() must be of the type string, float given
return $products->findOneById($id)
->map(function (Product $product) {
return new Any($product->getPrice());
})
->orElse(3);
// Not throwing exception because after map result is Optional instead of OptionProduct
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.