Skip to content
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

implemented requirements of the PHP 8 #348

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
107 changes: 27 additions & 80 deletions src/Aggregation/AbstractAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,124 +9,81 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ONGR\ElasticsearchDSL\Aggregation;

use ONGR\ElasticsearchDSL\BuilderBag;
use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\NameAwareTrait;
use ONGR\ElasticsearchDSL\NamedBuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;
use stdClass;

/**
* AbstractAggregation class.
*/
abstract class AbstractAggregation implements NamedBuilderInterface
{
use ParametersTrait;

use NameAwareTrait;

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

/**
* @var BuilderBag
*/
private $aggregations;

/**
* Abstract supportsNesting method.
*
* @return bool
*/
abstract protected function supportsNesting();

/**
* @return array|\stdClass
*/
abstract protected function getArray();

/**
* Inner aggregations container init.
*
* @param string $name
*/
public function __construct($name)
private ?string $field = null;

private ?BuilderBag $aggregations = null;

abstract protected function supportsNesting(): bool;

abstract protected function getArray(): array | stdClass;

public function __construct(string $name)
{
$this->setName($name);
}

/**
* @param string $field
*
* @return $this
*/
public function setField($field)
public function setField(?string $field): static
{
$this->field = $field;

return $this;
}

/**
* @return string
*/
public function getField()
public function getField(): ?string
{
return $this->field;
}

/**
* Adds a sub-aggregation.
*
* @param AbstractAggregation $abstractAggregation
*
* @return $this
*/
public function addAggregation(AbstractAggregation $abstractAggregation)
public function addAggregation(AbstractAggregation $abstractAggregation): static
{
if (!$this->aggregations) {
$this->aggregations = $this->createBuilderBag();
}

$this->aggregations->add($abstractAggregation);

return $this;
}

/**
* Returns all sub aggregations.
isrenato marked this conversation as resolved.
Show resolved Hide resolved
*
* @return BuilderBag[]|NamedBuilderInterface[]
*/
public function getAggregations()
public function getAggregations(): array
{
if ($this->aggregations) {
return $this->aggregations->all();
} else {
return [];
}

return [];
}

/**
* Returns sub aggregation.
* @param string $name Aggregation name to return.
*
* @return AbstractAggregation|NamedBuilderInterface|null
*/
public function getAggregation($name)
public function getAggregation(string $name): AbstractAggregation|BuilderInterface|null
{
if ($this->aggregations && $this->aggregations->has($name)) {
return $this->aggregations->get($name);
} else {
return null;
}

return null;
}

/**
* {@inheritdoc}
*/
public function toArray()
public function toArray(): array
{
$array = $this->getArray();
$result = [
Expand All @@ -144,12 +101,7 @@ public function toArray()
return $result;
}

/**
* Process all nested aggregations.
*
* @return array
*/
protected function collectNestedAggregations()
protected function collectNestedAggregations(): array
{
$result = [];
/** @var AbstractAggregation $aggregation */
Expand All @@ -160,12 +112,7 @@ protected function collectNestedAggregations()
return $result;
}

/**
* Creates BuilderBag new instance.
*
* @return BuilderBag
*/
private function createBuilderBag()
private function createBuilderBag(): BuilderBag
{
return new BuilderBag();
}
Expand Down
32 changes: 7 additions & 25 deletions src/Aggregation/Bucketing/AdjacencyMatrixAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;

use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
Expand All @@ -26,20 +28,11 @@ class AdjacencyMatrixAggregation extends AbstractAggregation

use BucketingTrait;

/**
* @var BuilderInterface[]
*/
private $filters = [
private array $filters = [
self::FILTERS => []
];

/**
* Inner aggregations container init.
*
* @param string $name
* @param BuilderInterface[] $filters
*/
public function __construct($name, $filters = [])
public function __construct(string $name, array $filters = [])
{
parent::__construct($name);

Expand All @@ -49,32 +42,21 @@ public function __construct($name, $filters = [])
}

/**
* @param string $name
* @param BuilderInterface $filter
*
* @throws \LogicException
*
* @return self
*/
public function addFilter($name, BuilderInterface $filter)
public function addFilter(string $name, BuilderInterface $filter): static
{
$this->filters[self::FILTERS][$name] = $filter->toArray();

return $this;
}

/**
* {@inheritdoc}
*/
public function getArray()
public function getArray(): array
{
return $this->filters;
}

/**
* {@inheritdoc}
*/
public function getType()
public function getType(): string
{
return 'adjacency_matrix';
}
Expand Down
32 changes: 11 additions & 21 deletions src/Aggregation/Bucketing/AutoDateHistogramAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;

use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
Expand All @@ -24,16 +26,12 @@ class AutoDateHistogramAggregation extends AbstractAggregation
{
use BucketingTrait;

/**
* Inner aggregations container init.
*
* @param string $name
* @param string $field
* @param int $buckets
* @param string $format
*/
public function __construct($name, $field, $buckets = null, $format = null)
{
public function __construct(
private string $name,
private string $field,
private ?int $buckets = null,
private ?string $format = null
) {
parent::__construct($name);

$this->setField($field);
Expand All @@ -47,24 +45,16 @@ public function __construct($name, $field, $buckets = null, $format = null)
}
}

/**
* {@inheritdoc}
*/
public function getArray()
public function getArray(): array
{
$data = array_filter(
return array_filter(
[
'field' => $this->getField(),
]
);

return $data;
}

/**
* {@inheritdoc}
*/
public function getType()
public function getType(): string
{
return 'auto_date_histogram';
}
Expand Down
45 changes: 11 additions & 34 deletions src/Aggregation/Bucketing/ChildrenAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;

use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
Expand All @@ -23,56 +25,31 @@ class ChildrenAggregation extends AbstractAggregation
{
use BucketingTrait;

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

/**
* Return children.
*
* @return string
*/
public function getChildren()
{
return $this->children;
}

/**
* @param string $name
* @param string $children
*/
public function __construct($name, $children = null)
public function __construct(private string $name, private ?string $children = null)
{
parent::__construct($name);

$this->setChildren($children);
}

/**
* @param string $children
*
* @return $this
*/
public function setChildren($children)
public function getChildren(): ?string
{
return $this->children;
}

public function setChildren(?string $children): static
{
$this->children = $children;

return $this;
}

/**
* {@inheritdoc}
*/
public function getType()
public function getType(): string
{
return 'children';
}

/**
* {@inheritdoc}
*/
public function getArray()
public function getArray(): array
{
if (count($this->getAggregations()) == 0) {
throw new \LogicException("Children aggregation `{$this->getName()}` has no aggregations added");
Expand Down
Loading