Passed
Push — develop ( d74ec0...dc3999 )
by Richard
03:13
created

MultiInput::addToJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Riclep\StoryblokForms;
4
5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
8
class MultiInput extends Input
9
{
10
11
	protected function addToJson() {
12
		$json['options'] = $this->options();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$json was never initialized. Although not strictly required by PHP, it is generally a good practice to add $json = array(); before regardless.
Loading history...
13
14
		return $json;
15
	}
16
17
	/**
18
	 * @return \Illuminate\Support\Collection
19
	 */
20
	public function options() {
21
		return collect(preg_split('/\r\n|\r|\n/', $this->{$this->optionsName}))->filter()->map(function ($option) {
0 ignored issues
show
Bug Best Practice introduced by
The property optionsName does not exist on Riclep\StoryblokForms\MultiInput. Since you implemented __get, consider adding a @property annotation.
Loading history...
22
23
			/**
24
			 * Parses the possible option formats into a named array
25
			 * name
26
			 * [*]name
27
			 * [key]name
28
			 * [key][*]name
29
			 * */
30
			preg_match('/(?:\[(?<value>[\w]+)\])?(?:\[(?<selected>\*)\])?(?<label>.+)/', $option, $settings);
31
32
			if ($settings['value'] === '') {
33
				$settings['value'] = Str::slug($settings['label']);
34
			}
35
36
			if (request()->session()->has('_old_input')) {
37
				$settings['selected'] = $this->optionIsSelected($settings['selected']);
38
			} else {
39
				$settings['selected'] = $settings['selected'] === '*' ? true : false;
40
			}
41
42
			return array_filter($settings, function ($key) {
43
				return is_string($key);
44
			}, ARRAY_FILTER_USE_KEY);
45
		})->values();;
46
	}
47
48
	protected function optionIsSelected($formInput) {
49
		return request()->old($this->input_name) && (in_array(Str::slug($formInput), Arr::wrap(request()->old($this->input_name))));
0 ignored issues
show
Bug Best Practice introduced by
The property input_name does not exist on Riclep\StoryblokForms\MultiInput. Since you implemented __get, consider adding a @property annotation.
Loading history...
50
	}
51
52
	/**
53
	 * Returns the Input’s response after the form has been submitted and validated
54
	 * All options are returned as an array with their name and a selected boolean
55
	 * based on the user’s input
56
	 *
57
	 * @param $input
58
	 * @return array
59
	 */
60
	public function response($input) {
61
		$formatted = [
62
			'label' => $this->label,
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Riclep\StoryblokForms\MultiInput. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
			'response' => ['selected' => [], 'unselected' => []],
64
			'type' => $this->type,
0 ignored issues
show
Bug Best Practice introduced by
The property type does not exist on Riclep\StoryblokForms\MultiInput. Since you implemented __get, consider adding a @property annotation.
Loading history...
65
		];
66
67
		$this->options()->map(function ($formInput) use ($input, &$formatted) {
68
			if (in_array($formInput['value'], Arr::wrap($input))) {
69
				return $formatted['response']['selected'][] = $formInput['label'];
70
			}
71
72
			return $formatted['response']['unselected'][] = $formInput['label'];
73
		})->toArray();
74
75
		return $formatted;
76
	}
77
}