/** * @param array $data * @return Option */ public function extract($data) { $result = array(); foreach ($data as $row) { $r = $this->innerExtractor->extract($row)->getOrElse(null); if ($r !== null) { $result[] = $r; } } return $result ? new Some($this->process($result)) : None::create(); }
/** * @param array $data * @return Option */ public function extract($data) { try { $result = array(); foreach ($this->extractors as $field => $extractor) { $result[$field] = $extractor->extract($data)->getOrThrow(new InvalidArgumentException()); } return new Some($result); } catch (InvalidArgumentException $e) { return None::create(); } }
private function extractInner($path, $data) { if (!is_array($data)) { return None::create(); } $key = array_shift($path); if (!array_key_exists($key, $data)) { return None::create(); } elseif ($path) { return $this->extractInner($path, $data[$key]); } else { return new Some($data[$key]); } }
/** * @test */ public function shouldReturnNoneIfAnyExtractorReturnsNone() { $firstExtractor = new TestExtractor($this->anything(), None::create()); $secondExtractor = new TestExtractor($this->anything(), null); $extractor = new CompositeExtractor($firstExtractor, $secondExtractor); $this->assertEquals(None::create(), $extractor->extract(array())); $firstExtractor->assertCalled(); $secondExtractor->assertNotCalled(); $firstExtractor = new TestExtractor($this->anything(), new Some(array())); $secondExtractor = new TestExtractor($this->anything(), None::create()); $extractor = new CompositeExtractor($firstExtractor, $secondExtractor); $this->assertEquals(None::create(), $extractor->extract(array())); $firstExtractor->assertCalled(); $secondExtractor->assertCalled(); }
/** * @test * @return void */ public function otherwiseTranslatesNoneForVoid() { $none = new None(); $this->assertInstanceOf('\\Ustream\\Option\\None', $none->otherwise(function () { })); }
/** * @param callable $predicate * * @return Option */ public function filter($predicate) { return $predicate($this->value) ? $this : None::create(); }
/** * @test * * @param mixed $empty * * @dataProvider emptyOrInvalidValues * * @throws Exception * @return void */ public function mapEmptyOrInvalidToNone($empty) { $this->assertEquals(None::create(), Arr::map($empty, function ($item) { throw new Exception(); })); }
/** * @param mixed $value * @param mixed $noneValue * * @return None|Some */ public static function createFromValue($value, $noneValue = null) { return $value === $noneValue ? None::create() : new Some($value); }