Repository.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace Dcat\Admin\Repositories;
  3. use Dcat\Admin\Contracts\Repository as RepositoryInterface;
  4. use Dcat\Admin\Contracts\TreeRepository;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Traits\Macroable;
  10. use RuntimeException;
  11. abstract class Repository implements RepositoryInterface, TreeRepository
  12. {
  13. use Macroable;
  14. /**
  15. * @var string
  16. */
  17. protected $keyName = 'id';
  18. /**
  19. * @var bool
  20. */
  21. protected $isSoftDeletes = false;
  22. /**
  23. * 获取主键名称.
  24. *
  25. * @return string|array
  26. */
  27. public function getKeyName()
  28. {
  29. return $this->keyName ?: 'id';
  30. }
  31. /**
  32. * 设置主键名称.
  33. *
  34. * @param string|array $keyName
  35. */
  36. public function setKeyName($keyName)
  37. {
  38. $this->keyName = $keyName;
  39. }
  40. /**
  41. * 获取创建时间字段.
  42. *
  43. * @return string
  44. */
  45. public function getCreatedAtColumn()
  46. {
  47. return 'created_at';
  48. }
  49. /**
  50. * 获取更新时间字段.
  51. *
  52. * @return string
  53. */
  54. public function getUpdatedAtColumn()
  55. {
  56. return 'updated_at';
  57. }
  58. /**
  59. * 是否使用软删除.
  60. *
  61. * @return bool
  62. */
  63. public function isSoftDeletes()
  64. {
  65. return $this->isSoftDeletes;
  66. }
  67. /**
  68. * @param bool $isSoftDeletes
  69. */
  70. public function setIsSoftDeletes(?bool $isSoftDeletes)
  71. {
  72. $this->isSoftDeletes = $isSoftDeletes;
  73. }
  74. /**
  75. * 获取Grid表格数据.
  76. *
  77. * @param Grid\Model $model
  78. *
  79. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|Collection|array
  80. */
  81. public function get(Grid\Model $model)
  82. {
  83. throw new RuntimeException('This repository does not support "get" method.');
  84. }
  85. /**
  86. * 获取编辑页面数据.
  87. *
  88. * @param Form $form
  89. *
  90. * @return array|\Illuminate\Contracts\Support\Arrayable
  91. */
  92. public function edit(Form $form)
  93. {
  94. throw new RuntimeException('This repository does not support "edit" method.');
  95. }
  96. /**
  97. * 获取详情页面数据.
  98. *
  99. * @param Show $show
  100. *
  101. * @return array|\Illuminate\Contracts\Support\Arrayable
  102. */
  103. public function detail(Show $show)
  104. {
  105. throw new RuntimeException('This repository does not support "detail" method.');
  106. }
  107. /**
  108. * 新增记录.
  109. *
  110. * @param Form $form
  111. *
  112. * @return mixed
  113. */
  114. public function store(Form $form)
  115. {
  116. throw new RuntimeException('This repository does not support "store" method.');
  117. }
  118. /**
  119. * 查询更新前的行数据.
  120. *
  121. * @param Form $form
  122. *
  123. * @return array
  124. */
  125. public function updating(Form $form)
  126. {
  127. throw new RuntimeException('This repository does not support "updating" method.');
  128. }
  129. /**
  130. * 更新数据.
  131. *
  132. * @param Form $form
  133. *
  134. * @return bool
  135. */
  136. public function update(Form $form)
  137. {
  138. throw new RuntimeException('This repository does not support "update" method.');
  139. }
  140. /**
  141. * 删除数据.
  142. *
  143. * @param Form $form
  144. * @param array $deletingData
  145. *
  146. * @return mixed
  147. */
  148. public function delete(Form $form, array $deletingData)
  149. {
  150. throw new RuntimeException('This repository does not support "destroy" method.');
  151. }
  152. /**
  153. * 查询删除前的行数据.
  154. *
  155. * @param Form $form
  156. *
  157. * @return array
  158. */
  159. public function deleting(Form $form)
  160. {
  161. throw new RuntimeException('This repository does not support "deleting" method.');
  162. }
  163. /**
  164. * 获取主键字段名称.
  165. *
  166. * @return string
  167. */
  168. public function getPrimaryKeyColumn()
  169. {
  170. return $this->getKeyName();
  171. }
  172. /**
  173. * 获取父级ID字段名称.
  174. *
  175. * @return string
  176. */
  177. public function getParentColumn()
  178. {
  179. return 'parent_id';
  180. }
  181. /**
  182. * 获取标题字段名称.
  183. *
  184. * @return string
  185. */
  186. public function getTitleColumn()
  187. {
  188. return 'title';
  189. }
  190. /**
  191. * 获取排序字段名称.
  192. *
  193. * @return string
  194. */
  195. public function getOrderColumn()
  196. {
  197. return 'order';
  198. }
  199. /**
  200. * 保存层级数据排序.
  201. *
  202. * @param array $tree
  203. * @param int $parentId
  204. */
  205. public function saveOrder($tree = [], $parentId = 0)
  206. {
  207. throw new RuntimeException('This repository does not support "saveOrder" method.');
  208. }
  209. /**
  210. * 设置数据查询回调.
  211. *
  212. * @param \Closure|null $query
  213. *
  214. * @return $this
  215. */
  216. public function withQuery($queryCallback)
  217. {
  218. throw new RuntimeException('This repository does not support "withQuery" method.');
  219. }
  220. /**
  221. * 获取层级数据.
  222. *
  223. * @return array
  224. */
  225. public function toTree()
  226. {
  227. throw new RuntimeException('This repository does not support "toTree" method.');
  228. }
  229. /**
  230. * @param mixed ...$params
  231. *
  232. * @return $this
  233. */
  234. public static function make(...$params)
  235. {
  236. return new static(...$params);
  237. }
  238. }