Skip to content

Add facets + document prefix #1

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion .unused.php
Original file line number Diff line number Diff line change
@@ -20,9 +20,22 @@
* Optional params
**/
'skipPackages' => [
'phpmd/phpmd',// QA tools
'insolita/unused-scanner',// QA tools
'vimeo/psalm',// QA tools
'friendsofphp/php-cs-fixer',// QA tools
'rskuipers/php-assumptions',// QA tools
'phan/phan',// QA tools
'ergebnis/composer-normalize',// QA tools
'enlightn/security-checker',// QA tools
'jakub-onderka/php-parallel-lint',// QA tools
'phpunit/phpunit',// Unit test
'sebastian/phpcpd',// QA tools

'symfony/polyfill-mbstring',// Prevent Symfony Polyfill requiring PHP 8
],
'excludeDirectories' => $excludeDirectories,
'scanFiles' => $scanFiles,
'extensions' => ['*.php'],
'requireDev' => false
'requireDev' => true
];
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Facets
- Document prefix in mappings (JSON/XML/Annotation/Attribute)

## [1.0.0]

First version
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: analyze fix-code
.PHONY: analyze fix-code test

analyze: | vendor
$(COMPOSER) install --optimize-autoloader --no-suggest --prefer-dist
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -235,25 +235,27 @@ The **After** allow you to do more action with results.

### The `Before` group

_In **bold** paramaters that can be changed._
_In **bold** parameters that can be changed._

Event name | `ObjectManager` method | Available parameters
--- | --- | ---
`AddingDocumentToSearchEvent` | `addObjectInSearch` and `addObject` | **`data`**, **`documentId`**, `instance` _(r/o)_
`AddingSuggestionEvent` | `addObjectInSuggestion` and `addObject` | **`group`**, **`suggestion`**, **`score`**, **`increment`**, **`payload`**, `instance` _(r/o)_
`CreatingIndexEvent` | `createIndex` | **`builder`**, `classname` _(r/o)_
`GettingFacetsEvent` | `getFacets` | `classname` _(r/o)_, **`query`**, **`fields`**
`GettingSuggestionsEvent` | `getSuggestions` | `classname` _(r/o)_, **`prefix`**, **`fuzzy`**, **`withScores`**, **`withPayloads`**, **`max`**, **`inGroup`**
`RemovingDocumentEvent` | `removeObjectFromSearch` | `instance` _(r/o)_, **`documentId`**

### The `After` group

_In **bold** paramaters that can be changed._
_In **bold** parameters that can be changed._

Event name | `ObjectManager` method | Available parameters
--- | --- | ---
`AddingDocumentToSearchEvent` | `addObjectInSearch` and `addObject` | `data` _(r/o)_, `documentId` _(r/o)_, `instance` _(r/o)_
`AddingSuggestionEvent` | `addObjectInSuggestion` and `addObject` | `group` _(r/o)_, `suggestion` _(r/o)_, `score` _(r/o)_, `increment` _(r/o)_, `payload` _(r/o)_, `instance` _(r/o)_
`CreatingIndexEvent` | `createIndex` | `succeed` _(r/o)_, `classname` _(r/o)_
`GettingFacetsEvent` | `getFacets` | `classname` _(r/o)_, `query` _(r/o)_, `fields` _(r/o)_, **`facets`**
`GettingSearchBuilderEvent` | `getSearchBuilder` | **`searchBuilder`**, `classname` _(r/o)_
`GettingSuggestionsEvent` | `getSuggestions` | `classname` _(r/o)_, `prefix` _(r/o)_, `fuzzy` _(r/o)_, `withScores` _(r/o)_, `withPayloads` _(r/o)_, `max` _(r/o)_, `inGroup` _(r/o)_, **`suggestions`**
`RemovingDocumentEvent` | `removeObjectFromSearch` | `instance` _(r/o)_, `documentId` _(r/o)_, `succeed` _(r/o)_
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -18,15 +18,14 @@
],
"require": {
"php": "^7.3 || ^8.0",
"macfja/redisearch": "^1.1",
"macfja/redisearch": "^1.3",
"predis/predis": "^1.1",
"psr/event-dispatcher": "^1.0",
"sgh/comparable": "^1.1"
},
"require-dev": {
"api-platform/core": "^2.5",
"doctrine/annotations": "^1.11",
"doctrine/orm": "^2.8",
"enlightn/security-checker": "^1.5",
"ergebnis/composer-normalize": "^2.11",
"friendsofphp/php-cs-fixer": "^2.17",
"insolita/unused-scanner": "^2.2",
@@ -37,7 +36,6 @@
"phpunit/phpunit": "^9.5",
"rskuipers/php-assumptions": "^0.8.0",
"sebastian/phpcpd": "^6.0",
"sensiolabs/security-checker": "^6.0",
"symfony/polyfill-mbstring": "< 1.22.0",
"vimeo/psalm": "^4.3"
},
3 changes: 3 additions & 0 deletions src/Annotation/FieldAnnotation.php
Original file line number Diff line number Diff line change
@@ -24,6 +24,9 @@
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation;
use MacFJA\RediSearch\Index\Builder\Field;

/**
* @phan-suppress PhanDeprecatedInterface
*/
interface FieldAnnotation extends NamedArgumentConstructorAnnotation
{
public function getField(string $name): Field;
12 changes: 11 additions & 1 deletion src/Annotation/Index.php
Original file line number Diff line number Diff line change
@@ -26,19 +26,29 @@
/**
* @Annotation
* @Target({"CLASS"})
* @phan-suppress PhanDeprecatedInterface
*/
class Index implements NamedArgumentConstructorAnnotation
{
/** @var string */
private $name;

public function __construct(string $name)
/** @var null|string */
private $prefix;

public function __construct(string $name, ?string $prefix = null)
{
$this->name = $name;
$this->prefix = $prefix;
}

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

public function getPrefix(): ?string
{
return $this->prefix;
}
}
4 changes: 1 addition & 3 deletions src/Annotation/Suggestion.php
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
/**
* @Annotation
* @Target({"PROPERTY", "METHOD"})
* @phan-suppress PhanDeprecatedInterface
*/
class Suggestion implements NamedArgumentConstructorAnnotation
{
@@ -48,9 +49,6 @@ class Suggestion implements NamedArgumentConstructorAnnotation
/** @var bool */
private $increment;

/**
* Suggestion constructor.
*/
public function __construct(string $group = 'suggestion', float $score = 1.0, string $type = self::TYPE_FULL, bool $increment = false, ?string $payload = null)
{
$this->group = $group;
15 changes: 15 additions & 0 deletions src/Annotation/TemplateAnnotationMapper.php
Original file line number Diff line number Diff line change
@@ -75,6 +75,21 @@ public static function getRSIndexName(): string
return self::getClass();
}

public static function getRSIndexDocumentPrefix(): ?string
{
$reflectionClass = new ReflectionClass(self::getClass());
$reader = new AnnotationReader();
$indexName = $reader->getClassAnnotation(
$reflectionClass,
Index::class
);
if ($indexName instanceof Index) {
return $indexName->getPrefix();
}

return null;
}

public function getRSDataArray($instance): array
{
$classname = self::getClass();
11 changes: 10 additions & 1 deletion src/Attribute/Index.php
Original file line number Diff line number Diff line change
@@ -30,13 +30,22 @@ class Index implements NamedArgumentConstructorAnnotation
/** @var string */
private $name;

public function __construct(string $name)
/** @var null|string */
private $prefix;

public function __construct(string $name, ?string $prefix = null)
{
$this->name = $name;
$this->prefix = $prefix;
}

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

public function getPrefix(): ?string
{
return $this->prefix;
}
}
13 changes: 13 additions & 0 deletions src/Attribute/TemplateAttributeMapper.php
Original file line number Diff line number Diff line change
@@ -74,6 +74,19 @@ public static function getRSIndexName(): string
return self::getClass();
}

public static function getRSIndexDocumentPrefix(): ?string
{
$reflectionClass = new ReflectionClass(self::getClass());
$indexName = $reflectionClass->getAttributes(Index::class);
if (1 === count($indexName)) {
$indexName = reset($indexName);
/** @var Index $indexName */
return $indexName->getPrefix();
}

return null;
}

public function getRSDataArray($instance): array
{
$classname = self::getClass();
85 changes: 85 additions & 0 deletions src/Event/After/GettingFacetsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\Integration\Event\After;

class GettingFacetsEvent
{
/** @var string */
private $classname;

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

/** @var array<string> */
private $fields;

/** @var array<string,array<string,int>> */
private $facets;

/**
* @param array<string> $fields
* @param array<string,array<string,int>> $facets
*/
public function __construct(string $classname, string $query, array $fields, array $facets)
{
$this->classname = $classname;
$this->query = $query;
$this->fields = $fields;
$this->facets = $facets;
}

public function getClassname(): string
{
return $this->classname;
}

public function getQuery(): string
{
return $this->query;
}

/**
* @return array<string>
*/
public function getFields(): array
{
return $this->fields;
}

/**
* @return array<string,array<string,int>>
*/
public function getFacets(): array
{
return $this->facets;
}

/**
* @param array<string,array<string,int>> $facets
*/
public function setFacets(array $facets): GettingFacetsEvent
{
$this->facets = $facets;

return $this;
}
}
82 changes: 82 additions & 0 deletions src/Event/Before/GettingFacetsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

/*
* Copyright MacFJA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

namespace MacFJA\RediSearch\Integration\Event\Before;

use MacFJA\RediSearch\Helper\DataHelper;

class GettingFacetsEvent
{
/** @var string */
private $classname;

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

/** @var array<string> */
private $fields;

/**
* @param array<string> $fields
*/
public function __construct(string $classname, string $query, array $fields)
{
$this->classname = $classname;
$this->query = $query;
$this->fields = $fields;
}

public function getClassname(): string
{
return $this->classname;
}

public function getQuery(): string
{
return $this->query;
}

public function setQuery(string $query): GettingFacetsEvent
{
$this->query = $query;

return $this;
}

/**
* @return array<string>
*/
public function getFields(): array
{
return $this->fields;
}

/**
* @param array<string> $fields
*/
public function setFields(array $fields): GettingFacetsEvent
{
DataHelper::assertArrayOf($fields, 'string');
$this->fields = $fields;

return $this;
}
}
Loading
Oops, something went wrong.