src/Eccube/Entity/Master/AbstractMasterEntity.php line 23

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of EC-CUBE
  4. *
  5. * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6. *
  7. * http://www.ec-cube.co.jp/
  8. *
  9. * For the full copyright and license information, please view the LICENSE
  10. * file that was distributed with this source code.
  11. */
  12. namespace Eccube\Entity\Master;
  13. use Doctrine\ORM\Mapping as ORM;
  14. /**
  15. * AbstractMasterentity
  16. *
  17. * @ORM\MappedSuperclass
  18. */
  19. abstract class AbstractMasterEntity extends \Eccube\Entity\AbstractEntity
  20. {
  21. /**
  22. * @return string
  23. */
  24. public function __toString()
  25. {
  26. return (string) $this->getName();
  27. }
  28. /**
  29. * @var int
  30. *
  31. * @ORM\Column(name="id", type="smallint", options={"unsigned":true})
  32. * @ORM\Id
  33. * @ORM\GeneratedValue(strategy="NONE")
  34. */
  35. protected $id;
  36. /**
  37. * @var string
  38. *
  39. * @ORM\Column(name="name", type="string", length=255)
  40. */
  41. protected $name;
  42. /**
  43. * @var int
  44. *
  45. * @ORM\Column(name="sort_no", type="smallint", options={"unsigned":true})
  46. */
  47. protected $sort_no;
  48. /**
  49. * Set id.
  50. *
  51. * @param int $id
  52. *
  53. * @return $this
  54. */
  55. public function setId($id)
  56. {
  57. $this->id = $id;
  58. return $this;
  59. }
  60. /**
  61. * Get id.
  62. *
  63. * @return int
  64. */
  65. public function getId()
  66. {
  67. return $this->id;
  68. }
  69. /**
  70. * Set name.
  71. *
  72. * @param string $name
  73. *
  74. * @return $this
  75. */
  76. public function setName($name)
  77. {
  78. $this->name = $name;
  79. return $this;
  80. }
  81. /**
  82. * Get name.
  83. *
  84. * @return string
  85. */
  86. public function getName()
  87. {
  88. return $this->name;
  89. }
  90. /**
  91. * Set sortNo.
  92. *
  93. * @param int $sortNo
  94. *
  95. * @return $this
  96. */
  97. public function setSortNo($sortNo)
  98. {
  99. $this->sort_no = $sortNo;
  100. return $this;
  101. }
  102. /**
  103. * Get sortNo.
  104. *
  105. * @return int
  106. */
  107. public function getSortNo()
  108. {
  109. return $this->sort_no;
  110. }
  111. public function __get($name)
  112. {
  113. return self::getConstantValue($name);
  114. }
  115. public function __set($name, $value)
  116. {
  117. throw new \InvalidArgumentException();
  118. }
  119. public static function __callStatic($name, $arguments)
  120. {
  121. return self::getConstantValue($name);
  122. }
  123. protected static function getConstantValue($name)
  124. {
  125. if (in_array($name, ['id', 'name', 'sortNo'])) {
  126. throw new \InvalidArgumentException();
  127. }
  128. // see also. http://qiita.com/Hiraku/items/71e385b56dcaa37629fe
  129. $ref = new \ReflectionClass(static::class);
  130. // クラス定数が存在していれば, クラス定数から値を取得する
  131. $constants = $ref->getConstants();
  132. if (array_key_exists($name, $constants)) {
  133. return $constants[$name];
  134. }
  135. // XXX $obj = new static(); とすると segmentation fault が発生するため, リフレクションで値を取得する
  136. $refProperty = $ref->getProperty($name);
  137. $refProperty->setAccessible(true);
  138. return $refProperty->getValue($ref->newInstance());
  139. }
  140. }