public function testFilterNot() { $some = new Some('foo'); $this->assertInstanceOf('PhpOption\None', $some->filterNot(function($v) { return strlen($v) > 0; })); $this->assertSame($some, $some->filterNot(function($v) { return strlen($v) === 0; })); }
public function testOrElseWithLazyOptions() { $throws = function () { throw new \LogicException('Should never be called.'); }; $a = new \PhpOption\Some('a'); $b = new \PhpOption\LazyOption($throws); $this->assertEquals('a', $a->orElse($b)->get()); }
/** * 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); }
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()); }
public function testGet() { $some = new \PhpOption\Some('foo'); $this->assertEquals('foo', $some->get()); $this->assertEquals('foo', $some->getOrElse(null)); $this->assertEquals('foo', $some->getOrCall('does_not_exist')); $this->assertEquals('foo', $some->getOrThrow(new \RuntimeException('Not found'))); $this->assertFalse($some->isEmpty()); }
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)); }
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'))); }
public function testFoldLeftRight() { $some = new Some(5); $this->assertSame(6, $some->foldLeft(1, function ($a, $b) { $this->assertEquals(1, $a); $this->assertEquals(5, $b); return $a + $b; })); $this->assertSame(6, $some->foldRight(1, function ($a, $b) { $this->assertEquals(1, $b); $this->assertEquals(5, $a); return $a + $b; })); }
/** * 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)); } }
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; }, "_"]]; }