CollectionItemBulkActionRequest.php
1 week ago
CollectionItemConditionRequest.php
1 week ago
CollectionItemSingleActionRequest.php
1 month ago
CollectionItemStoreRequest.php
1 week ago
CollectionItemStoreRequest.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Http\Requests\CollectionItem; |
| 4 | |
| 5 | use Kirki\App\Constants\PostStatus; |
| 6 | use Kirki\Framework\Http\Request; |
| 7 | use Kirki\Framework\Sanitizer; |
| 8 | use Kirki\Framework\Supports\Arr; |
| 9 | |
| 10 | use function Kirki\App\ensure_array; |
| 11 | |
| 12 | class CollectionItemStoreRequest extends Request |
| 13 | { |
| 14 | /** |
| 15 | * Prepare data for validation. |
| 16 | */ |
| 17 | protected function prepare_for_validation() |
| 18 | { |
| 19 | $data = $this->get('data'); |
| 20 | |
| 21 | if (is_string($data)) { |
| 22 | $data = json_decode($data, true); |
| 23 | } |
| 24 | |
| 25 | if (is_array($data)) { |
| 26 | $this->merge($data); |
| 27 | } |
| 28 | |
| 29 | if (empty($data['post_status'])) { |
| 30 | $this->merge(['post_status' => PostStatus::DRAFT]); |
| 31 | } |
| 32 | |
| 33 | $this->merge([ |
| 34 | 'fields' => ensure_array($this->fields), |
| 35 | ]); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Validation rules. |
| 40 | */ |
| 41 | public function rules() |
| 42 | { |
| 43 | return [ |
| 44 | 'ID' => 'nullable|integer', |
| 45 | 'post_parent' => 'required|integer', |
| 46 | 'post_title' => 'required|string', |
| 47 | 'post_name' => 'nullable|string', |
| 48 | 'fields' => 'nullable|array', |
| 49 | 'post_status' => 'nullable|string|in:' . Arr::join([ |
| 50 | PostStatus::DRAFT, |
| 51 | PostStatus::PUBLISH, |
| 52 | PostStatus::FUTURE, |
| 53 | ]), |
| 54 | 'post_date' => 'nullable', |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Sanitization filters. |
| 60 | */ |
| 61 | public function filters() |
| 62 | { |
| 63 | return [ |
| 64 | 'ID' => Sanitizer::INT, |
| 65 | 'post_parent' => Sanitizer::INT, |
| 66 | 'post_title' => Sanitizer::TEXT, |
| 67 | 'post_name' => Sanitizer::TEXT, |
| 68 | 'fields' => Sanitizer::ARRAY , |
| 69 | 'post_status' => Sanitizer::TEXT, |
| 70 | 'post_date' => Sanitizer::DATETIME, |
| 71 | ]; |
| 72 | } |
| 73 | } |
| 74 |