Author.php
2 days ago
CommentLink.php
2 days ago
CommentObject.php
2 days ago
CommentText.php
2 days ago
Content.php
2 days ago
LinkableCommentDate.php
2 days ago
Meta.php
2 days ago
MetaDateAndAuthor.php
2 days ago
ParentId.php
2 days ago
Property.php
2 days ago
ReplyToLink.php
2 days ago
StatusLabel.php
2 days ago
UserId.php
2 days ago
Property.php
41 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter\Comment; |
| 6 | |
| 7 | use AC\Exception\ValueNotFoundException; |
| 8 | use AC\Formatter; |
| 9 | use AC\Type\Value; |
| 10 | |
| 11 | class Property implements Formatter |
| 12 | { |
| 13 | private string $property; |
| 14 | |
| 15 | public function __construct(string $property) |
| 16 | { |
| 17 | $this->property = $property; |
| 18 | } |
| 19 | |
| 20 | public function format(Value $value): Value |
| 21 | { |
| 22 | $comment = get_comment($value->get_id()); |
| 23 | $field = $comment->{$this->property} ?? null; |
| 24 | |
| 25 | if (null === $field) { |
| 26 | throw ValueNotFoundException::from_id($value->get_id()); |
| 27 | } |
| 28 | |
| 29 | if (in_array($this->property, ['comment_post_ID', 'comment_parent', 'user_id'], true)) { |
| 30 | if ($field === 0) { |
| 31 | throw ValueNotFoundException::from_id($value->get_id()); |
| 32 | } |
| 33 | |
| 34 | return new Value((int)$field); |
| 35 | } |
| 36 | |
| 37 | return $value->with_value($field); |
| 38 | } |
| 39 | |
| 40 | } |
| 41 |