Skip to content

Commit

Permalink
add SimpleFilterResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed May 20, 2021
1 parent ddf477d commit aa8fc01
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/Resolver/FilterResolvers/SimpleFilterResolver.php
@@ -0,0 +1,65 @@
<?php declare(strict_types = 1);

namespace Contributte\Imagist\Resolver\FilterResolvers;

use Contributte\Imagist\Filter\FilterInterface;
use Contributte\Imagist\Resolver\FilterResolverInterface;
use LogicException;

final class SimpleFilterResolver implements FilterResolverInterface
{

private const MAX_LENGTH = 255;

public function resolve(FilterInterface $filter): string
{
$options = $this->parseOptions($filter->getOptions());
$name = '_' . $filter->getName() . ($options ? '-' . implode('-', $options) : '');
$length = strlen($name);

if ($length > self::MAX_LENGTH) {
throw new LogicException(
sprintf('Maximum length of directory must be equal or less than 255, %d given.', $length)
);
}

return $name;
}

/**
* @param mixed[] $options
* @return mixed[]
*/
private function parseOptions(array $options): array
{
$listKey = 0;
foreach ($options as $key => &$value) {
if ($key !== $listKey) {
throw new LogicException(
sprintf('%s only supports option list, given array.', self::class)
);
}

if (is_bool($value)) {
$value = (string) (int) $value;
} elseif (is_string($value)) {
if (!preg_match('#^[\w]+$#', $value)) {
throw new LogicException(
sprintf('%s only supports string contains a-z, A-Z and _', self::class)
);
}
} elseif (is_int($value)) {
$value = (string) $value;
} else {
throw new LogicException(
sprintf('%s only supports int, string or bool values.', self::class)
);
}

$listKey++;
}

return $options;
}

}

0 comments on commit aa8fc01

Please sign in to comment.