phpninjas/comprehensions

There is no license information available for the latest version (v0.1.0) of this package.

v0.1.0 2015-01-06 23:08 UTC

This package is not auto-updated.

Last update: 2024-12-17 05:20:49 UTC


README

Installation

composer.json

{
  "require": {
    "phpninjas/comprehensions":"dev-master"
  }
}

Options

Imagine if you're using Doctrine, and you don't know if attempting to find an object by id will result in an object or a null value. Thus you might want to wrap the object into an option. It will either be Some[DbObject] or None. However, you can still perform mapping operations on it as if it were going to be a DbObject. Thus you negate the need for null checks.

$product = $entityManager->find('Product', "some identifier");

if ($product === null) {
    return $product->getName();   
}
return "";

becomes

$product = Option($entityManager->find('Product', "some identifier"));
$name = $product->map(function($p){
  return $p->getName();
});

return $name->getOrElse("");