for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace Knp\DictionaryBundle\ValueTransformer;
use Knp\DictionaryBundle\ValueTransformer;
final class Constant implements ValueTransformer
{
private const PATTERN = '/^(.*)::(.*)$/';
public function supports(mixed $value): bool
if (!\is_string($value)) {
return false;
}
$matches = [];
$matches
if (null === $matches = $this->extract($value)) {
[$class, $constant] = $matches;
$constants = (new \ReflectionClass($class))->getConstants();
return \array_key_exists($constant, $constants);
public function transform(mixed $value): mixed
throw new \Exception(\sprintf('Unable to resolve constant %s.', $value));
return (new \ReflectionClass($class))->getConstant($constant);
/**
* @return ?array{class-string, string}
?array{class-string, string}
2
*/
private function extract(string $value): ?array
if (preg_match(self::PATTERN, $value, $matches)) {
[, $class, $constant] = $matches;
return class_exists($class) || interface_exists($class)
? [$class, $constant]
: null;
return null;