Register.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\Core\RegisterManager;
  3. use \ModulesGarden\Servers\ZimbraEmail\Core\HandlerError\Exceptions\Exception;
  4. use \ModulesGarden\Servers\ZimbraEmail\Core\HandlerError\ErrorCodes\ErrorCodesLib;
  5. /**
  6. * Description of Register
  7. *
  8. * @author Rafał Ossowski <rafal.os@modulesgarden.com>
  9. */
  10. class Register
  11. {
  12. /**
  13. * @var Register
  14. */
  15. private static $instace;
  16. /**
  17. * @var Entity[]
  18. */
  19. private $registeredEntity = [];
  20. private function __construct()
  21. {
  22. }
  23. private function __clone()
  24. {
  25. }
  26. public function register($key = null, $data = null)
  27. {
  28. if ($key != null)
  29. {
  30. if ($this->isRegister($key))
  31. {
  32. throw new Exception(ErrorCodesLib::CORE_CREG_000001, ['registerKey' => $key]);
  33. }
  34. $key = str_replace(' ', '', $key);
  35. $this->registeredEntity[$key] = $this->factoryEntityModel()
  36. ->setKey($key)
  37. ->setData($data);
  38. }
  39. return $this;
  40. }
  41. public function isRegister($key = null)
  42. {
  43. return isset($this->registeredEntity[$key]);
  44. }
  45. public function registry($key = null)
  46. {
  47. if ($this->isRegister($key))
  48. {
  49. return $this->registeredEntity[$key]->getData();
  50. }
  51. return null;
  52. }
  53. public function removeRegistry($key = null)
  54. {
  55. if ($this->isRegister($key))
  56. {
  57. unset($this->registeredEntity[$key]);
  58. }
  59. return $this;
  60. }
  61. protected static function createInstace()
  62. {
  63. self::$instace = (new Register());
  64. }
  65. public static function getInstace()
  66. {
  67. if (self::$instace === null)
  68. {
  69. self::createInstace();
  70. }
  71. return self::$instace;
  72. }
  73. /**
  74. * @return Entity
  75. */
  76. public function factoryEntityModel()
  77. {
  78. return (new Entity());
  79. }
  80. }