/**
  * Creates appropriate option object
  *
  * @param $entity
  * @return PhpOption\Option
  */
 protected function createOption($entity)
 {
     if ($entity === null) {
         return PhpOption\None::create();
     }
     return PhpOption\Some::create($entity);
 }
Exemple #2
0
 public function testOptionReturnedFromClosure()
 {
     $option = $this->ensure(function () {
         return Some::create(1);
     });
     $this->assertTrue($option->isDefined());
     $this->assertSame(1, $option->get());
     $option = $this->ensure(function () {
         return None::create();
     });
     $this->assertFalse($option->isDefined());
 }
 protected function processEntityConfig(array $entity)
 {
     $processedEntity = [];
     if (isset($entity['properties'])) {
         $properties = $entity['properties'];
         $processedProperties = [];
         foreach ($properties as $name => $property) {
             $processedProperties[$name] = Some::create(['candidates' => Some::fromArraysValue($property, 'candidates'), 'key' => Some::fromArraysValue($property, 'key'), 'mandatory' => Some::fromArraysValue($property, 'mandatory'), 'persistent' => Some::fromArraysValue($property, 'persistent'), 'validators' => Some::fromArraysValue($property, 'validators'), 'converters' => Some::fromArraysValue($property, 'converters'), 'filters' => Some::fromArraysValue($property, 'filters')]);
         }
         $processedEntity['properties'] = Some::create($processedProperties);
     } else {
         $processedEntity['properties'] = None::create();
     }
     $processedEntity['decorators'] = Some::fromArraysValue($entity, 'decorators');
     $processedEntity['filters'] = Some::fromArraysValue($entity, 'filters');
     $processedEntity['validators'] = Some::fromArraysValue($entity, 'validators');
     return $processedEntity;
 }
 public function testOrElse()
 {
     $option = \PhpOption\Some::create('foo');
     $this->assertSame($option, \PhpOption\None::create()->orElse($option));
 }
Exemple #5
0
 public function testOrElse()
 {
     $some = \PhpOption\Some::create('foo');
     $lazy = \PhpOption\LazyOption::create(function() use ($some) {return $some;});
     $this->assertSame($some, $lazy->orElse(\PhpOption\None::create()));
     $this->assertSame($some, $lazy->orElse(\PhpOption\Some::create('bar')));
 }
Exemple #6
0
 public function testOrElse()
 {
     $some = \PhpOption\Some::create('foo');
     $this->assertSame($some, $some->orElse(\PhpOption\None::create()));
     $this->assertSame($some, $some->orElse(\PhpOption\Some::create('bar')));
 }
Exemple #7
0
 /**
  * Implements {@link Pipeable::reduceOption}.
  * @param callable $binaryFunction
  * @param mixed $initial
  * @return Option
  */
 public function reduceOption(callable $binaryFunction, $initial = null)
 {
     if (func_num_args() == 1) {
         if ($this->isEmpty()) {
             return None::create();
         } else {
             return Some::create($this->reduce($binaryFunction));
         }
     } else {
         return Some::create($this->reduce($binaryFunction, $initial));
     }
 }
Exemple #8
0
 public function reduceOptionProvider()
 {
     return ['sum closure without initial' => [[10, 5, 1], Some::create(16), function ($a, $b) {
         return $a + $b;
     }], 'concat closure without initial' => [["a", "b", "c"], Some::create("abc"), function ($a, $b) {
         return $a . $b;
     }], 'empty without initial' => [[], None::create(), function ($a, $b) {
         return $a . $b;
     }], 'sum closure with initial' => [[10, 5, 1], Some::create(18), function ($a, $b) {
         return $a + $b;
     }, 2], 'concat closure with initial' => [["a", "b", "c"], Some::create("_abc"), function ($a, $b) {
         return $a . $b;
     }, "_"], 'empty with initial' => [[], Some::create("_"), function ($a, $b) {
         return $a . $b;
     }, "_"]];
 }