2021-11-20 15:31:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace Rector\PhpAttribute;
|
|
|
|
|
2022-02-05 10:39:52 +00:00
|
|
|
use PhpParser\BuilderHelpers;
|
2021-11-20 15:31:12 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-02-05 10:39:52 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
2021-11-20 15:31:12 +00:00
|
|
|
use Rector\PhpAttribute\Contract\AnnotationToAttributeMapperInterface;
|
2021-12-19 12:21:32 +00:00
|
|
|
use Rector\PhpAttribute\Enum\DocTagNodeState;
|
2021-11-20 15:31:12 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\PhpAttribute\AnnotationToAttributeMapper\AnnotationToAttributeMapperTest
|
|
|
|
*/
|
|
|
|
final class AnnotationToAttributeMapper
|
|
|
|
{
|
|
|
|
/**
|
2021-12-30 16:07:15 +00:00
|
|
|
* @var AnnotationToAttributeMapperInterface[]
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-11-20 15:31:12 +00:00
|
|
|
*/
|
|
|
|
private $annotationToAttributeMappers;
|
|
|
|
/**
|
|
|
|
* @param AnnotationToAttributeMapperInterface[] $annotationToAttributeMappers
|
|
|
|
*/
|
|
|
|
public function __construct(array $annotationToAttributeMappers)
|
|
|
|
{
|
|
|
|
$this->annotationToAttributeMappers = $annotationToAttributeMappers;
|
|
|
|
}
|
|
|
|
/**
|
2022-02-05 10:39:52 +00:00
|
|
|
* @return \PhpParser\Node\Expr|string
|
2021-11-20 15:31:12 +00:00
|
|
|
* @param mixed $value
|
|
|
|
*/
|
|
|
|
public function map($value)
|
|
|
|
{
|
|
|
|
foreach ($this->annotationToAttributeMappers as $annotationToAttributeMapper) {
|
|
|
|
if ($annotationToAttributeMapper->isCandidate($value)) {
|
|
|
|
return $annotationToAttributeMapper->map($value);
|
|
|
|
}
|
|
|
|
}
|
2021-11-20 22:45:17 +00:00
|
|
|
if ($value instanceof \PhpParser\Node\Expr) {
|
|
|
|
return $value;
|
|
|
|
}
|
2022-02-05 10:39:52 +00:00
|
|
|
// remove node, as handled elsewhere
|
|
|
|
if ($value instanceof \Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode) {
|
|
|
|
return \Rector\PhpAttribute\Enum\DocTagNodeState::REMOVE_ARRAY;
|
|
|
|
}
|
|
|
|
// fallback
|
|
|
|
return \PhpParser\BuilderHelpers::normalizeValue($value);
|
2021-11-20 15:31:12 +00:00
|
|
|
}
|
|
|
|
}
|