1: <?php declare(strict_types=1);
2:
3: namespace Salient\PHPDoc\Tag;
4:
5: /**
6: * @api
7: */
8: class TemplateTag extends AbstractTag
9: {
10: protected ?string $Default;
11: protected bool $IsCovariant;
12: protected bool $IsContravariant;
13:
14: /**
15: * Creates a new TemplateTag object
16: */
17: public function __construct(
18: string $name,
19: ?string $type = null,
20: ?string $default = null,
21: bool $isCovariant = false,
22: bool $isContravariant = false,
23: ?string $class = null,
24: ?string $member = null,
25: array $aliases = []
26: ) {
27: if ($isCovariant && $isContravariant) {
28: $this->throw('$isCovariant and $isContravariant cannot both be true');
29: }
30: parent::__construct('template', $name, $type, null, $class, $member, $aliases);
31: $this->Default = $this->filterType($default, $aliases);
32: $this->IsCovariant = $isCovariant;
33: $this->IsContravariant = $isContravariant;
34: }
35:
36: /**
37: * @inheritDoc
38: */
39: public function getName(): string
40: {
41: return $this->Name;
42: }
43:
44: /**
45: * @return null
46: */
47: public function getDescription(): ?string
48: {
49: return null;
50: }
51:
52: /**
53: * Get the default value of the template
54: */
55: public function getDefault(): ?string
56: {
57: return $this->Default;
58: }
59:
60: /**
61: * Check if the template is covariant
62: */
63: public function isCovariant(): bool
64: {
65: return $this->IsCovariant;
66: }
67:
68: /**
69: * Check if the template is contravariant
70: */
71: public function isContravariant(): bool
72: {
73: return $this->IsContravariant;
74: }
75:
76: /**
77: * Get an instance with the given name
78: *
79: * @return static
80: */
81: public function withName(string $name)
82: {
83: return $this->with('Name', $this->filterString($name, 'name'));
84: }
85:
86: /**
87: * Get an instance with the given PHPDoc type
88: *
89: * @return static
90: */
91: public function withType(?string $type)
92: {
93: if ($type === null) {
94: return $this->without('Type');
95: }
96: return $this->with('Type', $this->filterType($type));
97: }
98:
99: /**
100: * @inheritDoc
101: */
102: public function withDescription(?string $description)
103: {
104: if ($description !== null) {
105: $this->throw('Invalid description');
106: }
107: return $this;
108: }
109:
110: /**
111: * Get an instance with the given default value
112: *
113: * @return static
114: */
115: public function withDefault(?string $default)
116: {
117: return $this->with('Default', $this->filterType($default));
118: }
119:
120: /**
121: * Get an instance without covariance or contravariance
122: *
123: * @return static
124: */
125: public function withoutVariance()
126: {
127: return $this
128: ->with('IsCovariant', false)
129: ->with('IsContravariant', false);
130: }
131:
132: /**
133: * @inheritDoc
134: */
135: public function __toString(): string
136: {
137: $string = "@{$this->Tag}";
138: if ($this->IsCovariant) {
139: $string .= '-covariant';
140: } elseif ($this->IsContravariant) {
141: $string .= '-contravariant';
142: }
143: $string .= " {$this->Name}";
144: if (isset($this->Type)) {
145: $string .= " of {$this->Type}";
146: }
147: if ($this->Default !== null) {
148: $string .= " = {$this->Default}";
149: }
150: return $string;
151: }
152: }
153: