Download the PHP package emilianobovetti/php-option without Composer

On this page you can find all versions of the php package emilianobovetti/php-option. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-option

PhpOption

Tired of Trying to get property of non-object?

This is a porting of the option type in PHP.

Installation

composer require emilianobovetti/php-option

Creating Options

You can create options through constructors (e.g. new Some(0), new None) or using facade methods of the Option class.

Option::none() returns an instance of the None class. Since this class is stateless there is no need to create a new object every time that a None is needed.

Option::create($value, $empty = null) returns an Option object. By default it returns a None if the $value is null, or a new Some($value) otherwise.

You can change this behavior passing the $empty parameter. E.g. Option::create(0, 0) === Option::none()

If another option is passed to the create method, the option itself is returned.

If the $value is callable then the value that it yields is used.

If $empty is callable then it's applied to $value and a None is returned if it yields a falsy value.

Using Options

With a convenient syntax you can use PhpOption to get properties from objects and keys from arrays while handling nulls.

Options also offer a lot of flexibility.

Wait, but I have an array!

Okay, what if I have some resource intensive functions?

If $user->some->property exists and is not null, no methods get called. If the first callback gets called and returns a non-null value, the following functions aren't executed, and so on...

How to output Options?

Option methods

filter(Closure $function)

If the Option is a Some and the given $function returns false on its value, filter method returns None. Returns the Option itself otherwise.

map(Closure $function)

If the Option is a Some returns another Option wrapping the result of $function applied to Option's value. Returns a None otherwise.

each(Closure $function)

Applies given $function to the Option's value if is non-empty, does nothing otherwise. Returns Option itself.

get()

Gets the value of the Option or throws NoneValueException if the Option is a None.

getOrElse(mixed $default)

If the Option is a Some returns its value, returns $default otherwise.

$default can be:

  1. a callback - which is called and its result returned if the Option is a None.
  2. any other PHP value - which is returned in case the Option is a None.

getOrThrow(Exception $e)

Gets the value of the Option or throws $e if the Option is a None.

getOrCall(Closure $function)

If the Option is a Some returns its value, returns the value that yields $function otherwise.

orElse(mixed $default)

Acts like getOrElse, but returns an Option instead of its value.

isDefined() and isEmpty()

The first one returns true if the Option is not a None, the second one returns true if it is.

Dynamic methods

__toString()

A None gets casted to an empty string, while a non-empty Option returns the (string) cast of its value.

__get(string $name)

If the Option is a Some which contains an array or an object, it looks for the given key or property. If it exists and is not null, returns an Option wrapping this value. In any other case returns a None.

__isset(string $name)

Check if the given key or property exists in the Option's value.

E.g.

__invoke(mixed $default)

Makes the Option callable. This is a shortcut for get() and getOrElse() methods.

E.g.

Like getOrElse method $default can be a callback or any other PHP value.

Run tests


All versions of php-option with dependencies

PHP Build Version
Package Version
Requires php Version >=5.5.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package emilianobovetti/php-option contains the following files

Loading the files please wait ....