Skip to content

Commit

Permalink
added items to the before select stage
Browse files Browse the repository at this point in the history
  • Loading branch information
jeyroik committed Aug 26, 2020
1 parent b9d5f92 commit d95b143
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 27 deletions.
21 changes: 8 additions & 13 deletions src/components/operations/jsonrpc/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
namespace extas\components\operations\jsonrpc;

use extas\components\conditions\ConditionParameter;
use extas\components\expands\Box;
use extas\components\expands\Expand;
use extas\components\expands\Expander;
use extas\components\api\jsonrpc\operations\OperationRunner;
use extas\components\expands\ExpandingBox;
use extas\interfaces\extensions\IExtensionJsonRpcIndex;
use extas\interfaces\IItem;
use extas\interfaces\jsonrpc\IRequest;
use extas\interfaces\operations\IJsonRpcOperation;
use extas\interfaces\operations\IOperation;
use extas\interfaces\operations\jsonrpc\IIndex;
use extas\interfaces\stages\IStageJsonRpcBeforeSelect;

Expand All @@ -38,6 +33,7 @@ public function run(): array
$request->getOffset(0),
$this->convertIntoTableSort($request->getSort([]))
);

$items = $this->filter($request->getFilter(), $records);
$items = $this->selectFields($items);
$items = $this->expandItems($items);
Expand Down Expand Up @@ -101,28 +97,27 @@ protected function selectFields(array $items): array
/**
* @var IStageJsonRpcBeforeSelect $plugin
*/
$select = $plugin($select);
$select = $plugin($select, $items);
}

$valid = [];
foreach ($items as $index => $item) {
$this->selectItemFields($item, $index, $select, $items);
$this->selectItemFields($item, $select, $valid);
}

return $items;
return $valid;
}

/**
* @param IItem $item
* @param int $index
* @param array $select
* @param array $items
*/
protected function selectItemFields(IItem $item, int $index, array $select, array &$items): void
protected function selectItemFields(IItem $item, array $select, array &$items): void
{
if (!$item->has(...$select)) {
unset($items[$index]);
if ($item->has(...$select)) {
$items[] = $item->__select($select);
}
$items[$index] = $item->__select($select);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/interfaces/stages/IStageJsonRpcBeforeSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use extas\interfaces\http\IHasJsonRpcRequest;
use extas\interfaces\http\IHasJsonRpcResponse;
use extas\interfaces\IItem;

/**
* Interface IStageJsonRpcBeforeSelect
Expand All @@ -16,7 +17,8 @@ interface IStageJsonRpcBeforeSelect extends IHasJsonRpcRequest, IHasJsonRpcRespo

/**
* @param array $select
* @param IItem[] $items
* @return array
*/
public function __invoke(array $select): array;
public function __invoke(array $select, array $items): array;
}
97 changes: 89 additions & 8 deletions tests/jsonrpc/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
use extas\interfaces\http\IHasHttpIO;
use extas\interfaces\jsonrpc\IRequest;
use extas\interfaces\samples\parameters\ISampleParameter;
use extas\interfaces\stages\IStageJsonRpcBeforeSelect;
use PHPUnit\Framework\TestCase;
use tests\jsonrpc\misc\ExpandDescription;
use tests\jsonrpc\misc\UnpackSelect;

/**
* Class IndexTest
Expand Down Expand Up @@ -56,6 +58,7 @@ protected function setUp(): void
Plugin::FIELD__CLASS => PluginExpand::class,
Plugin::FIELD__STAGE => 'extas.expand.@expand'
]));

$this->createWithSnuffRepo('extensionRepository', new Extension([
Extension::FIELD__CLASS => ExtensionJsonRpcIndex::class,
Extension::FIELD__INTERFACE => IExtensionJsonRpcIndex::class,
Expand Down Expand Up @@ -94,15 +97,9 @@ public function testIndexMethods()

public function testIndexOperation()
{
$this->getMagicClass('snuffRepo')->create(new SnuffItem([
'name' => 'test',
'value' => 'is ok'
]));
$this->getMagicClass('snuffRepo')->create(new SnuffItem([
'name' => 'test_2',
'value' => 'is ok again'
]));
$this->createSnuffItems();
$this->createSnuffPlugin(ExpandDescription::class, ['extas.expand.snuff.item.description']);
$this->createSnuffPlugin(UnpackSelect::class, [IStageJsonRpcBeforeSelect::NAME]);

$operation = new Index([
Index::FIELD__OPERATION => new JsonRpcOperation([
Expand Down Expand Up @@ -152,4 +149,88 @@ public function testIndexOperation()
'Incorrect result: ' . print_r($result, true)
);
}

public function testEmptySelectAndFilter()
{
$this->createSnuffItems();
$this->createSnuffPlugin(ExpandDescription::class, ['extas.expand.snuff.item.description']);

$operation = new Index([
Index::FIELD__OPERATION => new JsonRpcOperation([
JsonRpcOperation::FIELD__PARAMETERS => [
JsonRpcOperation::PARAM__ITEM_REPOSITORY => [
ISampleParameter::FIELD__NAME => JsonRpcOperation::PARAM__ITEM_REPOSITORY,
ISampleParameter::FIELD__VALUE => 'snuffRepo'
],
JsonRpcOperation::PARAM__ITEM_CLASS => [
ISampleParameter::FIELD__NAME => JsonRpcOperation::PARAM__ITEM_CLASS,
ISampleParameter::FIELD__VALUE => SnuffItem::class
],
JsonRpcOperation::PARAM__ITEM_NAME => [
ISampleParameter::FIELD__NAME => JsonRpcOperation::PARAM__ITEM_NAME,
ISampleParameter::FIELD__VALUE => 'snuff.item'
],
JsonRpcOperation::PARAM__METHOD => [
ISampleParameter::FIELD__NAME => JsonRpcOperation::PARAM__METHOD,
ISampleParameter::FIELD__VALUE => 'index'
]
]
])
]);

$result = $operation([
IHasHttpIO::FIELD__PSR_REQUEST => $this->getPsrRequest('.empty'),
IHasHttpIO::FIELD__PSR_RESPONSE => $this->getPsrResponse(),
IHasHttpIO::FIELD__ARGUMENTS => [
'version' => 0,
'expand' => ['snuff.item.description']
],
]);

$this->assertEquals(
[
'items' => [
[
'name' => 'x_it?',
'value' => 'is ok again',
'expand' => ['snuff.item.description'],
'description' => 'long long description'
],
[
'name' => 'test_3',
'expand' => ['snuff.item.description'],
'description' => 'long long description'
],
[
'name' => 'test_2',
'value' => 'is ok again',
'expand' => ['snuff.item.description'],
'description' => 'long long description'
]
],
'total' => 3
],
$result,
'Incorrect result: ' . print_r($result, true)
);
}

protected function createSnuffItems()
{
$this->getMagicClass('snuffRepo')->create(new SnuffItem([
'name' => 'test',
'value' => 'is ok'
]));
$this->getMagicClass('snuffRepo')->create(new SnuffItem([
'name' => 'test_2',
'value' => 'is ok again'
]));
$this->getMagicClass('snuffRepo')->create(new SnuffItem([
'name' => 'test_3'
]));
$this->getMagicClass('snuffRepo')->create(new SnuffItem([
'name' => 'x_it?',
'value' => 'is ok again'
]));
}
}
37 changes: 37 additions & 0 deletions tests/jsonrpc/misc/UnpackSelect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
namespace tests\jsonrpc\misc;

use extas\components\http\THasJsonRpcRequest;
use extas\components\http\THasJsonRpcResponse;
use extas\components\plugins\Plugin;
use extas\interfaces\IItem;
use extas\interfaces\stages\IStageJsonRpcBeforeSelect;

/**
* Class UnpackSelect
*
* @package tests\jsonrpc\misc
* @author jeyroik <jeyroik@gmail.com>
*/
class UnpackSelect extends Plugin implements IStageJsonRpcBeforeSelect
{
use THasJsonRpcRequest;
use THasJsonRpcResponse;

/**
* @param array $select
* @param IItem[] $items
* @return array
*/
public function __invoke(array $select, array $items): array
{
if (in_array('*', $select)) {
$select = [];
foreach ($items as $item) {
$select = array_merge($select, array_diff(array_keys($item->__toArray()), $select));
}
}

return $select;
}
}
12 changes: 12 additions & 0 deletions tests/request.empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "2f5d0719-5b82-4280-9b3b-10f23aff226b",
"method": "snuff.index",
"params": {
"limit": 3,
"offset": 0,
"sort": {
"name": -1
},
"expand": ["snuff.item.description"]
}
}
7 changes: 2 additions & 5 deletions tests/request.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@
"id": "2f5d0719-5b82-4280-9b3b-10f23aff226b",
"method": "snuff.index",
"params": {
"limit": 1,
"limit": 3,
"offset": 0,
"sort": {
"name": -1
},
"select": [
"name",
"value"
],
"select": ["*"],
"expand": ["snuff.item.description"],
"filter": {
"name": {
Expand Down

0 comments on commit d95b143

Please sign in to comment.