Passed
Push — develop ( 793a1d...14a8df )
by Richard
03:33
created

FormResponse::flatten()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Riclep\StoryblokForms;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Http\UploadedFile;
7
use Illuminate\Support\Arr;
8
use Riclep\Storyblok\StoryblokFacade as StoryBlok;
9
use Riclep\StoryblokForms\Blocks\LsfFieldset;
10
11
class FormResponse
12
{
13
	// TODO - pass extra data - imagine a staff contact form wrapped in a component where they select the email address this instance should go to
14
15
16
	/**
17
	 * @param Request $request
18
	 */
19
	public function __construct(Request $request)
20
	{
21
		// convert JSON response to same format as standard HTML forms
22
		if ($request->isJson()) {
23
			$request->replace(Arr::undot($request->all()));
24
		}
25
26
		$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...
27
28
		$this->requestPage();
29
	}
30
31
32
	/**
33
	 * @return void
34
	 */
35
	protected function requestPage() {
36
		$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...
37
	}
38
39
	/**
40
	 * @return \Illuminate\Support\Collection
41
	 */
42
	protected function form() {
43
		return $this->page->form;
44
	}
45
46
	/**
47
	 * @return array
48
	 */
49
	public function validate() {
50
		return $this->request->validate($this->form()->validationRules(), $this->form()->errorMessages());
51
	}
52
53
	public function validationRules() {
54
		return $this->form()->validationRules();
55
	}
56
57
	/**
58
	 * @return mixed
59
	 */
60
	public function response() {
61
		return $this->responses();
62
	}
63
64
	/**
65
	 * @return false|string
66
	 */
67
	public function json() {
68
		return json_encode($this->response());
69
	}
70
71
	/**
72
	 * @return mixed
73
	 */
74
	protected function responses() {
75
		$input = $this->request->except(['_token', '_slug']);
76
77
		return $this->form()->fields->map(function ($field) use ($input) {
78
79
			// Handle empty radio buttons sending nothing in POST request
80
			//if ($field instanceof \Riclep\StoryblokForms\Blocks\LsfRadioButton) {
81
				if (!array_key_exists($field->name, $input)) {
82
					$input[$field->name] = null;
83
				}
84
			//}
85
86
			return $field->response($input[$field->name]);
87
		})->toArray();
88
	}
89
90
91
	/**
92
	 * Flattens the response returning an array of responses
93
	 *
94
	 * @return array
95
	 */
96
	public function flatten() {
97
		return Arr::flatten($this->response());
98
	}
99
100
	public function files() {
101
		return array_values(array_filter($this->flatten(), function ($response) {
102
			return $response instanceof UploadedFile;
103
		}));
104
	}
105
}