Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: apisearch-io/php-client
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.2.11
Choose a base ref
...
head repository: apisearch-io/php-client
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 10 commits
  • 10 files changed
  • 1 contributor

Commits on Aug 31, 2022

  1. Suggestions can be set

    mmoreram committed Aug 31, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    mmoreram Marc Morera
    Copy the full SHA
    ffc2e30 View commit details
  2. Merge pull request #148 from apisearch-io/feature/result-set-suggestions

    Suggestions can be set
    mmoreram authored Aug 31, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    b391f47 View commit details

Commits on Sep 20, 2022

  1. Added some new features

    - Sort by function accepts params
    - Query and Result have now a context array
    mmoreram committed Sep 20, 2022

    Verified

    This commit was signed with the committer’s verified signature.
    mmoreram Marc Morera
    Copy the full SHA
    94439ff View commit details
  2. Merge pull request #149 from apisearch-io/feature/new-features

    Added some new features
    mmoreram authored Sep 20, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    fe33755 View commit details

Commits on Jan 23, 2023

  1. Verified

    This commit was signed with the committer’s verified signature.
    mmoreram Marc Morera
    Copy the full SHA
    7d2bb96 View commit details
  2. Merge pull request #150 from apisearch-io/feature/item-for-deletion

    Added item for deletion model class
    mmoreram authored Jan 23, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    d59c782 View commit details

Commits on Apr 13, 2023

  1. Verified

    This commit was signed with the committer’s verified signature.
    mmoreram Marc Morera
    Copy the full SHA
    63cf67a View commit details
  2. Merge pull request #151 from apisearch-io/feature/added-set-subresults

    Addd setSubresults method in Result
    mmoreram authored Apr 13, 2023

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    5bb7672 View commit details

Commits on Mar 8, 2024

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    mmoreram Marc Morera
    Copy the full SHA
    e2d49fc View commit details
  2. Merge pull request #152 from apisearch-io/feature/filter_by_user

    Fields _user.* is treated as final paths
    mmoreram authored Mar 8, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    644f2ae View commit details
Showing with 209 additions and 6 deletions.
  1. +8 −5 Model/Item.php
  2. +29 −0 Model/ItemForDeletion.php
  3. +28 −0 Query/Query.php
  4. +4 −1 Query/SortBy.php
  5. +46 −0 Result/Result.php
  6. +33 −0 Tests/Model/ItemForDeletionTest.php
  7. +1 −0 Tests/Model/ItemTest.php
  8. +13 −0 Tests/Query/QueryTest.php
  9. +16 −0 Tests/Query/SortByTest.php
  10. +31 −0 Tests/Result/ResultTest.php
13 changes: 8 additions & 5 deletions Model/Item.php
Original file line number Diff line number Diff line change
@@ -159,7 +159,7 @@ public static function create(
array $exactMatchingMetadata = [],
array $suggest = []
) {
return new self(
return new static(
$uuid,
null,
$metadata,
@@ -192,7 +192,7 @@ public static function createLocated(
array $exactMatchingMetadata = [],
array $suggest = []
) {
return new self(
return new static(
$uuid,
$coordinate,
$metadata,
@@ -617,7 +617,7 @@ public static function createFromArray(array $array): self
}

$item = isset($array['coordinate'])
? self::createLocated(
? static::createLocated(
ItemUUID::createFromArray($array['uuid']),
Coordinate::createFromArray($array['coordinate']),
$array['metadata'] ?? [],
@@ -626,7 +626,7 @@ public static function createFromArray(array $array): self
$array['exact_matching_metadata'] ?? [],
$array['suggest'] ?? []
)
: self::create(
: static::create(
ItemUUID::createFromArray($array['uuid']),
$array['metadata'] ?? [],
$array['indexed_metadata'] ?? [],
@@ -671,7 +671,10 @@ public static function createFromArray(array $array): self
*/
public static function getPathByField(string $field)
{
if (0 === strpos($field, 'indexed_metadata.')) {
if (
0 === strpos($field, 'indexed_metadata.') ||
0 === strpos($field, '_user')
) {
return $field;
}

29 changes: 29 additions & 0 deletions Model/ItemForDeletion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Apisearch PHP Client.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Apisearch\Model;

class ItemForDeletion extends Item
{
/**
* @param ItemUUID $uuid
*
* @return Item
*/
public static function createByUUID(ItemUUID $uuid)
{
return static::create($uuid);
}
}
28 changes: 28 additions & 0 deletions Query/Query.php
Original file line number Diff line number Diff line change
@@ -186,6 +186,11 @@ class Query implements HttpTransportable
*/
private $queryOperator = null;

/**
* @var array
*/
private $context = [];

/**
* Construct.
*
@@ -1535,6 +1540,26 @@ public function getQueryOperator(): ?string
return $this->queryOperator ?? self::QUERY_OPERATOR_OR;
}

/**
* @return array
*/
public function getContext(): array
{
return $this->context;
}

/**
* @param array $context
*
* @return Query
*/
public function setContext(array $context): Query
{
$this->context = $context;

return $this;
}

/**
* To array.
*
@@ -1612,6 +1637,7 @@ public function toArray(): array
'query_operator' => self::QUERY_OPERATOR_OR !== $this->queryOperator
? $this->queryOperator
: null,
'context' => $this->context,
], function ($element) {
return
!(
@@ -1695,6 +1721,8 @@ public static function createFromArray(array $array): self
$query->queryOperator = $array['query_operator'];
}

$query->context = $array['context'] ?? [];

return $query;
}
}
5 changes: 4 additions & 1 deletion Query/SortBy.php
Original file line number Diff line number Diff line change
@@ -354,17 +354,20 @@ public function byNestedFieldAndFilter(
*
* @param string $function
* @param string $order
* @param array $params
*
* @return SortBy
*/
public function byFunction(
string $function,
string $order
string $order,
array $params = []
): SortBy {
$this->sortsBy[] = [
'type' => self::TYPE_FUNCTION,
'function' => $function,
'order' => $order,
'params' => $params,
];

return $this;
46 changes: 46 additions & 0 deletions Result/Result.php
Original file line number Diff line number Diff line change
@@ -79,6 +79,11 @@ class Result implements HttpTransportable
*/
private $metadata;

/**
* @var array
*/
private $context = [];

/**
* Result constructor.
*
@@ -323,6 +328,14 @@ public function getSuggestions(): array
return array_values($this->suggestions);
}

/**
* @param array $suggestions
*/
public function setSuggestions(array $suggestions)
{
$this->suggestions = $suggestions;
}

/**
* @param string $autocomplete
*/
@@ -389,6 +402,16 @@ public function getSubresults(): array
return $this->subresults;
}

/**
* @param array $subResults
*
* @return array
*/
public function setSubResults(array $subResults)
{
$this->subresults = $subResults;
}

/**
* Set metadata.
*
@@ -426,6 +449,26 @@ public function getMetadataValue(string $name)
return $this->metadata[$name] ?? null;
}

/**
* @return array
*/
public function getContext(): array
{
return $this->context;
}

/**
* @param array $context
*
* @return Result
*/
public function setContext(array $context): Result
{
$this->context = $context;

return $this;
}

/**
* To array.
*
@@ -451,6 +494,7 @@ public function toArray(): array
return $result->toArray();
}, $this->subresults),
'metadata' => $this->metadata,
'context' => $this->context,
], function ($element) {
return
!(
@@ -493,6 +537,8 @@ public static function createFromArray(array $array): self
}, $array['subresults'] ?? [])
);

$result->context = $array['context'] ?? [];

return $result;
}
}
33 changes: 33 additions & 0 deletions Tests/Model/ItemForDeletionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Apisearch PHP Client.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <yuhu@mmoreram.com>
*/

declare(strict_types=1);

namespace Apisearch\Tests\Model;

use Apisearch\Model\ItemForDeletion;
use Apisearch\Model\ItemUUID;
use PHPUnit\Framework\TestCase;

class ItemForDeletionTest extends TestCase
{
public function testCreation()
{
$item = ItemForDeletion::createByUUID(ItemUUID::createByComposedUUID('1~product'));
$this->assertEquals('1~product', $item->composeUUID());
$this->assertInstanceOf(ItemForDeletion::class, $item);
$item = ItemForDeletion::createFromArray($item->toArray());
$this->assertEquals('1~product', $item->composeUUID());
$this->assertInstanceOf(ItemForDeletion::class, $item);
}
}
1 change: 1 addition & 0 deletions Tests/Model/ItemTest.php
Original file line number Diff line number Diff line change
@@ -545,5 +545,6 @@ public function testPathByField()
$this->assertEquals('indexed_metadata.another_id', Item::getPathByField('another_id'));
$this->assertEquals('indexed_metadata._field', Item::getPathByField('_field'));
$this->assertEquals('indexed_metadata._field', Item::getPathByField('indexed_metadata._field'));
$this->assertEquals('_user.lol', Item::getPathByField('_user.lol'));
}
}
13 changes: 13 additions & 0 deletions Tests/Query/QueryTest.php
Original file line number Diff line number Diff line change
@@ -385,4 +385,17 @@ public function testForceSizeAndPage()
$this->assertEquals(3, $query->getPage());
$this->assertEquals(10, $query->getFrom());
}

public function testContext()
{
$this->assertEmpty(Query::createMatchAll()->getContext());
$this->assertEmpty((Query::createMatchAll())->toArray()['context'] ?? []);
$query = Query::createMatchAll()->setContext(['context1']);
$queryAsArray = [
'context' => ['context1'],
];
$this->assertEquals($queryAsArray, $query->toArray());
$this->assertEquals(['context1'], $query->getContext());
$this->assertEquals($queryAsArray, Query::createFromArray($query->toArray())->toArray());
}
}
16 changes: 16 additions & 0 deletions Tests/Query/SortByTest.php
Original file line number Diff line number Diff line change
@@ -387,4 +387,20 @@ public function testMultiplesSorts()
HttpHelper::emulateHttpTransport($sortBy)
);
}

public function testByFunction()
{
$sortBy = SortBy::create()->byFunction('function 1', 'asc', ['val1', 'val2']);
$sortByAsArray = [
[
'type' => 'function',
'function' => 'function 1',
'order' => 'asc',
'params' => ['val1', 'val2'],
],
];

$this->assertEquals($sortByAsArray, $sortBy->toArray());
$this->assertEquals($sortByAsArray, SortBy::createFromArray($sortByAsArray)->toArray());
}
}
31 changes: 31 additions & 0 deletions Tests/Result/ResultTest.php
Original file line number Diff line number Diff line change
@@ -246,6 +246,19 @@ public function testMultiResult()
$this->assertEquals(3, $subqueries['res1']->getTotalHits());
$this->assertEquals(4, $subqueries['res2']->getTotalHits());
$this->assertEquals(5, $subqueries['res3']->getTotalHits());

$result = Result::createFromArray([]);
$result->setSubResults([
'res1' => Result::create(Query::createMatchAll()->identifyWith('1'), 10, 3, null, [], []),
'res2' => Result::create(Query::createMatchAll()->identifyWith('2'), 10, 4, null, [], []),
'res3' => Result::create(Query::createMatchAll()->identifyWith('3'), 10, 5, null, [], []),
]);

$this->assertCount(3, $result->getSubresults());
$subqueries = HttpHelper::emulateHttpTransport($result)->getSubresults();
$this->assertEquals(3, $subqueries['res1']->getTotalHits());
$this->assertEquals(4, $subqueries['res2']->getTotalHits());
$this->assertEquals(5, $subqueries['res3']->getTotalHits());
}

/**
@@ -275,5 +288,23 @@ public function testSuggest()
'sugg1',
], []
)->toArray()['suggests'][0]);

$result->setSuggestions(['sugg100']);
$this->assertEquals(['sugg100'], $result->getSuggestions());
}

public function testContext()
{
$this->assertEmpty((new Result(Query::createMatchAll(), 0, 0))->getContext());
$this->assertEmpty((new Result(Query::createMatchAll(), 0, 0))->toArray()['context'] ?? []);
$result = (new Result(Query::createMatchAll(), 0, 0))->setContext(['context1']);
$resultAsArray = [
'total_items' => 0,
'total_hits' => 0,
'context' => ['context1'],
];
$this->assertEquals($resultAsArray, $result->toArray());
$this->assertEquals(['context1'], $result->getContext());
$this->assertEquals(['context' => ['context1']], Query::createFromArray($result->toArray())->toArray());
}
}