Skip to content

Commit

Permalink
Added image search criterions
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Oct 12, 2023
1 parent 0ef85fa commit a0f3e1a
Show file tree
Hide file tree
Showing 7 changed files with 551 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/contracts/Repository/Values/Content/Query/Criterion/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image\FileSize;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image\Height;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image\MimeType;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image\Orientation;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image\Width;

/**
* @phpstan-type Range array{
* min?: int|null,
* max?: int|null,
* }
*
* @phpstan-type ImageCriteria array{
* mimeTypes?: string|array<string>,
* size?: Range,
* width?: Range,
* height?: Range,
* orientation?: string|array<string>,
* }
*/
final class Image extends CompositeCriterion
{
/**
* @phpstan-param ImageCriteria $data
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidCriterionArgumentException
*/
public function __construct(
string $fieldDefIdentifier,
array $data
) {
$criteria = new Criterion\LogicalAnd(
$this->buildCriteria($fieldDefIdentifier, $data)
);

parent::__construct($criteria);
}

/**
* @phpstan-param ImageCriteria $data
*
* @return array<\Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion>
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
private function buildCriteria(
string $fieldDefIdentifier,
array $data
): array {
$criteria = [];

if (isset($data['mimeTypes'])) {
$criteria[] = new MimeType(
$fieldDefIdentifier,
$data['mimeTypes']
);
}

if (isset($data['size'])) {
$criteria[] = new FileSize(
$fieldDefIdentifier,
$data['size']['min'] ?? 0,
$data['size']['max'] ?? null,
);
}

if (isset($data['width'])) {
$criteria[] = new Width(
$fieldDefIdentifier,
$data['width']['min'] ?? 0,
$data['width']['max'] ?? null,
);
}

if (isset($data['height'])) {
$criteria[] = new Height(
$fieldDefIdentifier,
$data['height']['min'] ?? 0,
$data['height']['max'] ?? null,
);
}

if (isset($data['orientation'])) {
$criteria[] = new Orientation(
$fieldDefIdentifier,
$data['orientation'],
);
}

return $criteria;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator\Specifications;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;

final class FileSize extends Criterion
{
private string $fieldDefIdentifier;

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
public function __construct(
string $fieldDefIdentifier,
int $minFileSize = 0,
?int $maxFileSize = null
) {
$this->validate($minFileSize, $maxFileSize);

$value['minFileSize'] = $minFileSize * 1024 * 1024;
$operator = Operator::GTE;

if ($maxFileSize >= 1) {
$operator = Operator::BETWEEN;
$value['maxFileSize'] = $maxFileSize * 1024 * 1024;
}

parent::__construct(
null,
$operator,
$value
);

$this->fieldDefIdentifier = $fieldDefIdentifier;
}

public function getSpecifications(): array
{
return [
new Specifications(
Operator::BETWEEN,
Specifications::FORMAT_ARRAY,
Specifications::TYPE_INTEGER
),
new Specifications(
Operator::GTE,
Specifications::FORMAT_ARRAY,
Specifications::TYPE_INTEGER
),
];
}

public function getFieldDefIdentifier(): string
{
return $this->fieldDefIdentifier;
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
private function validate(
int $minFileSize,
?int $maxFileSize
): void {
if ($minFileSize < 0) {
throw new InvalidArgumentException(
'$minFileSize',
'Value should be grater or equal 0'
);
}

if (
null !== $maxFileSize
&& $maxFileSize < 1
) {
throw new InvalidArgumentException(
'$maxFileSize',
'Value should be grater or equal 1'
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator\Specifications;
use Ibexa\Core\Base\Exceptions\InvalidArgumentException;

final class Height extends Criterion
{
private string $fieldDefIdentifier;

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
public function __construct(
string $fieldDefIdentifier,
int $minHeight = 0,
?int $maxHeight = null
) {
$this->validate($minHeight, $maxHeight);

$value['minHeight'] = $minHeight;
$operator = Operator::GTE;

if ($maxHeight >= 1) {
$operator = Operator::BETWEEN;
$value['maxHeight'] = $maxHeight;
}

parent::__construct(
null,
$operator,
$value
);

$this->fieldDefIdentifier = $fieldDefIdentifier;
}

public function getSpecifications(): array
{
return [
new Specifications(
Operator::BETWEEN,
Specifications::FORMAT_ARRAY,
Specifications::TYPE_INTEGER
),
new Specifications(
Operator::GTE,
Specifications::FORMAT_ARRAY,
Specifications::TYPE_INTEGER
),
];
}

public function getFieldDefIdentifier(): string
{
return $this->fieldDefIdentifier;
}

/**
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
*/
private function validate(
int $minHeight,
?int $maxHeight
): void {
if ($minHeight < 0) {
throw new InvalidArgumentException(
'$minHeight',
'Value should be grater or equal 0'
);
}

if (
null !== $maxHeight
&& $maxHeight < 1
) {
throw new InvalidArgumentException(
'$maxHeight',
'Value should be grater or equal 1'
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image;

use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator;
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Operator\Specifications;

final class MimeType extends Criterion
{
private string $fieldDefIdentifier;

/**
* @param string|array<string> $format
*/
public function __construct(
string $fieldDefIdentifier,
$format
) {
parent::__construct(null, null, $format);

$this->fieldDefIdentifier = $fieldDefIdentifier;
}

public function getSpecifications(): array
{
return [
new Specifications(
Operator::EQ,
Specifications::FORMAT_SINGLE,
Specifications::TYPE_STRING
),
new Specifications(
Operator::IN,
Specifications::FORMAT_ARRAY,
Specifications::TYPE_STRING
),
];
}

public function getFieldDefIdentifier(): string
{
return $this->fieldDefIdentifier;
}
}
Loading

0 comments on commit a0f3e1a

Please sign in to comment.