Skip to content

Commit

Permalink
HTTP: list keys support
Browse files Browse the repository at this point in the history
  • Loading branch information
robocoder committed Mar 6, 2017
1 parent 195ac36 commit 6b5731d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Riak/Api/Http.php
Expand Up @@ -214,6 +214,10 @@ protected function buildPath()
case 'Basho\Riak\Command\Object\Delete':
$this->path = sprintf('/types/%s/buckets/%s/keys/%s', $bucket->getType(), $bucket->getName(), $key);
break;
case 'Basho\Riak\Command\Object\Keys':
$this->headers[static::CONTENT_TYPE_KEY] = static::CONTENT_TYPE_JSON;
$this->path = sprintf('/types/%s/buckets/%s/keys', $bucket->getType(), $bucket->getName());
break;
case 'Basho\Riak\Command\DataType\Counter\Store':
case 'Basho\Riak\Command\DataType\Set\Store':
/** @noinspection PhpMissingBreakStatementInspection */
Expand Down Expand Up @@ -686,6 +690,7 @@ protected function parseResponse()
break;

case 'Basho\Riak\Command\Object\FetchPreflist':
case 'Basho\Riak\Command\Object\Keys':
$response = new Command\Object\Response($this->success, $this->statusCode, $this->error, $location, [new Object(json_decode($body))]);
break;

Expand Down
79 changes: 79 additions & 0 deletions src/Riak/Command/Builder/ListObjects.php
@@ -0,0 +1,79 @@
<?php

namespace Basho\Riak\Command\Builder;

use Basho\Riak;
use Basho\Riak\Command;

/**
* Used to list KV objects in Riak
*
* <code>
* $command = (new Command\Builder\ListObjecst($riak))
* ->buildBucket('users', 'default')
* ->build();
*
* $response = $command->execute();
*
* $key = $response->getObject();
* </code>
*
* @author Christopher Mancini <cmancini at basho d0t com>
*/
class ListObjects extends Command\Builder implements Command\BuilderInterface
{
use BucketTrait;
use ObjectTrait;

/**
* @var bool
*/
protected $decodeAsAssociative = false;

public function __construct(Riak $riak)
{
parent::__construct($riak);
}

/**
* {@inheritdoc}
*
* @return Command\Object\Keys
*/
public function build()
{
$this->validate();

return new Command\Object\Keys($this);
}

/**
* Tells the client to decode the data as an associative array instead of a PHP stdClass object.
* Only works if the fetched object type is JSON.
*
* @return $this
*/
public function withDecodeAsAssociative()
{
$this->decodeAsAssociative = true;
return $this;
}

/**
* Fetch the setting for decodeAsAssociative.
*
* @return bool
*/
public function getDecodeAsAssociative()
{
return $this->decodeAsAssociative;
}

/**
* {@inheritdoc}
*/
public function validate()
{
$this->required('Bucket');
}
}
23 changes: 23 additions & 0 deletions src/Riak/Command/Object/Keys.php
@@ -0,0 +1,23 @@
<?php

namespace Basho\Riak\Command\Object;

use Basho\Riak\Command;
use Basho\Riak\CommandInterface;

/**
* Lists Riak Kv Object keys
*
* @author Christopher Mancini <cmancini at basho d0t com>
*/
class Keys extends Command\Object implements CommandInterface
{
public function __construct(Command\Builder\ListObjects $builder)
{
parent::__construct($builder);

$this->parameters['keys'] = 'true';
$this->bucket = $builder->getBucket();
$this->decodeAsAssociative = $builder->getDecodeAsAssociative();
}
}

0 comments on commit 6b5731d

Please sign in to comment.