示例#1
0
 /**
  * The fromMaybe function takes a default value and and Maybe value.
  * If the Maybe is Nothing, it returns the default values; otherwise,
  * it returns the value contained in the Maybe.
  *
  * @param mixed $default
  * @param Maybe $maybe
  *
  * @return mixed
  */
 public static function fromMaybe($default, Maybe $maybe)
 {
     if ($maybe->isNothing()) {
         return $default;
     }
     return $maybe->value;
 }
示例#2
0
 /**
  * Get the value of the provided key.
  *
  * @param string $key
  *
  * @return Maybe
  */
 public function lookup($key)
 {
     if (!$this->member($key)) {
         return Maybe::nothing();
     }
     return Maybe::just($this->value->offsetGet($key));
 }
示例#3
0
 public function testInsert()
 {
     $instance = new ArrayAccessMap(new ArrayObject(['great' => 'job']));
     $instance2 = $instance->insert('omg', 'wow');
     $this->assertNotSame($instance, $instance2);
     $this->assertEquals('wow', Maybe::fromJust($instance2->lookup('omg')));
 }
 /**
  * Get the value of the provided key.
  *
  * @param string $key
  *
  * @return Maybe
  * @throws CoreException
  */
 public function lookup($key)
 {
     foreach ($this->value as $innerKey => $value) {
         if ($key === $innerKey) {
             return Maybe::just($value);
         }
     }
     return Maybe::nothing();
 }
示例#5
0
 public function testSetFieldDescription()
 {
     $spec = new FormSpec();
     $this->assertTrue($spec->getFieldDescription('wow')->isNothing());
     $final = $spec->withFieldDescription('wow', 'WOWs');
     $this->assertNotSame($spec, $final);
     $this->assertTrue($final->getFieldDescription('wow')->isJust());
     $this->assertEquals('WOWs', Maybe::fromJust($final->getFieldDescription('wow')));
     $this->assertEquals(['wow' => 'WOWs'], $final->getDescriptions()->toArray());
 }
示例#6
0
 /**
  * Run the flick on input.
  *
  * @param string|int $input
  *
  * @throws UnknownKeyException
  * @return mixed
  */
 public function go($input)
 {
     Arguments::define(Boa::readMap())->define($input);
     $map = ComplexFactory::toReadMap($this->functions);
     if ($map->member($input)) {
         /** @var callable $function */
         $function = Maybe::fromJust($map->lookup($input));
         return $function();
     } elseif ($map->member($this->default)) {
         /** @var callable $function */
         $function = Maybe::fromJust($map->lookup($this->default));
         return $function();
     }
     throw new UnknownKeyException();
 }
示例#7
0
 /**
  * Attempt to set a field using the provided updater function.
  *
  * @param string $key
  * @param callable $updater
  * @param mixed|null $default
  *
  * @return static
  */
 public function update($key, callable $updater, $default = null)
 {
     return $this->insert($key, $updater(Maybe::fromMaybe($default, $this->lookup($key))));
 }
示例#8
0
 /**
  * Get the type annotation for a field.
  *
  * @param string $fieldName
  *
  * @return AbstractTypeConstraint
  */
 public function getFieldType($fieldName)
 {
     return Maybe::fromMaybe(Boa::any(), $this->getFieldAnnotation($fieldName, static::ANNOTATION_TYPE));
 }
示例#9
0
 /**
  * @param string $fieldName
  *
  * @return bool
  */
 public function getFieldRequired($fieldName)
 {
     return Maybe::fromMaybe(false, $this->getFieldAnnotation($fieldName, static::ANNOTATION_REQUIRED));
 }
示例#10
0
 /**
  * Add one or more constraints to a field.
  *
  * @param string $field
  * @param AbstractConstraint|AbstractConstraint[] $constraint
  *
  * @throws MismatchedArgumentTypesException
  * @return $this
  */
 public function let($field, $constraint)
 {
     Arguments::define(Boa::string(), Boa::either(Boa::instance(AbstractConstraint::class), Boa::arrOf(Boa::instance(AbstractConstraint::class))))->check($field, $constraint);
     if (!$this->constraints->member($field)) {
         $this->constraints = $this->constraints->insert($field, ArrayList::zero());
     }
     if (!is_array($constraint)) {
         $constraint = [$constraint];
     }
     $this->constraints = $this->constraints->insert($field, Maybe::fromMaybe(ArrayList::zero(), $this->constraints->lookup($field))->append(ArrayList::of($constraint)));
     return $this;
 }
示例#11
0
 /**
  * @param callable $predicate
  *
  * @return Maybe
  */
 public function find(callable $predicate)
 {
     $result = Maybe::nothing();
     $this->each(function ($value, $key) use($predicate, &$result) {
         if ($predicate($value, $key, $this)) {
             $result = Maybe::just($value);
             return false;
         }
         return true;
     });
     return $result;
 }
示例#12
0
 /**
  * @param mixed $key
  *
  * @return Maybe
  */
 public function lookup($key)
 {
     if ($this->member($key) === false) {
         return Maybe::nothing();
     }
     $copy = array_merge($this->value);
     return Maybe::just($copy[$key]);
 }
示例#13
0
 public function testFmap()
 {
     $just = Maybe::just('DOGE');
     $nothing = Maybe::nothing();
     $lowerCase = function ($string) {
         return Maybe::just(strtolower($string));
     };
     $result1 = $just->fmap($lowerCase);
     $result2 = $nothing->fmap($lowerCase);
     $this->assertTrue($result1->isJust());
     $this->assertTrue($result2->isNothing());
 }