PHP code example of ekiwok / option

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 $request->get('id')
        ->map([$this->products, 'findOneById')
        ->map([JsonResponse::class, 'create'])
        ->orElse(new NotFoundResponse());

    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 ...


   $maybeString   = Optional::Some("test");                          // OptionString
   $maybeInt      = Optional::Some(43);                              // OptionInteger
   $maybeDouble   = Optional::Some(0.0);                             // OptionDobule
   $maybeBool     = Optional::Some(false);                           // OptionBoolean
   $maybeArray    = Optional::Some([]);                              // OpionArray
   $maybeBlogPost = Optional::Some($blogPosts->findOneById($uuid));  // Optional
   
   OptionArray::of(null) instanceof None; // true
   OptionArray::of([])   instanceof Some; // true

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 Optional::of("test")
        ->orElse(34.5);

    $maybeIsPalindrome = OptionString::of(null)
        ->map('isPalindrome');
    
    $maybeIsPalindrome instanceof OptionBoolean; // false
    $maybeIsPalindrome instanceof Optional;      // true

   $maybeIsPalindrome = OptionString::of(null)
       ->map('isPalindrome', 'boolean');
   
   $maybeIsPalindrome instanceof OptionBoolean; // true
   $maybeIsPalindrome instanceof Optional;      // false
   
   $maybeIsPalindrome->orElseGet(function () {
      return null;
   });
   // Fatal error: Uncaught TypeError: Return value of class@anonymous::orElseGet() must be of the type boolean

   Optional::registerMappings([
       Foo::class => OptionFoo::class,
       Bar::class => OptionBar::clsss,
   ]);
   
   Optional::Some(new Foo()) instanceof OptionFoo; // true
   Optional::Some(new Bar()) instanceof OptionBar; // true

    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