1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\PhpDocParser\Ast\Type;
4:
5: use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
6: use PHPStan\PhpDocParser\Ast\Node;
7: use PHPStan\PhpDocParser\Ast\NodeAttributes;
8: use function sprintf;
9:
10: class ObjectShapeItemNode implements Node
11: {
12:
13: use NodeAttributes;
14:
15: /** @var ConstExprStringNode|IdentifierTypeNode */
16: public $keyName;
17:
18: public bool $optional;
19:
20: public TypeNode $valueType;
21:
22: /**
23: * @param ConstExprStringNode|IdentifierTypeNode $keyName
24: */
25: public function __construct($keyName, bool $optional, TypeNode $valueType)
26: {
27: $this->keyName = $keyName;
28: $this->optional = $optional;
29: $this->valueType = $valueType;
30: }
31:
32: public function __toString(): string
33: {
34: if ($this->keyName !== null) {
35: return sprintf(
36: '%s%s: %s',
37: (string) $this->keyName,
38: $this->optional ? '?' : '',
39: (string) $this->valueType,
40: );
41: }
42:
43: return (string) $this->valueType;
44: }
45:
46: /**
47: * @param array<string, mixed> $properties
48: */
49: public static function __set_state(array $properties): self
50: {
51: $instance = new self($properties['keyName'], $properties['optional'], $properties['valueType']);
52: if (isset($properties['attributes'])) {
53: foreach ($properties['attributes'] as $key => $value) {
54: $instance->setAttribute($key, $value);
55: }
56: }
57: return $instance;
58: }
59:
60: }
61: