IndexedCommentsBlogPost.php 1.4 KB

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