/** * @param callable $callback * * @return Optional */ public function ensure(callable $callback) : Optional { if ($callback($this->value)) { return $this; } return None::Instance(); }
/** * @param $value * @param null $reject * @return Option */ public static function wrap($value, $reject = null) { if ($value === $reject) { return None::instance(); } return new Some($value); }
function filter($callable) { if (is_callable($callable)) { return $callable($this->get()) ? $this : None::instance(); } elseif (is_array($callable)) { return in_array($this->get(), $callable) ? $this : None::instance(); } throw new \InvalidArgumentException("Expected only callable or array type of argument."); }
/** * Creates a lazy-option with the given callback. * * This is also a helper constructor for lazy-consuming existing APIs where * the return value is not yet an option. By default, we treat ``null`` as * None case, and everything else as Some. * * @param callable $callback The callback to evaluate. * @param array $arguments * @param mixed $noneValue The value which should be considered "None"; null * by default. * * @return Option */ public static function fromReturn($callback, array $arguments = array(), $noneValue = null) { return new LazyOption(function () use($callback, $arguments, $noneValue) { $return = call_user_func_array($callback, $arguments); if ($return === $noneValue) { return None::create(); } return new Some($return); }); }
protected final function dataTypeCheck($value, $variableName, $soft) { if (!is_numeric($value) || static::$strictNumberCheck && is_string($value)) { return static::handleTypeError($this->getTypeString(), $value, $variableName, $soft); } if (!$this->checkRange($value, $variableName, $soft)) { return None::getInstance(); } return $this->subTypeCheck($value, $variableName, $soft); }
protected final function dataTypeCheck($value, $variableName, $soft) { if (!is_string($value)) { return static::handleTypeError($this->getTypeString(), $value, $variableName, $soft); } if ($this->validatorRegex) { if (!preg_match($this->validatorRegex, $value)) { if ($soft) { return None::getInstance(); } throw new TypeError("Value named: {$variableName} with data: \"{$value}\" does not conform to regex: {$this->validatorRegex}"); } } return $this->additionalCheck($value, $variableName, $soft); }
/** * append() をテストします. * 以下を確認します. * * - 任意のスカラー値および Node が追加できること * - null および None を指定した場合, 変化がない (追加されない) こと * - コンテナを追加した場合, 自身ではなくその子ノードが追加されること * * @covers Peach\Markup\Container::append */ public function testAppend() { $nList = new NodeList(); $nList->append("foo"); $nList->append("bar"); $nList->append("baz"); $test = $this->test; $obj = $this->object; $obj->append(null); $obj->append("TEXT"); // (count: 1) $obj->append(new EmptyElement("test")); // (count: 2) $obj->append(None::getInstance()); // added nothing (count: 2) $obj->append($nList); // added 3 nodes (count: 5) $obj->append(array("A", "B", array("C", "D"))); // added 4 nodes (count: 9) $test->assertSame(9, count($obj->getChildNodes())); }
protected function dataTypeCheck($value, $variableName, $soft) { $isObject = is_object($value); $isArray = is_array($value); if (!($isObject || $isArray)) { if ($soft) { return None::getInstance(); } throw new TypeError("Type must be object, type of value named: {$variableName} given: " . gettype($value) . " with data: " . static::safePrint($value)); } //Set missing fields to null, let the child type check if it accepts it. foreach ($this->childrenType as $key => $element) { if ($isArray && !array_key_exists($key, $value)) { $value[$key] = null; } else { if ($isObject && !property_exists($value, $key)) { $value->{$key} = null; } } //throw new TypeError("Required property: $key is missing from object: $variableName"); } if ($this->childrenType !== null) { foreach ($value as $key => $element) { if (!isset($this->childrenType[$key])) { throw new TypeError("Property {$key} in array: {$variableName} does not have a validator Type"); } //$actualValue = $this->childrenType[$key]->check($element, "{$variableName}:{{$key}}"); $actualValue = $this->childrenType[$key]->check($element, "{$variableName}:{$key}"); if ($isArray) { $value[$key] = $actualValue; } else { $value->{$key} = $actualValue; } } } return $value; }
public function reject($value) { if ($this->value === $value) { return None::create(); } return $this; }
/** * @param $method * @param $args * @return $this */ public function call($method, ...$args) { return is_object($this->get()) && method_exists($this->get(), $method) ? Option::Some($this->get()->{$method}(...$args)) : None::instance(); }
/** * Create a new file cache store instance. * * @param array $options * @return void */ public function __construct(array $options = array()) { parent::__construct($options); }
/** * @inheritdoc */ public function find(callable $hof) { return $hof($this->value, 0, $this) ? new Some($this->value) : None::getInstance(); }
/** * @return None */ function None() { return None::getInstance(); }
public function testTryingAgain() { $backoff = new None(); $this->assertFalse($backoff->shouldWeTryAgain(), 'Never try again with this strategy'); }
/** * @inheritdoc * @return None */ public function left() { return None::getInstance(); }
/** * @return None */ function none() : None { return None::Instance(); }
/** * Context の handleNone() が呼び出されることを確認します. * * @covers Peach\Markup\None::accept */ public function testAccept() { $context = new TestContext(); $this->object->accept($context); $this->assertSame("handleNone", $context->getResult()); }
/** * @inheritdoc * @return Some|None */ public function filterNot(callable $hof) { return !call_user_func($hof, $this->x, 0, $this) ? $this : None::getInstance(); }
/** * @param $filePath * @return Option */ public static function ofFile($filePath) { return file_exists($filePath) ? new Some($filePath) : None::getInstance(); }
public function selectInstance($object) { return $this->get() instanceof $object ? $this : None::instance(); }
/** * handleNone() のテストです. * * @covers Peach\Markup\DebugContext::handleNone * @covers Peach\Markup\DebugContext::append */ public function testHandleNone() { $expected = "None\r\n"; $context = $this->object; $none = None::getInstance(); $this->expectOutputString($expected); $context->handleNone($none); $this->assertSame($expected, $context->getResult()); }
/** * @dataProvider serializerProvider */ public function testIsSerialized($var) { $formatter = new None(); $this->assertFalse($formatter->isSerialized($var)); }
/** * @inheritdoc * @see QueryInterface::find * This is an constant on perceived nothings * @return None */ public final function find(callable $hof) { return None::getInstance(); }
/** * @return Option */ public static function None() { return None::instance(); }
/** * 結果に何も追加されないことを確認します。 * * @covers Peach\Markup\DefaultContext::handleNone */ public function testHandleNone() { $none = None::getInstance(); $obj = $this->object; $obj->handle($none); $this->assertSame("", $obj->getResult()); }
/** * @inheritdoc * @return None */ public function right() { return None::getInstance(); }