HasFields.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /*
  3. * This file is part of the Dcat package.
  4. *
  5. * (c) Pian Zhou <pianzhou2021@163.com>
  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 Dcat\Admin\Form\Concerns;
  11. use Dcat\Admin\Contracts\FieldsCollection;
  12. use Dcat\Admin\Form\Field;
  13. use Illuminate\Support\Collection;
  14. trait HasFields
  15. {
  16. /**
  17. * @var Collection|Field[]
  18. */
  19. private $fields;
  20. /**
  21. * Get fields of this builder.
  22. *
  23. * @return Collection
  24. */
  25. public function fields()
  26. {
  27. if (! $this->fields) {
  28. $this->resetFields();
  29. }
  30. return $this->fields;
  31. }
  32. /**
  33. * Get specify field.
  34. *
  35. * @param string|Field $name
  36. * @return Field|null
  37. */
  38. public function field($name)
  39. {
  40. return $this->fields()->first(function (Field $field) use ($name) {
  41. if (is_array($field->column())) {
  42. $result = in_array($name, $field->column(), true) || $field->column() === $name ? $field : null;
  43. if ($result) {
  44. return $result;
  45. }
  46. }
  47. return $field === $name || $field->column() === $name;
  48. });
  49. }
  50. /**
  51. * Remove Field.
  52. *
  53. * @param $column
  54. * @return void
  55. */
  56. public function removeField($column)
  57. {
  58. $this->fields = $this->fields()->filter(function (Field $field) use ($column) {
  59. return $field->column() != $column;
  60. });
  61. }
  62. /**
  63. * Push Field.
  64. *
  65. * @param Field $field
  66. * @return Collection
  67. */
  68. public function pushField(Field $field)
  69. {
  70. $this->fields()->push($field);
  71. }
  72. /**
  73. * Reset Fields.
  74. *
  75. * @return void
  76. */
  77. public function resetFields()
  78. {
  79. $this->fields = new Collection();
  80. }
  81. /**
  82. * Reject Fields.
  83. *
  84. * @param [type] $reject
  85. * @return void
  86. */
  87. public function rejectFields($reject)
  88. {
  89. $this->fields = $this->fields()->reject($reject);
  90. }
  91. /**
  92. * Set Fields.
  93. *
  94. * @param Collection $fields
  95. * @return void
  96. */
  97. public function setFields(Collection $fields)
  98. {
  99. $this->fields = $fields;
  100. }
  101. /**
  102. * Get all merged fields.
  103. *
  104. * @return array
  105. */
  106. protected function mergedFields()
  107. {
  108. $fields = [];
  109. foreach ($this->fields() as $field) {
  110. if ($field instanceof FieldsCollection) {
  111. /** @var Field $field */
  112. $fields = array_merge($fields, $field->mergedFields());
  113. } else {
  114. $fields[] = $field;
  115. }
  116. }
  117. return $fields;
  118. }
  119. }