1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Tests\Fixtures;
6
7use JMS\Serializer\Annotation\Accessor;
8use JMS\Serializer\Annotation\XmlAttribute;
9use JMS\Serializer\Annotation\XmlList;
10use JMS\Serializer\Annotation\XmlMap;
11use JMS\Serializer\Annotation\XmlRoot;
12
13/** @XmlRoot("post") */
14class IndexedCommentsBlogPost
15{
16    /**
17     * @XmlMap(keyAttribute="author-name", inline=true, entry="comments")
18     * @Accessor(getter="getCommentsIndexedByAuthor")
19     */
20    private $comments = [];
21
22    public function __construct()
23    {
24        $author = new Author('Foo');
25        $this->comments[] = new Comment($author, 'foo');
26        $this->comments[] = new Comment($author, 'bar');
27    }
28
29    public function getCommentsIndexedByAuthor()
30    {
31        $indexedComments = [];
32        foreach ($this->comments as $comment) {
33            $authorName = $comment->getAuthor()->getName();
34
35            if (!isset($indexedComments[$authorName])) {
36                $indexedComments[$authorName] = new IndexedCommentsList();
37            }
38
39            $indexedComments[$authorName]->addComment($comment);
40        }
41
42        return $indexedComments;
43    }
44}
45
46class IndexedCommentsList
47{
48    /** @XmlList(inline=true, entry="comment") */
49    private $comments = [];
50
51    /** @XmlAttribute */
52    private $count = 0;
53
54    public function addComment(Comment $comment)
55    {
56        $this->comments[] = $comment;
57        $this->count += 1;
58    }
59}
60