Manager.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace ForkBB\Models\User;
  3. use ForkBB\Models\ManagerModel;
  4. use ForkBB\Models\User\Model as User;
  5. use InvalidArgumentException;
  6. class Manager extends ManagerModel
  7. {
  8. /**
  9. * Создает новую модель пользователя
  10. *
  11. * @param array $attrs
  12. *
  13. * @return User
  14. */
  15. public function create(array $attrs = []): User
  16. {
  17. return $this->c->UserModel->setAttrs($attrs);
  18. }
  19. /**
  20. * Получает пользователя по id
  21. *
  22. * @throws InvalidArgumentException
  23. */
  24. public function load(int $id): ?User
  25. {
  26. if ($id < 1) {
  27. throw new InvalidArgumentException('Expected id > 0');
  28. }
  29. if ($this->isset($id)) {
  30. return $this->get($id);
  31. } else {
  32. $user = $this->Load->load($id);
  33. $this->set($id, $user);
  34. return $user;
  35. }
  36. }
  37. /**
  38. * Получает массив пользователей по ids
  39. *
  40. * @throws InvalidArgumentException
  41. */
  42. public function loadByIds(array $ids): array
  43. {
  44. $result = [];
  45. $data = [];
  46. foreach ($ids as $id) {
  47. if (! \is_int($id) || $id < 1) {
  48. throw new InvalidArgumentException('Expected id > 0');
  49. }
  50. if ($this->isset($id)) {
  51. $result[$id] = $this->get($id);
  52. } else {
  53. $result[$id] = null;
  54. $data[] = $id;
  55. $this->set($id, null);
  56. }
  57. }
  58. if (empty($data)) {
  59. return $result;
  60. }
  61. foreach ($this->Load->loadByIds($data) as $user) {
  62. if ($user instanceof User) {
  63. $result[$user->id] = $user;
  64. $this->set($user->id, $user);
  65. }
  66. }
  67. return $result;
  68. }
  69. /**
  70. * Возвращает результат
  71. */
  72. protected function returnUser(?User $user): ?User
  73. {
  74. if ($user instanceof User) {
  75. $loadedUser = $this->get($user->id);
  76. if ($loadedUser instanceof User) {
  77. return $loadedUser;
  78. } else {
  79. $this->set($user->id, $user);
  80. return $user;
  81. }
  82. } else {
  83. return null;
  84. }
  85. }
  86. /**
  87. * Получает пользователя по имени
  88. */
  89. public function loadByName(string $name, bool $caseInsencytive = false): ?User
  90. {
  91. return $this->returnUser($this->Load->loadByName($name, $caseInsencytive));
  92. }
  93. /**
  94. * Получает пользователя по email
  95. */
  96. public function loadByEmail(string $email): ?User
  97. {
  98. return $this->returnUser($this->Load->loadByEmail($email));
  99. }
  100. /**
  101. * Обновляет данные пользователя
  102. *
  103. * @param User $user
  104. *
  105. * @return User
  106. */
  107. public function update(User $user): User
  108. {
  109. return $this->Save->update($user);
  110. }
  111. /**
  112. * Добавляет новую запись в таблицу пользователей
  113. *
  114. * @param User $user
  115. *
  116. * @return int
  117. */
  118. public function insert(User $user): int
  119. {
  120. $id = $this->Save->insert($user);
  121. $this->set($id, $user);
  122. return $id;
  123. }
  124. }