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 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
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
}
],
"require": {
"php": "^7.0",
"symfony/serializer": "^3.0|^4.0",
"php": "^8.0",
"symfony/serializer": "5.*",
"paragonie/random_compat": "*"
},
"require-dev": {
"elasticsearch/elasticsearch": "^7.0",
"phpunit/phpunit": "~6.0",
"squizlabs/php_codesniffer": "^3.0"
"phpunit/phpunit": "~9.0",
"squizlabs/php_codesniffer": "^3.0",
"friendsofphp/php-cs-fixer": "2.18.x-dev"
},
"suggest": {
"elasticsearch/elasticsearch": "This library is for elasticsearch/elasticsearch client to enhance it with DSL functionality."
Expand Down
92 changes: 21 additions & 71 deletions src/Aggregation/AbstractAggregation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,85 +12,54 @@
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;
private ?string $field = null;

/**
* @var BuilderBag
*/
private $aggregations;
private ?BuilderBag $aggregations = null;

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

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

/**
* Inner aggregations container init.
*
* @param string $name
*/
public function __construct($name)
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;
}

Expand All @@ -99,34 +68,25 @@ public function addAggregation(AbstractAggregation $abstractAggregation)
*
* @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 +104,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 +115,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(
string $name,
string $field,
?int $buckets = null,
?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
43 changes: 9 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,29 @@ class ChildrenAggregation extends AbstractAggregation
{
use BucketingTrait;

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

/**
* Return children.
*
* @return string
*/
public function getChildren()
public function __construct(private string $name, private ?string $children = null)
{
return $this->children;
parent::__construct($name);
}

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

$this->setChildren($children);
return $this->children;
}

/**
* @param string $children
*
* @return $this
*/
public function setChildren($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