Example #1
0
 /**
  * @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();
 }
Example #2
0
 /**
  * @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();
     }
 }
Example #3
0
 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]);
     }
 }
Example #4
0
 /**
  * @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();
 }
Example #5
0
 /**
  * @test
  * @return void
  */
 public function otherwiseTranslatesNoneForVoid()
 {
     $none = new None();
     $this->assertInstanceOf('\\Ustream\\Option\\None', $none->otherwise(function () {
     }));
 }
Example #6
0
 /**
  * @param callable $predicate
  *
  * @return Option
  */
 public function filter($predicate)
 {
     return $predicate($this->value) ? $this : None::create();
 }
Example #7
0
 /**
  * @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();
     }));
 }
Example #8
0
 /**
  * @param mixed $value
  * @param mixed $noneValue
  *
  * @return None|Some
  */
 public static function createFromValue($value, $noneValue = null)
 {
     return $value === $noneValue ? None::create() : new Some($value);
 }