public function completeItem($id) : JsonResponse { $status = Nullable::fromValue($id)->reject('')->map('intval')->filter(P::lt(0))->flatMap(Item::class . '::findNullable')->map(function ($item) { return $item->setStateId(State::of('completed')->id)->save(); })->getOrThrow(new \RuntimeException("Item by ID {$id} not found!")); return response()->json(['status' => $status, 'id' => $id]); }
public function testCreateContentList() { $rpbContent1 = $this->getMock('Riak\\Client\\ProtoBuf\\RpbContent', [], [], '', false); $rpbContent2 = $this->getMock('Riak\\Client\\ProtoBuf\\RpbContent', [], [], '', false); $rpbContent1->expects($this->once())->method('getContentType')->willReturn(Option::fromValue('application/json')); $rpbContent2->expects($this->once())->method('getContentType')->willReturn(Option::fromValue('application/json')); $rpbContent1->expects($this->once())->method('getLastMod')->willReturn(Option::fromValue(1420229384)); $rpbContent2->expects($this->once())->method('getLastMod')->willReturn(Option::fromValue(1420229377)); $rpbContent1->expects($this->once())->method('getVtag')->willReturn(Option::fromValue('vtag-hash')); $rpbContent2->expects($this->once())->method('getVtag')->willReturn(Option::fromValue('vtag-hash')); $rpbContent1->expects($this->once())->method('getValue')->willReturn('[1,1,1]'); $rpbContent2->expects($this->once())->method('getValue')->willReturn('[2,2,2]'); $rpbContent1->expects($this->once())->method('getIndexesList')->willReturn([]); $rpbContent2->expects($this->once())->method('getIndexesList')->willReturn([]); $rpbContent1->expects($this->once())->method('getUsermetaList')->willReturn([]); $rpbContent2->expects($this->once())->method('getUsermetaList')->willReturn([]); $rpbContent1->expects($this->once())->method('getLinksList')->willReturn([]); $rpbContent2->expects($this->once())->method('getLinksList')->willReturn([]); $contentList = $this->invokeMethod($this->instance, 'createContentList', [[$rpbContent1, $rpbContent2]]); $this->assertCount(2, $contentList); $this->assertEquals('[1,1,1]', $contentList[0]->value); $this->assertEquals('[2,2,2]', $contentList[1]->value); $this->assertEquals(1420229384, $contentList[0]->lastModified); $this->assertEquals(1420229377, $contentList[1]->lastModified); $this->assertEquals('application/json', $contentList[0]->contentType); $this->assertEquals('application/json', $contentList[1]->contentType); }
/** * @expectedException \Bart\GitHook\GitHookException * @expectedExceptionMessage All repositories are frozen */ public function testWhenAllFrozenRepoAndNoEnvVarConfigured() { $this->shmockAndDieselify('\\Bart\\GitHook\\GitHookSystemConfig', function ($config) { $config->envVarNameForPushUser()->once()->return_value(Option::fromValue(null)); // This should not be called because env var name is None $config->superUserNames()->never(); $config->frozenRepoNames()->once()->return_value(['all']); }, true); $freeze = new CodeFreeze(); $freeze->run($this->head); }
/** * Flexible getter * * ```php * // Get value or throw * $data->attribute('key.sub')->get(); * // Get value or throw custom exception * $data->attribute('m.s.k')->getOrThrow(new \LogicException('does not exists')); * // Or call some closure * $data->attribute('my.sub.key')->getOrCall(function() { return 'fallBackValue';}); * // Or use default value * $data->attribute('my.sub.key')->getOrElse(2); * // Check if it exists * $data->attribute('m.s.k')->isDefined(); * // Value is an array convert to observable * $data->attribute('my.array') * ->map([Observable, 'fromArray') * ->get() * ->subscribeCallback(...); * ``` * * @see https://github.com/schmittjoh/php-option * @param string $key * @return None|Option */ public function attribute($key) { $object = $this->payload; foreach (explode('.', $key) as $segment) { if (!is_object($object) || !isset($object->{$segment})) { return None::create(); } $object = $object->{$segment}; } return Option::fromValue($object); }
/** * Executes a console command. * * @param ContainerInterface $container * @param Command $command * @param string $alias * @param array $args */ protected static function runCommand(ContainerInterface $container, Command $command, $alias, array $args = null) { $application = new Application(static::$kernel); $application->setAutoExit(false); $application->add($command); $command = $application->find($alias); if ($command instanceof ContainerAwareCommand) { $command->setContainer($container); } $commandTester = new CommandTester($command); $call = array_merge(Option::fromValue($args)->getOrElse(array()), array('command' => $command->getName())); $commandTester->execute($call); }
/** * @param string $operator * @return Option Option of a callable. */ public static function forOperator($operator) { $fun = null; switch ($operator) { case "+": $fun = [OperatorCallbacks::class, 'add']; break; case "-": $fun = [OperatorCallbacks::class, 'sub']; break; } return Option::fromValue($fun); }
public function handle(Request $request, Closure $next, string $inputParam = 'input') { $value = $request->input($inputParam); if (!is_array($value)) { $request->merge([$inputParam => Nullable::fromValue($value)]); } else { $nullables = array_map(function ($id) { return Nullable::fromValue($id); }, $value); $request->merge([$inputParam => $nullables]); } return $next($request); }
public function testFromReturn() { $null = function () { return null; }; $false = function () { return false; }; $some = function () { return 'foo'; }; $this->assertTrue(\PhpOption\Option::fromReturn($null)->isEmpty()); $this->assertFalse(\PhpOption\Option::fromReturn($false)->isEmpty()); $this->assertTrue(\PhpOption\Option::fromReturn($false, array(), false)->isEmpty()); $this->assertTrue(\PhpOption\Option::fromReturn($some)->isDefined()); $this->assertFalse(\PhpOption\Option::fromReturn($some, array(), 'foo')->isDefined()); }
/** * Get "n_val" value * @return \PhpOption\Option of type (int) * * */ public function getNVal() { return \PhpOption\Option::fromValue($this->n_val); }
/** * @param Option $option * @param string $className * @return Option */ public static function filterConditionByClass(Option $option, $className) { return $option->filter(function (ConditionInterface $condition) use($className) { return $condition instanceof $className; }); }
/** * Get "value" value * @return \PhpOption\Option of type (int) * * */ public function getValue() { return \PhpOption\Option::fromValue($this->value); }
/** * * @param type $value * @param type $noneValue * @return type */ public function fromValue($value, $noneValue = null) { return \PhpOption\Option::fromValue($value, $noneValue); }
/** * @return Properties */ public static function getProperties() { return Option::fromValue(self::$props)->getOrCall(function () { return self::$props = new Properties(); }); }
/** * Get "server_version" value * @return \PhpOption\Option of type (string) * * */ public function getServerVersion() { return \PhpOption\Option::fromValue($this->server_version); }
/** * Get "counter_value" value * @return \PhpOption\Option of type (int) * * */ public function getCounterValue() { return \PhpOption\Option::fromValue($this->counter_value); }
/** * Get "map_op" value * @return \PhpOption\Option of type (MapOp) * * */ public function getMapOp() { return \PhpOption\Option::fromValue($this->map_op); }
/** * Converts this string to a new character array. * * @return CharArray a newly allocated character array whose length is the length * of this string and whose contents are initialized to contain * the character sequence represented by this string. */ public function toCharArray() { return Option::fromValue($this->charArray)->getOrCall(function () { return $this->charArray = CharArray::fromString($this->value); }); }
/** * Get "tag" value * @return \PhpOption\Option of type (string) * * */ public function getTag() { return \PhpOption\Option::fromValue($this->tag); }
/** * Get "consistent" value * @return \PhpOption\Option of type (boolean) * * */ public function getConsistent() { return \PhpOption\Option::fromValue($this->consistent); }
/** * @return Option [string] Name of environment variable capturing name of user * performing `git push` * @throws \Bart\Configuration\ConfigurationException */ public function envVarNameForPushUser() { return Option::fromValue($this->getValue('env_vars', 'push_user_name', null, false)); }
protected function ensure($value, $noneValue = null) { $option = Option::ensure($value, $noneValue); $this->assertInstanceOf('PhpOption\\Option', $option); return $option; }
/** * Get "type" value * @return \PhpOption\Option of type (string) * * */ public function getType() { return \PhpOption\Option::fromValue($this->type); }
/** * Get "name" value * @return \PhpOption\Option of type (string) * * */ public function getName() { return \PhpOption\Option::fromValue($this->name); }
/** * Get "include_context" value * @return \PhpOption\Option of type (boolean) * * */ public function getIncludeContext() { return \PhpOption\Option::fromValue($this->include_context); }
/** * Get "increment" value * @return \PhpOption\Option of type (int) * * */ public function getIncrement() { return \PhpOption\Option::fromValue($this->increment); }
public function getOptionResult($hydrationMode = null) { return Option::fromReturn(array($this, 'getOneOrNullResult'), array($hydrationMode)); }
/** * Get "done" value * @return \PhpOption\Option of type (boolean) * * */ public function getDone() { return \PhpOption\Option::fromValue($this->done); }
/** * Get "pagination_sort" value * @return \PhpOption\Option of type (boolean) * * */ public function getPaginationSort() { return \PhpOption\Option::fromValue($this->pagination_sort); }
/** * @param Event $event * @param string $email * * @return Option */ public function getRegistrationForEmail(Event $event, $email) { return Option::fromValue($this->createQueryBuilder('r')->andWhere('r.event = :event')->setParameter('event', $event)->andWhere('r.email = :email')->setParameter('email', $email)->andWhere('r.confirmed = 1')->orderBy('r.created', 'DESC')->setMaxResults(1)->getQuery()->getOneOrNullResult()); }
/** * Option factory, which creates new option based on passed value. * If value is already an option, it simply returns * If value is a \Closure, LazyOption with passed callback created and returned. If Option returned from callback, * it returns directly (flatMap-like behaviour) * On other case value passed to Option::fromValue() method * * @param Option|\Closure|mixed $value * @param null $noneValue used when $value is mixed or Closure, for None-check * * @return Option */ public static function ensure($value, $noneValue = null) { if ($value instanceof Option) { return $value; } elseif ($value instanceof \Closure) { return new LazyOption(function () use($value, $noneValue) { $return = $value(); if ($return instanceof Option) { return $return; } else { return Option::fromValue($return, $noneValue); } }); } else { return Option::fromValue($value, $noneValue); } }