コード例 #1
0
ファイル: Maybe.php プロジェクト: sellerlabs/nucleus
 /**
  * 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
ファイル: ArrayAccessMap.php プロジェクト: sellerlabs/nucleus
 /**
  * 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')));
 }
コード例 #4
0
 /**
  * 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
ファイル: FormSpecTest.php プロジェクト: sellerlabs/nucleus
 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
ファイル: Env.php プロジェクト: sellerlabs/nucleus
 /**
  * Get an environment variable or return the default if it is not defined.
  *
  * This avoid any post-processing, such as automatic casting.
  *
  * @param string $key
  * @param null|mixed|callable $default
  *
  * @return mixed|string
  */
 public static function getRaw($key, $default = null)
 {
     $env = ArrayMap::of($_ENV);
     $server = ArrayMap::of($_SERVER);
     if ($env->member($key)) {
         return Maybe::fromJust($env->lookup($key));
     } elseif ($server->member($key)) {
         return Maybe::fromJust($server->lookup($key));
     }
     $value = getenv($key);
     if ($value === false) {
         return Std::thunk($default);
     }
     return $value;
 }
コード例 #7
0
ファイル: Flick.php プロジェクト: sellerlabs/nucleus
 /**
  * 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();
 }
コード例 #8
0
ファイル: SpecFactory.php プロジェクト: sellerlabs/nucleus
 /**
  * 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;
 }
コード例 #9
0
 /**
  * Render the object into a string.
  *
  * @return mixed
  */
 public function render()
 {
     $addColon = function ($label) {
         return Maybe::just(vsprintf('%s:', [$label]));
     };
     return (new Form($this->attributes, $this->spec->getAnnotations()->map(function ($_, $key) use($addColon) {
         return new Row(['class' => 'form-group'], [new Node('label', ['class' => 'col-sm-2 form-control-label'], Maybe::fromMaybe('', $this->spec->getFieldLabel($key)->bind($addColon))), new Div(['class' => 'col-sm-8'], $this->renderFullField($key))]);
     })->append(ArrayMap::of([new Row(['class' => 'form-group'], [new Div(['class' => 'col-sm-offset-2 col-sm-10'], [new Div(['class' => 'btn-group'], [new Button(['type' => 'reset', 'class' => 'btn btn-secondary'], 'Reset'), new Button(['type' => 'submit', 'class' => 'btn btn-primary'], 'Submit')])])])]))))->render();
 }
コード例 #10
0
ファイル: Iterable.php プロジェクト: sellerlabs/nucleus
 /**
  * @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;
 }
コード例 #11
0
ファイル: TypedSpec.php プロジェクト: sellerlabs/nucleus
 /**
  * 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));
 }
コード例 #12
0
ファイル: Spec.php プロジェクト: sellerlabs/nucleus
 /**
  * @param string $fieldName
  *
  * @return bool
  */
 public function getFieldRequired($fieldName)
 {
     return Maybe::fromMaybe(false, $this->getFieldAnnotation($fieldName, static::ANNOTATION_REQUIRED));
 }
コード例 #13
0
ファイル: MaybeTest.php プロジェクト: sellerlabs/nucleus
 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());
 }
コード例 #14
0
ファイル: ArrayMap.php プロジェクト: sellerlabs/nucleus
 /**
  * @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]);
 }