Skip to content

Commit

Permalink
Add hasFields, Keys, Pluck, Values and Without manipulations #52 (#69)
Browse files Browse the repository at this point in the history
* Add some documents manipulation operators

* Row must be nullable to operate on all the document

* WIP HasFields

* Remove unused property

* WIP Tests

* Move GetFieldLogic to Query/Manipulation namespace

* Fix hasFields implementation for Table and Row

* Fix keys implementation manipulation

* Fix pluck implementation manipulation

* Fix values implementation manipulation

* Fix without implementation manipulation

* Remove obsolete nullable type, since if not passed it defaults to null
  • Loading branch information
Th3Mouk authored and tbolier committed Sep 9, 2018
1 parent 8a5d709 commit d3fd031
Show file tree
Hide file tree
Showing 27 changed files with 625 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Query/Aggregation/Max.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace TBolier\RethinkQL\Query\Aggregation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\Logic\LogicTrait;
use TBolier\RethinkQL\Query\Manipulation\LogicTrait;
use TBolier\RethinkQL\Query\Operation\OperationTrait;
use TBolier\RethinkQL\Query\QueryInterface;
use TBolier\RethinkQL\Query\Transformation\TransformationTrait;
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function ordening(string $key): Ordening
return $this->ordering;
}

public function row(string $value): Row
public function row(string $value = null): Row
{
$this->row = new Row($this->rethink, $value);

Expand Down
12 changes: 0 additions & 12 deletions src/Query/Logic/LogicTrait.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace TBolier\RethinkQL\Query\Logic;
namespace TBolier\RethinkQL\Query\Manipulation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\Aggregation\AggregationTrait;
Expand All @@ -10,7 +10,7 @@
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class GetFieldLogic extends AbstractQuery
class GetField extends AbstractQuery
{
use AggregationTrait;
use OperationTrait;
Expand Down
61 changes: 61 additions & 0 deletions src/Query/Manipulation/HasFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query\Manipulation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\Aggregation\AggregationTrait;
use TBolier\RethinkQL\Query\Operation\OperationTrait;
use TBolier\RethinkQL\Query\QueryInterface;
use TBolier\RethinkQL\Query\Transformation\TransformationTrait;
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class HasFields extends AbstractQuery
{
use AggregationTrait;
use OperationTrait;
use TransformationTrait;

/**
* @var array
*/
private $keys;

/**
* @var QueryInterface
*/
private $query;

public function __construct(
RethinkInterface $rethink,
QueryInterface $query,
array $keys
) {
parent::__construct($rethink);

$this->rethink = $rethink;
$this->query = $query;
$this->keys = $keys;
}

public function toArray(): array
{
if (\count($this->keys) === 1) {
$keysQuery = implode($this->keys);
} else {
$keysQuery = [
TermType::MAKE_ARRAY,
array_values($this->keys)
];
}

return [
TermType::HAS_FIELDS,
[
$this->query->toArray(),
$keysQuery
]
];
}
}
43 changes: 43 additions & 0 deletions src/Query/Manipulation/Keys.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query\Manipulation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\Aggregation\AggregationTrait;
use TBolier\RethinkQL\Query\Operation\OperationTrait;
use TBolier\RethinkQL\Query\QueryInterface;
use TBolier\RethinkQL\Query\Transformation\TransformationTrait;
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class Keys extends AbstractQuery
{
use AggregationTrait;
use TransformationTrait;

/**
* @var QueryInterface
*/
private $query;

public function __construct(
RethinkInterface $rethink,
QueryInterface $query
) {
parent::__construct($rethink);

$this->query = $query;
$this->rethink = $rethink;
}

public function toArray(): array
{
return [
TermType::KEYS,
[
$this->query->toArray()
]
];
}
}
12 changes: 12 additions & 0 deletions src/Query/Manipulation/LogicTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace TBolier\RethinkQL\Query\Manipulation;

trait LogicTrait
{
public function getField(string $field): GetField
{
return new GetField($this->rethink, $field, $this->query);
}
}
29 changes: 29 additions & 0 deletions src/Query/Manipulation/ManipulationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace TBolier\RethinkQL\Query\Manipulation;

use TBolier\RethinkQL\Query\QueryInterface;

trait ManipulationTrait
{
public function pluck(...$keys): Pluck
{
return new Pluck($this->rethink, /** @scrutinizer ignore-type */ $this, $keys);
}

public function without(...$keys): Without
{
return new Without($this->rethink, /** @scrutinizer ignore-type */ $this, $keys);
}

public function keys(): Keys
{
return new Keys($this->rethink, /** @scrutinizer ignore-type */ $this);
}

public function values(): Values
{
return new Values($this->rethink, /** @scrutinizer ignore-type */ $this);
}
}
61 changes: 61 additions & 0 deletions src/Query/Manipulation/Pluck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query\Manipulation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\Aggregation\AggregationTrait;
use TBolier\RethinkQL\Query\Operation\OperationTrait;
use TBolier\RethinkQL\Query\QueryInterface;
use TBolier\RethinkQL\Query\Transformation\TransformationTrait;
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class Pluck extends AbstractQuery
{
use AggregationTrait;
use OperationTrait;
use TransformationTrait;

/**
* @var array
*/
private $keys;

/**
* @var QueryInterface
*/
private $query;

public function __construct(
RethinkInterface $rethink,
QueryInterface $query,
array $keys
) {
parent::__construct($rethink);

$this->query = $query;
$this->rethink = $rethink;
$this->keys = $keys;
}

public function toArray(): array
{
if (\count($this->keys) === 1) {
$keysQuery = implode($this->keys);
} else {
$keysQuery = [
TermType::MAKE_ARRAY,
array_values($this->keys)
];
}

return [
TermType::PLUCK,
[
$this->query->toArray(),
$keysQuery
]
];
}
}
52 changes: 52 additions & 0 deletions src/Query/Manipulation/RowHasFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query\Manipulation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\Aggregation\AggregationTrait;
use TBolier\RethinkQL\Query\Operation\OperationTrait;
use TBolier\RethinkQL\Query\QueryInterface;
use TBolier\RethinkQL\Query\Transformation\TransformationTrait;
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class RowHasFields extends AbstractQuery
{
/**
* @var array
*/
private $keys;

public function __construct(
RethinkInterface $rethink,
array $keys
) {
parent::__construct($rethink);

$this->keys = $keys;
$this->rethink = $rethink;
}

public function toArray(): array
{
if (\count($this->keys) === 1) {
$keysQuery = implode($this->keys);
} else {
$keysQuery = [
TermType::MAKE_ARRAY,
array_values($this->keys)
];
}

return [
TermType::HAS_FIELDS,
[
[
TermType::IMPLICIT_VAR
],
$keysQuery
]
];
}
}
49 changes: 49 additions & 0 deletions src/Query/Manipulation/Values.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
declare(strict_types = 1);

namespace TBolier\RethinkQL\Query\Manipulation;

use TBolier\RethinkQL\Query\AbstractQuery;
use TBolier\RethinkQL\Query\Aggregation\AggregationTrait;
use TBolier\RethinkQL\Query\Operation\OperationTrait;
use TBolier\RethinkQL\Query\QueryInterface;
use TBolier\RethinkQL\Query\Transformation\TransformationTrait;
use TBolier\RethinkQL\RethinkInterface;
use TBolier\RethinkQL\Types\Term\TermType;

class Values extends AbstractQuery
{
use AggregationTrait;
use OperationTrait;
use TransformationTrait;

/**
* @var array
*/
private $keys;

/**
* @var QueryInterface
*/
private $query;

public function __construct(
RethinkInterface $rethink,
QueryInterface $query
) {
parent::__construct($rethink);

$this->query = $query;
$this->rethink = $rethink;
}

public function toArray(): array
{
return [
TermType::VALUES,
[
$this->query->toArray()
],
];
}
}
Loading

0 comments on commit d3fd031

Please sign in to comment.