src/Document/Inventory.php line 107

Open in your IDE?
  1. <?php
  2. namespace App\Document;
  3. use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
  4. use JsonSerializable;
  5. /**
  6. * @MongoDB\Document(repositoryClass="App\Repository\InventoryRepository", collection="inventory")
  7. * @MongoDB\UniqueIndex(keys={"gitId"="asc"})
  8. */
  9. class Inventory implements JsonSerializable, SyncDocument
  10. {
  11. const GIT_FIELDS = ['list'];
  12. const GIT_ID = 'gitId';
  13. /**
  14. * @MongoDB\Id
  15. */
  16. protected $_id;
  17. /**
  18. * @var string
  19. * @MongoDB\Field(type="string")
  20. */
  21. protected $gitId;
  22. /**
  23. * @var array
  24. * @MongoDB\Field(type="collection")
  25. */
  26. private $placements = [];
  27. /**
  28. * @var int
  29. * @MongoDB\Field(type="int")
  30. */
  31. protected $placementCount;
  32. /**
  33. * @return mixed
  34. */
  35. public function getId()
  36. {
  37. return $this->_id;
  38. }
  39. /**
  40. * @return mixed
  41. */
  42. public function getGitId()
  43. {
  44. return $this->gitId;
  45. }
  46. /**
  47. * @param string $id
  48. * @return Inventory
  49. */
  50. public function setGitId(string $id)
  51. {
  52. $this->gitId = $id;
  53. return $this;
  54. }
  55. /**
  56. * @return array
  57. */
  58. public function getPlacements(): array
  59. {
  60. return $this->placements;
  61. }
  62. /**
  63. * @param array $placements
  64. * @return Inventory
  65. */
  66. public function setPlacements(array $placements): Inventory
  67. {
  68. $this->placements = $placements;
  69. $this->setPlacementCount(count($placements));
  70. return $this;
  71. }
  72. /**
  73. * @return int
  74. */
  75. public function getPlacementCount(): int
  76. {
  77. return $this->placementCount;
  78. }
  79. /**
  80. * @param int $placementCount
  81. * @return Inventory
  82. */
  83. private function setPlacementCount(int $placementCount): Inventory
  84. {
  85. $this->placementCount = $placementCount;
  86. return $this;
  87. }
  88. public function jsonSerialize()
  89. {
  90. return [
  91. 'id' => $this->getId(),
  92. 'gitId' => $this->getGitId(),
  93. 'list' => $this->getPlacements(),
  94. 'placementCount' => $this->getPlacementCount(),
  95. ];
  96. }
  97. public function gitSerialize()
  98. {
  99. return [
  100. 'name' => $this->getGitId(),
  101. 'list' => $this->getPlacements()
  102. ];
  103. }
  104. public function gitUnserialize(array $gitData): Inventory
  105. {
  106. return $this->setPlacements($gitData['list']);
  107. }
  108. public function validateMutation(Mutation $m) {
  109. return $m->getOldValue()['list'] === $this->getPlacements();
  110. }
  111. }