-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Dawid Parafiński <dawid.parafinski@ez.no>
- Loading branch information
Showing
24 changed files
with
1,072 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/contracts/Repository/Values/Content/Query/Criterion/Image.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?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-import-type Range from \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image\AbstractImageCompositeCriterion | ||
* | ||
* @phpstan-type ImageCriteria array{ | ||
* mimeTypes?: string|array<string>, | ||
* size?: Range, | ||
* width?: Range, | ||
* height?: Range, | ||
* orientation?: string|array<string>, | ||
* } | ||
* | ||
* @template-extends \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion\Image\AbstractImageCompositeCriterion<ImageCriteria> | ||
*/ | ||
final class Image extends Criterion\Image\AbstractImageCompositeCriterion | ||
{ | ||
public const IMAGE_SEARCH_CRITERIA = [ | ||
'mimeTypes', | ||
'size', | ||
'width', | ||
'height', | ||
'orientation', | ||
]; | ||
|
||
protected function getSupportedCriteria(): array | ||
{ | ||
return self::IMAGE_SEARCH_CRITERIA; | ||
} | ||
|
||
/** | ||
* @phpstan-param ImageCriteria $data | ||
* | ||
* @return array<\Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion> | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
protected function buildCriteria( | ||
string $fieldDefIdentifier, | ||
array $data | ||
): array { | ||
$criteria = []; | ||
|
||
if (isset($data['mimeTypes'])) { | ||
$criteria[] = new MimeType( | ||
$fieldDefIdentifier, | ||
$data['mimeTypes'] | ||
); | ||
} | ||
|
||
if (isset($data['size'])) { | ||
$size = $data['size']; | ||
$criteria[] = new FileSize( | ||
$fieldDefIdentifier, | ||
$this->getMinValue($size), | ||
$this->getMaxValue($size), | ||
); | ||
} | ||
|
||
if (isset($data['width'])) { | ||
$width = $data['width']; | ||
$criteria[] = new Width( | ||
$fieldDefIdentifier, | ||
$this->getMinValue($width), | ||
$this->getMaxValue($width) | ||
); | ||
} | ||
|
||
if (isset($data['height'])) { | ||
$height = $data['height']; | ||
$criteria[] = new Height( | ||
$fieldDefIdentifier, | ||
$this->getMinValue($height), | ||
$this->getMaxValue($height) | ||
); | ||
} | ||
|
||
if (isset($data['orientation'])) { | ||
$criteria[] = new Orientation( | ||
$fieldDefIdentifier, | ||
$data['orientation'] | ||
); | ||
} | ||
|
||
return $criteria; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
...racts/Repository/Values/Content/Query/Criterion/Image/AbstractImageCompositeCriterion.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?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\CompositeCriterion; | ||
use Ibexa\Core\Base\Exceptions\InvalidArgumentException; | ||
|
||
/** | ||
* @template TImageCriteria of array | ||
* | ||
* @phpstan-type Range array{ | ||
* min?: int|null, | ||
* max?: int|null, | ||
* } | ||
*/ | ||
abstract class AbstractImageCompositeCriterion extends CompositeCriterion | ||
{ | ||
/** | ||
* @phpstan-param TImageCriteria $data | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
public function __construct( | ||
string $fieldDefIdentifier, | ||
array $data | ||
) { | ||
$this->validate($data, $this->getSupportedCriteria()); | ||
|
||
$criteria = new Criterion\LogicalAnd( | ||
$this->buildCriteria($fieldDefIdentifier, $data) | ||
); | ||
|
||
parent::__construct($criteria); | ||
} | ||
|
||
/** | ||
* @phpstan-param TImageCriteria $data | ||
* | ||
* @return array<\Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion> | ||
*/ | ||
abstract protected function buildCriteria(string $fieldDefIdentifier, array $data): array; | ||
|
||
/** | ||
* @return array<string> | ||
*/ | ||
abstract protected function getSupportedCriteria(): array; | ||
|
||
/** | ||
* @phpstan-param TImageCriteria $data | ||
* | ||
* @param array<string> $supportedCriteria | ||
* | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
protected function validate( | ||
array $data, | ||
array $supportedCriteria | ||
): void { | ||
if (empty($data)) { | ||
throw new InvalidArgumentException( | ||
'$data', | ||
sprintf( | ||
'At least one of the supported criteria should be passed: "%s"', | ||
implode(', ', $supportedCriteria) | ||
) | ||
); | ||
} | ||
|
||
$notSupportedCriteria = array_diff( | ||
array_keys($data), | ||
$supportedCriteria | ||
); | ||
|
||
if (!empty($notSupportedCriteria)) { | ||
throw new InvalidArgumentException( | ||
'$data', | ||
sprintf( | ||
'Given criteria are not supported: "%s". Supported image criteria: "%s"', | ||
implode(', ', $notSupportedCriteria), | ||
implode(', ', $supportedCriteria) | ||
) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param array{min?: int|null} $data | ||
*/ | ||
protected function getMinValue(array $data): int | ||
{ | ||
return $data['min'] ?? 0; | ||
} | ||
|
||
/** | ||
* @param array{max?: int|null} $data | ||
*/ | ||
protected function getMaxValue(array $data): ?int | ||
{ | ||
return $data['max'] ?? null; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
...contracts/Repository/Values/Content/Query/Criterion/Image/AbstractImageRangeCriterion.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?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; | ||
|
||
abstract class AbstractImageRangeCriterion extends Criterion | ||
{ | ||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
public function __construct( | ||
string $fieldDefIdentifier, | ||
int $minValue = 0, | ||
?int $maxValue = null | ||
) { | ||
$this->validate($minValue, $maxValue); | ||
|
||
$value[] = $minValue; | ||
$operator = Operator::GTE; | ||
|
||
if ($maxValue >= 1) { | ||
$operator = Operator::BETWEEN; | ||
$value[] = $maxValue; | ||
} | ||
|
||
parent::__construct( | ||
$fieldDefIdentifier, | ||
$operator, | ||
$value | ||
); | ||
} | ||
|
||
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 | ||
), | ||
]; | ||
} | ||
|
||
/** | ||
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException | ||
*/ | ||
protected function validate( | ||
int $minValue, | ||
?int $maxValue | ||
): void { | ||
if ($minValue < 0) { | ||
throw new InvalidArgumentException( | ||
'$minValue', | ||
'Value should be grater or equal 0' | ||
); | ||
} | ||
|
||
if ( | ||
null !== $maxValue | ||
&& $maxValue < 1 | ||
) { | ||
throw new InvalidArgumentException( | ||
'$maxValue', | ||
'Value should be grater or equal 1' | ||
); | ||
} | ||
|
||
if ( | ||
null !== $maxValue | ||
&& $minValue > $maxValue | ||
) { | ||
throw new InvalidArgumentException( | ||
'$minValue', | ||
'Value should be grater than' . $maxValue | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.