Completed
Push — master ( 9281ef...7089ac )
by Jean-Christophe
03:28
created

Form::addField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Ajax\semantic\components;
3
4
use Ajax\JsUtils;
5
use Ajax\semantic\components\validation\FieldValidation;
6
use Ajax\semantic\components\validation\Rule;
7
/**
8
 * @author jc
9
 * @version 1.001
10
 * Generates a JSON form validation string
11
 */
12
class Form extends SimpleSemExtComponent {
13
14
	/**
15
	 * @var array
16
	 */
17
	public function __construct(JsUtils $js=null) {
18
		parent::__construct($js);
0 ignored issues
show
Bug introduced by
It seems like $js defined by parameter $js on line 17 can be null; however, Ajax\semantic\components...omponent::__construct() does not accept null, maybe add an additional type check?

It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.

We recommend to add an additional type check (or disallow null for the parameter):

function notNullable(stdClass $x) { }

// Unsafe
function withoutCheck(stdClass $x = null) {
    notNullable($x);
}

// Safe - Alternative 1: Adding Additional Type-Check
function withCheck(stdClass $x = null) {
    if ($x instanceof stdClass) {
        notNullable($x);
    }
}

// Safe - Alternative 2: Changing Parameter
function withNonNullableParam(stdClass $x) {
    notNullable($x);
}
Loading history...
19
		$this->uiName="form";
20
		$this->params["fields"]=[];
21
	}
22
23
	public function addField($identifier){
24
		$this->params["fields"][$identifier]=new FieldValidation($identifier);
25
	}
26
27
	public function setInline($value){
0 ignored issues
show
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
		return $this->setParam("inline", true);
29
	}
30
31
	public function setOn($value){
32
		return $this->setParam("on", $value);
33
	}
34
35
36
37
	/**
38
	 * @param string $identifier
39
	 * @param Rule|string $type
40
	 * @param mixed $value
41
	 * @param string|NULL $prompt
42
	 */
43
	public function addFieldRule($identifier,$type,$prompt=NULL,$value=NULL){
44
		if(isset($this->params["fields"][$identifier])===false){
45
			$this->addField($identifier);
46
		}
47
		$this->params["fields"][$identifier]->addRule($type,$prompt,$value);
48
	}
49
50
	/**
51
	 * @param FieldValidation $fieldValidation
52
	 */
53
	public function addFieldValidation($fieldValidation){
54
		$this->params["fields"][$fieldValidation->getIdentifier()]=$fieldValidation;
55
	}
56
57
	public function setJs(JsUtils $js){
58
		$this->js=$js;
59
	}
60
61
	public function getScript() {
62
		$allParams=$this->params;
63
		$this->jquery_code_for_compile=array ();
64
		$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).{$this->uiName}(".$this->getParamsAsJSON($allParams).");";
65
		$this->compileEvents();
66
		return $this->compileJQueryCode();
67
	}
68
69
	public function onValid($jsCode){
70
		$this->addComponentEvent("onValid", $jsCode);
71
	}
72
}