Completed
Push — master ( daaff8...44ccfa )
by Ingo
22:24 queued 11:43
created

FormSchema   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 76
rs 10
wmc 8
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getState() 0 19 2
A getFieldStates() 0 12 3
B getSchema() 0 28 3
1
<?php
2
3
namespace SilverStripe\Forms\Schema;
4
5
use Form;
6
use FormField;
7
use CompositeField;
8
9
/**
10
 * Class FormSchema
11
 * @package SilverStripe\Forms\Schema
12
 *
13
 * Represents a {@link Form} as structured data which allows a frontend library to render it.
14
 * Includes information about the form as well as its fields.
15
 * Can create a "schema" (structure only) as well as "state" (data only).
16
 */
17
class FormSchema {
18
19
	/**
20
	 * Gets the schema for this form as a nested array.
21
	 *
22
	 * @param Form $form
23
	 * @return array
24
	 */
25
	public function getSchema(Form $form) {
26
		$request = $form->controller()->getRequest();
0 ignored issues
show
Deprecated Code introduced by
The method Form::Controller() has been deprecated with message: 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
27
28
		$schema = [
29
			'name' => $form->getName(),
30
			'id' => $form->FormName(),
31
			'action' => $form->FormAction(),
32
			'method' => $form->FormMethod(),
33
			// @todo Not really reliable. Refactor into action on $this->Link('schema')
34
			'schema_url' => $request->getUrl(),
35
			'attributes' => $form->getAttributes(),
36
			'data' => [],
37
			'fields' => [],
38
			'actions' => []
39
		];
40
41
		foreach ($form->Actions() as $action) {
0 ignored issues
show
Bug introduced by
The expression $form->Actions() of type object<FieldList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
42
			/** @var FormField $action */
43
			$schema['actions'][] = $action->getSchemaData();
44
		}
45
46
		foreach ($form->Fields() as $field) {
0 ignored issues
show
Bug introduced by
The expression $form->Fields() of type object<FieldList>|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
47
			/** @var FormField $field */
48
			$schema['fields'][] = $field->getSchemaData();
49
		}
50
51
		return $schema;
52
	}
53
54
	/**
55
	 * Gets the current state of this form as a nested array.
56
	 *
57
	 * @param Form $form
58
	 * @return array
59
	 */
60
	public function getState(Form $form) {
61
		$state = [
62
			'id' => $form->FormName(),
63
			'fields' => [],
64
			'messages' => []
65
		];
66
67
		// flattened nested fields are returned, rather than only top level fields.
68
		$state['fields'] = $this->getFieldStates($form->Fields());
69
70
		if($form->Message()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form->Message() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
71
			$state['messages'][] = [
72
				'value' => $form->Message(),
73
				'type' => $form->MessageType(),
74
			];
75
		}
76
77
		return $state;
78
	}
79
80
	protected function getFieldStates($fields) {
81
		$states = [];
82
		foreach ($fields as $field) {
83
			$states[] = $field->getSchemaState();
84
85
			if ($field instanceof CompositeField) {
86
				$subFields = $field->FieldList();
87
				array_merge($states, $this->getFieldStates($subFields));
88
			}
89
		}
90
		return $states;
91
	}
92
}
93