Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Showing
with 349 additions and 77 deletions
<?php
namespace IQDEV\ElasticSearch\Document\Property;
enum PropertyType
{
case BASE;
case KEYWORD;
case NUMBER;
case TEXT;
}
<?php <?php
namespace IQDEV\ElasticSearch\Facet; namespace IQDEV\ElasticSearch\Facet\Collection;
use IQDEV\ElasticSearch\Esable; use IQDEV\ElasticSearch\Esable;
use IQDEV\ElasticSearch\Facet\Facetable;
use Ramsey\Collection\AbstractCollection; use Ramsey\Collection\AbstractCollection;
final class FacetCollection extends AbstractCollection implements Esable final class FacetCollection extends AbstractCollection implements Esable
{ {
public function getType(): string public function getType(): string
{ {
return Facet::class; return Facetable::class;
} }
public function es(): array public function es(): array
{ {
return array_map(static fn(Facet $facet) => $facet->es(), $this->toArray()); return array_map(static fn(Facetable $facet) => $facet->es(), $this->toArray());
} }
} }
<?php
namespace IQDEV\ElasticSearch\Facet\Collection;
use IQDEV\ElasticSearch\Facet\FacetResult;
use Ramsey\Collection\AbstractCollection;
final class FacetResultCollection extends AbstractCollection
{
/**
* @inheritDoc
*/
public function getType(): string
{
return FacetResult::class;
}
}
...@@ -2,8 +2,18 @@ ...@@ -2,8 +2,18 @@
namespace IQDEV\ElasticSearch\Facet; namespace IQDEV\ElasticSearch\Facet;
use IQDEV\ElasticSearch\Esable; use IQDEV\ElasticSearch\Document\Property\Property;
interface Facet extends Esable abstract class Facet implements Facetable
{ {
public function __construct(
protected Property $property,
protected mixed $value,
) {
}
public function getProperty(): Property
{
return $this->property;
}
} }
<?php
namespace IQDEV\ElasticSearch\Facet;
use IQDEV\ElasticSearch\Esable;
final class FacetCategory implements Esable
{
private string $category;
public function __construct(string $category)
{
$this->category = $category;
}
public function es(): array
{
return [
'category_id' => $this->category,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
use IQDEV\ElasticSearch\Document\Property\Property;
use IQDEV\ElasticSearch\Document\Property\PropertyType;
use IQDEV\ElasticSearch\Facet\Type\BaseFacet;
use IQDEV\ElasticSearch\Facet\Type\KeywordFacet;
use IQDEV\ElasticSearch\Facet\Type\NumberFacet;
class FacetFactory
{
public static function createFromProperty(Property $property, mixed $value): Facet
{
match ($property->getType()) {
PropertyType::KEYWORD => $facet = new KeywordFacet($property, $value),
PropertyType::NUMBER => $facet = new NumberFacet($property, $value),
default => $facet = new BaseFacet($property, $value),
};
return $facet;
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
final class FacetKeyword implements Facet
{
private string $key;
/** @var string|string[] */
private $value;
/**
* @param string|string[] $value
*/
public function __construct(string $key, $value)
{
$this->key = $key;
$this->value = $value;
}
public function es(): array
{
return [
'facet_code' => $this->key,
'facet_value' => $this->value,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
final class FacetNumber implements Facet
{
private string $key;
private float $value;
public function __construct(string $key, float $value)
{
$this->key = $key;
$this->value = $value;
}
public function es(): array
{
return [
'facet_code' => $this->key,
'facet_value' => $this->value,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
use IQDEV\ElasticSearch\Facet\Item\FacetItemCollection;
class FacetResult
{
public FacetItemCollection $products;
protected FacetType $type;
protected string $code;
public function __construct(FacetType $type, string $code)
{
$this->type = $type;
$this->code = $code;
$this->products = new FacetItemCollection();
}
public function getType(): FacetType
{
return $this->type;
}
public function getCode(): string
{
return $this->code;
}
}
<?php
namespace IQDEV\ElasticSearch\Facet;
enum FacetType: string
{
case LIST = 'list';
case RANGE = 'range';
}
<?php
namespace IQDEV\ElasticSearch\Facet;
use IQDEV\ElasticSearch\Esable;
interface Facetable extends Esable
{
}
<?php
namespace IQDEV\ElasticSearch\Facet\Item;
abstract class FacetItem
{
protected ?string $label = null;
protected ?string $value = null;
protected bool $selected = false;
protected int $count = 0;
abstract public static function create(): self;
public function getLabel(): ?string
{
return $this->label;
}
public function getValue(): ?string
{
return $this->value;
}
public function getCount(): int
{
return $this->count;
}
public function isActive(): bool
{
return $this->count > 0;
}
public function isSelected(): bool
{
return $this->selected;
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Item;
use Ramsey\Collection\AbstractCollection;
class FacetItemCollection extends AbstractCollection
{
public function getType(): string
{
return FacetItem::class;
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Item;
class FacetItemList extends FacetItem
{
public static function create(
?string $value = null,
int $count = 0,
bool $selected = false,
?string $label = null
): self
{
$instance = new self();
$instance->value = $value;
$instance->label = $label;
$instance->count = $count;
$instance->selected = $selected;
return $instance;
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Item;
class FacetItemRange extends FacetItem
{
protected FacetItemRangeDTO $data;
protected ?FacetItemRangeDTO $selectedData;
public static function create(
FacetItemRangeDTO $data = null,
FacetItemRangeDTO $selectedData = null,
int $count = 0,
bool $selected = false,
?string $label = null
): self
{
$instance = new self();
$instance->label = $label;
$instance->count = $count;
$instance->selected = $selected;
$instance->data = $data;
$instance->selectedData = $selectedData;
return $instance;
}
public function getFullRange(): array
{
return [
'min' => $this->data->min,
'max' => $this->data->max
];
}
public function getSelectedRange(): array
{
return [
'min' => $this->selectedData->min,
'max' => $this->selectedData->max
];
}
public function getData(): FacetItemRangeDTO
{
return $this->data;
}
public function getSelectedData(): FacetItemRangeDTO
{
return $this->selectedData;
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Item;
class FacetItemRangeDTO
{
public ?float $min = null;
public ?float $max = null;
public ?float $avg = null;
public ?float $sum = null;
public static function create(?float $min = null, ?float $max = null, ?float $avg = null, ?float $sum = null): self
{
$instance = new self();
$instance->min = $min;
$instance->max = $max;
$instance->avg = $avg;
$instance->sum = $sum;
return $instance;
}
}
\ No newline at end of file
<?php
namespace IQDEV\ElasticSearch\Facet\Item;
class FacetItemSingle extends FacetItem
{
public static function create(?string $value = null, int $count = 0, ?string $label = null): self
{
$instance = new self();
$instance->value = $value;
$instance->count = $count;
$instance->label = $label;
return $instance;
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
use IQDEV\ElasticSearch\Facet\Facet;
final class BaseFacet extends Facet
{
/**
* @inheritDoc
*/
public function es(): array
{
return [
$this->property->getKey() => $this->value,
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
use IQDEV\ElasticSearch\Facet\Facet;
final class KeywordFacet extends Facet
{
/**
* @inheritDoc
*/
public function es(): array
{
$value = is_array($this->value) ?
array_map(static fn($value) => (string) $value, $this->value) :
(string) $this->value;
return [
'facet_code' => $this->property->getKey(),
'facet_value' => $value
];
}
}
<?php
namespace IQDEV\ElasticSearch\Facet\Type;
use IQDEV\ElasticSearch\Facet\Facet;
final class NumberFacet extends Facet
{
/**
* @inheritDoc
*/
public function es(): array
{
return [
'facet_code' => $this->property->getKey(),
'facet_value' => (float) $this->value,
];
}
}