Passed
Push — develop ( 529aa7...f56b73 )
by Richard
02:56
created

FormResponse::validationRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Riclep\StoryblokForms;
4
5
use Illuminate\Http\Request;
6
use Riclep\Storyblok\StoryblokFacade as StoryBlok;
7
8
class FormResponse
9
{
10
	// TODO - pass extra data - imagine a staff contact form wrapped in a component where they select the address this instance should go to
11
12
13
	/**
14
	 * @param Request $request
15
	 */
16
	public function __construct(Request $request)
17
	{
18
		$this->request = $request;
0 ignored issues
show
Bug Best Practice introduced by
The property request does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
19
20
		$this->requestPage();
21
	}
22
23
24
	/**
25
	 * @return void
26
	 */
27
	protected function requestPage() {
28
		$this->page = Storyblok::read($this->request->input('_slug'));
0 ignored issues
show
Bug Best Practice introduced by
The property page does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
	}
30
31
	/**
32
	 * @return \Illuminate\Support\Collection
33
	 */
34
	protected function form() {
35
		return $this->page->form;
36
	}
37
38
	/**
39
	 * @return array
40
	 */
41
	public function validate() {
42
		return $this->request->validate($this->form()->validationRules(), $this->form()->errorMessages());
43
	}
44
45
	public function validationRules() {
46
		return $this->form()->validationRules();
47
	}
48
49
	/**
50
	 * @return mixed
51
	 */
52
	public function response() {
53
		return $this->responses();
54
	}
55
56
	/**
57
	 * @return false|string
58
	 */
59
	public function json() {
60
		return json_encode($this->response());
61
	}
62
63
	/**
64
	 * @return mixed
65
	 */
66
	protected function responses() {
67
		$input = $this->request->except(['_token', '_slug']);
68
69
70
		return $this->form()->fields->map(function ($field) use ($input) {
71
72
			//dd($field, $input, $field->name);
73
74
			return $field->response($input[$field->name]);
75
			/*dd($field);
76
77
			return [
78
				'label' => $field->label,
79
				'response' => $field->response($input[$field->name] ?? ''),
80
			];*/
81
		})->toArray();
82
83
84
		/*
85
		return $this->form()->flattenFieldsets()->map(function ($field) use ($input) {
86
			return [
87
				'label' => $field->label,
88
				'response' => $field->response($input[$field->name] ?? ''),
89
			];
90
		})->toArray();*/
91
	}
92
}