Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behigh dev #19

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@
$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->notPath('vendor')
->name('*.php')
;
->name('*.php');

return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'braces' => [
'position_after_functions_and_oop_constructs' => 'same',
],
'single_import_per_statement' => false,
'ordered_imports' => true,
])
$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR2' => true,
'braces' => [
'position_after_functions_and_oop_constructs' => 'same',
],
'single_import_per_statement' => false,
'ordered_imports' => true,
])
->setFinder($finder)
->setUsingCache(true)
;
->setUsingCache(true);
30 changes: 25 additions & 5 deletions src/Reindexer/Entities/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Index extends Entity {
private $isAppendable = false;
private $collateMode = 'none';
private $sortOrderLetters;
private $expireAfter = 0;

protected $mapJsonFields = [
'name' => 'name',
Expand All @@ -25,9 +26,10 @@ class Index extends Entity {
'isAppendable' => 'is_appendable',
'collateMode' => 'collate_mode',
'sortOrderLetters' => 'sort_order_letters',
'expireAfter' => 'expire_after'
];

public function getName() {
public function getName(): ?string {
return $this->name;
}

Expand All @@ -37,7 +39,7 @@ public function setName(string $name) {
return $this;
}

public function getJsonPaths(): array {
public function getJsonPaths(): ?array {
return $this->jsonPaths ?? [];
}

Expand All @@ -47,7 +49,7 @@ public function setJsonPaths(array $jsonPaths): self {
return $this;
}

public function getFieldType(): string {
public function getFieldType(): ?string {
return $this->fieldType;
}

Expand Down Expand Up @@ -107,7 +109,7 @@ public function setCollateMode(string $collateMode): self {
return $this;
}

public function getSortOrderLetters(): string {
public function getSortOrderLetters(): ?string {
return $this->sortOrderLetters;
}

Expand All @@ -117,7 +119,7 @@ public function setSortOrderLetters(string $sortOrderLetters): self {
return $this;
}

public function getIndexType(): string {
public function getIndexType(): ?string {
return $this->indexType;
}

Expand All @@ -126,4 +128,22 @@ public function setIndexType(string $indexType): self {

return $this;
}

/**
* @return int
*/
public function getExpireAfter(): int {
return $this->expireAfter;
}

/**
* @param int $expireAfter
*
* @return $this
*/
public function setExpireAfter(int $expireAfter): self {
$this->expireAfter = $expireAfter;

return $this;
}
}
1 change: 1 addition & 0 deletions src/Reindexer/Enum/IndexType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ class IndexType {
const TREE = 'tree';
const TEXT = 'text';
const COLUMN = '-';
const TTL = 'ttl';
}
66 changes: 62 additions & 4 deletions src/Reindexer/Services/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Reindexer\Services;

use Reindexer\BaseService;
use Reindexer\Response;

class Item extends BaseService {
protected $database;
Expand All @@ -24,14 +25,53 @@ public function setNamespace(string $namespace): void {
$this->namespace = $namespace;
}

public function add(array $data = []) {
/**
* @param array $precepts
*
* @return string
*/
protected function preparePercepts(array $precepts): string {
$result = [];
foreach ($precepts as $k => $v) {
$result[] = 'precepts=' . $this->urlencode("{$k}={$v}");
}

return implode('&', $result);
}

/**
* @param string $str
*
* @return string
* @todo Move to separate helper class
*/
public function urlencode(string $str): string {
return str_ireplace(
['%3B', '%3A', '%40', '%24', '%21', '%2A', '%28', '%29', '%2C', '%2F'],
[';', ':', '@', '$', '!', '*', '(', ')', ',', '/'],
\urlencode($str)
);
}

/**
* This method will INSERT documents to namespace, by their primary keys.
* @param array $data Single document or Array of documents
* @param array $precepts Example: ['id' => 'serial()']
*
* @return Response
*/
public function add(array $data = [], array $precepts = []): Response {
$uri = sprintf(
'/api/%s/db/%s/namespaces/%s/items',
$this->version,
$this->getDatabase(),
$this->getNamespace()
);

if ($precepts) {
$uri .= '?' . $this->preparePercepts($precepts);
}

return $this->client->request(
'POST',
$uri,
Expand All @@ -40,7 +80,7 @@ public function add(array $data = []) {
);
}

public function update(array $data = []) {
public function update(array $data = []): Response {
$uri = sprintf(
'/api/%s/db/%s/namespaces/%s/items',
$this->version,
Expand All @@ -56,7 +96,7 @@ public function update(array $data = []) {
);
}

public function delete(array $data = []) {
public function delete(array $data = []): Response {
$uri = sprintf(
'/api/%s/db/%s/namespaces/%s/items',
$this->version,
Expand All @@ -72,7 +112,17 @@ public function delete(array $data = []) {
);
}

public function get(int $limit = 0, int $offset = 0, string $sortField = '', string $sortOrder = '') {
/**
* @param int $limit Maximum count of returned items
* @param int $offset Offset of first returned item
* @param string $sortField Sort Field
* @param string $sortOrder Sort Order
* @param string $filter Filter with SQL syntax, e.g: field1 = 'v1' AND field2 > 'v2'
* @param array $fields List of returned fields
*
* @return Response
*/
public function get(int $limit = 0, int $offset = 0, string $sortField = '', string $sortOrder = '', string $filter = '', array $fields = []): Response {
$uri = sprintf(
'/api/%s/db/%s/namespaces/%s/items',
$this->version,
Expand All @@ -97,10 +147,18 @@ public function get(int $limit = 0, int $offset = 0, string $sortField = '', str
$params['sort_order'] = $sortOrder;
}

if (!empty($fields)) {
$params['fields'] = implode(',', $fields);
}

if ($params) {
$uri .= '?' . http_build_query($params);
}

if (!empty($filter)) {
$uri .= ($params ? '&' : '?') . 'filter=' . $this->urlencode($filter);
}

return $this->client->request(
'GET',
$uri,
Expand Down