### Installation via Composer Source: https://github.com/schmittjoh/php-option/blob/master/README.md Shows how to install the phpoption package using Composer. ```bash $ composer require phpoption/phpoption ``` -------------------------------- ### Case 1: Always require an entity Source: https://github.com/schmittjoh/php-option/blob/master/README.md Example of getting an entity from an Option, which throws an exception if the Option is None. ```php $entity = $repo->findSomeEntity(...)->get(); // returns entity, or throws exception ``` -------------------------------- ### Case 2: Fallback to default value Source: https://github.com/schmittjoh/php-option/blob/master/README.md Example of getting an entity from an Option, providing a default value if the Option is None. ```php $entity = $repo->findSomeEntity(...)->getOrElse(new Entity()); // Or, if you want to lazily create the entity. $entity = $repo->findSomeEntity(...)->getOrCall(function() { return new Entity(); }); ``` -------------------------------- ### Using the Option Type in your API Source: https://github.com/schmittjoh/php-option/blob/master/README.md Demonstrates how to return an Option type from a repository method, using Some and None. ```php class MyRepository { public function findSomeEntity($criteria): \PhpOption\Option { if (null !== $entity = $this->em->find(...)) { return new \PhpOption\Some($entity); } // We use a singleton, for the None case. return \PhpOption\None::create(); } } ``` -------------------------------- ### Using Option::fromValue for null handling Source: https://github.com/schmittjoh/php-option/blob/master/README.md Shows a shorter way to create an Option, where null is treated as None by default. ```php class MyRepository { public function findSomeEntity($criteria): \PhpOption\Option { return \PhpOption\Option::fromValue($this->em->find(...)); // or, if you want to change the none value to false for example: return \PhpOption\Option::fromValue($this->em->find(...), false); } } ``` -------------------------------- ### Trying Multiple Alternative Options Source: https://github.com/schmittjoh/php-option/blob/master/README.md Demonstrates using orElse to chain multiple Option-returning calls, returning the first non-empty one. ```php return $this->findSomeEntity() ->orElse($this->findSomeOtherEntity()) ->orElse($this->createEntity()); ``` -------------------------------- ### Lazy-Evaluated Options Source: https://github.com/schmittjoh/php-option/blob/master/README.md Demonstrates how to use LazyOption to defer the evaluation of options until they are actually needed, preventing unnecessary overhead. ```php return $this->findSomeEntity() ->orElse(new LazyOption(array($this, 'findSomeOtherEntity'))) ->orElse(new LazyOption(array($this, 'createEntity'))); ``` -------------------------------- ### More Concise Null Handling Source: https://github.com/schmittjoh/php-option/blob/master/README.md Compares traditional null checks returning a default value with the Option type's getOrElse method. ```php // Before $entity = $this->findSomeEntity(); if (null === $entity) { return new Entity(); } return $entity; // After return $this->findSomeEntity()->getOrElse(new Entity()); ``` -------------------------------- ### No More Control Flow Exceptions Source: https://github.com/schmittjoh/php-option/blob/master/README.md Compares traditional try-catch blocks for exceptions with the Option type's getOrElse method. ```php // Before try { $entity = $this->findSomeEntity(); } catch (NotFoundException $ex) { $entity = new Entity(); } // After $entity = $this->findSomeEntity()->getOrElse(new Entity()); ``` -------------------------------- ### No More Boiler Plate Code Source: https://github.com/schmittjoh/php-option/blob/master/README.md Compares traditional null checking with the concise Option type usage. ```php // Before $entity = $this->findSomeEntity(); if (null === $entity) { throw new NotFoundException(); } echo $entity->name; // After echo $this->findSomeEntity()->get()->name; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.