AttributeValue.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of the Sylius package.
  4. *
  5. * (c) Paweł Jędrzejewski
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sylius\Component\Attribute\Model;
  11. /**
  12. * Attribute to subject relation.
  13. *
  14. * @author Paweł Jędrzejewski <pawel@sylius.org>
  15. */
  16. class AttributeValue implements AttributeValueInterface
  17. {
  18. /**
  19. * Id.
  20. *
  21. * @var integer
  22. */
  23. protected $id;
  24. /**
  25. * Subject.
  26. *
  27. * @var AttributeSubjectInterface
  28. */
  29. protected $subject;
  30. /**
  31. * Attribute.
  32. *
  33. * @var AttributeInterface
  34. */
  35. protected $attribute;
  36. /**
  37. * Attribute value.
  38. *
  39. * @var mixed
  40. */
  41. protected $value;
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function __toString()
  46. {
  47. return $this->value;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getId()
  53. {
  54. return $this->id;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function getSubject()
  60. {
  61. return $this->subject;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function setSubject(AttributeSubjectInterface $subject = null)
  67. {
  68. $this->subject = $subject;
  69. return $this;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getAttribute()
  75. {
  76. return $this->attribute;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function setAttribute(AttributeInterface $attribute)
  82. {
  83. $this->attribute = $attribute;
  84. return $this;
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. public function getValue()
  90. {
  91. if ($this->attribute && AttributeTypes::CHECKBOX === $this->attribute->getType()) {
  92. return (Boolean) $this->value;
  93. }
  94. return $this->value;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function setValue($value)
  100. {
  101. $this->value = $value;
  102. return $this;
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getName()
  108. {
  109. $this->assertAttributeIsSet();
  110. return $this->attribute->getName();
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function getPresentation()
  116. {
  117. $this->assertAttributeIsSet();
  118. return $this->attribute->getPresentation();
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function getType()
  124. {
  125. $this->assertAttributeIsSet();
  126. return $this->attribute->getType();
  127. }
  128. /**
  129. * {@inheritdoc}
  130. */
  131. public function getConfiguration()
  132. {
  133. $this->assertAttributeIsSet();
  134. return $this->attribute->getConfiguration();
  135. }
  136. /**
  137. * @throws \BadMethodCallException When attribute is not set
  138. */
  139. protected function assertAttributeIsSet()
  140. {
  141. if (null === $this->attribute) {
  142. throw new \BadMethodCallException('The attribute is undefined, so you cannot access proxy methods.');
  143. }
  144. }
  145. }