Skip to content

Commit 00a9ed4

Browse files
committed
Add json serialization support
1 parent 55c1874 commit 00a9ed4

File tree

7 files changed

+34
-3
lines changed

7 files changed

+34
-3
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"issues": "https://github.com/php-type-language/phpdoc/issues"
1010
},
1111
"require": {
12-
"php": "^8.1"
12+
"php": "^8.1",
13+
"ext-json": "*"
1314
},
1415
"autoload": {
1516
"psr-4": {

src/DocBlock.php

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ final class DocBlock implements
2121
OptionalDescriptionProviderInterface,
2222
TagsProviderInterface,
2323
\IteratorAggregate,
24+
\JsonSerializable,
2425
\ArrayAccess,
2526
\Countable
2627
{
@@ -94,6 +95,14 @@ public function getIterator(): \Traversable
9495
return new \ArrayIterator($this->tags);
9596
}
9697

98+
public function jsonSerialize(): array
99+
{
100+
return [
101+
'description' => $this->description,
102+
'tags' => $this->tags,
103+
];
104+
}
105+
97106
/**
98107
* @return int<0, max>
99108
*/

src/Tag/Description/Description.php

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public static function fromStringableOrNull(string|\Stringable|null $description
3737
return self::fromStringable($description);
3838
}
3939

40+
public function jsonSerialize(): string
41+
{
42+
return $this->value;
43+
}
44+
4045
public function __toString(): string
4146
{
4247
return $this->value;

src/Tag/Description/DescriptionInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Any class that implements this interface is a description object
99
* that can be represented as a raw string scalar value.
1010
*/
11-
interface DescriptionInterface extends \Stringable
11+
interface DescriptionInterface extends \JsonSerializable, \Stringable
1212
{
1313
/**
1414
* Returns a plain string representation of this description.

src/Tag/Description/TaggedDescription.php

+5
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public function offsetUnset(mixed $offset): void
8888
throw new \BadMethodCallException(static::class . ' objects are immutable');
8989
}
9090

91+
public function jsonSerialize(): array
92+
{
93+
return $this->components;
94+
}
95+
9196
public function __toString(): string
9297
{
9398
return \implode('', $this->components);

src/Tag/Tag.php

+8
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ public function getDescription(): ?DescriptionInterface
3131
return $this->description;
3232
}
3333

34+
public function jsonSerialize(): array
35+
{
36+
return \array_filter([
37+
'name' => $this->name,
38+
'description' => $this->description,
39+
], static fn(mixed $value): bool => $value !== null);
40+
}
41+
3442
public function __toString(): string
3543
{
3644
if ($this->description === null) {

src/Tag/TagInterface.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
use TypeLang\PHPDoc\Tag\Description\OptionalDescriptionProviderInterface;
88

9-
interface TagInterface extends OptionalDescriptionProviderInterface, \Stringable
9+
interface TagInterface extends
10+
OptionalDescriptionProviderInterface,
11+
\JsonSerializable,
12+
\Stringable
1013
{
1114
/**
1215
* Returns the non-empty tag name string without the '@' prefix.

0 commit comments

Comments
 (0)