| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\TypeInfo\Type; |
| 13 |
|
| 14 |
use Symfony\Component\TypeInfo\Type; |
| 15 |
use Symfony\Component\TypeInfo\TypeIdentifier; |
| 16 |
|
| 17 |
/** |
| 18 |
* @author Mathias Arlaud <[email protected]> |
| 19 |
* @author Baptiste Leduc <[email protected]> |
| 20 |
* |
| 21 |
* @template T of class-string |
| 22 |
*/ |
| 23 |
class ObjectType extends Type |
| 24 |
{ |
| 25 |
/** |
| 26 |
* @param T $className |
| 27 |
*/ |
| 28 |
public function __construct( |
| 29 |
private readonly string $className, |
| 30 |
) { |
| 31 |
} |
| 32 |
|
| 33 |
public function getTypeIdentifier(): TypeIdentifier |
| 34 |
{ |
| 35 |
return TypeIdentifier::OBJECT; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @return T |
| 40 |
*/ |
| 41 |
public function getClassName(): string |
| 42 |
{ |
| 43 |
return $this->className; |
| 44 |
} |
| 45 |
|
| 46 |
public function isIdentifiedBy(TypeIdentifier|string ...$identifiers): bool |
| 47 |
{ |
| 48 |
foreach ($identifiers as $identifier) { |
| 49 |
if ($identifier instanceof TypeIdentifier) { |
| 50 |
if (TypeIdentifier::OBJECT === $identifier) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
|
| 54 |
continue; |
| 55 |
} |
| 56 |
|
| 57 |
if (TypeIdentifier::OBJECT->value === $identifier) { |
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
if (is_a($this->className, $identifier, allow_string: true)) { |
| 62 |
return true; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return false; |
| 67 |
} |
| 68 |
|
| 69 |
public function accepts(mixed $value): bool |
| 70 |
{ |
| 71 |
return $value instanceof $this->className; |
| 72 |
} |
| 73 |
|
| 74 |
public function __toString(): string |
| 75 |
{ |
| 76 |
return $this->className; |
| 77 |
} |
| 78 |
} |