Completed
Push — master ( 141d55...411df4 )
by Jean-Christophe
02:55
created

Form::addFieldRule()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 4
1
<?php
2
namespace Ajax\semantic\components;
3
4
use Ajax\common\components\SimpleExtComponent;
5
use Ajax\JsUtils;
6
use Ajax\semantic\components\validation\FieldValidation;
7
use Ajax\semantic\components\validation\Rule;
8
/**
9
 * @author jc
10
 * @version 1.001
11
 * Generates a JSON form validation string
12
 */
13
class Form extends SimpleExtComponent {
14
15
	/**
16
	 * @var array
17
	 */
18
	public function __construct(JsUtils $js=null) {
19
		parent::__construct($js);
0 ignored issues
show
Bug introduced by
It seems like $js defined by parameter $js on line 18 can be null; however, Ajax\common\components\S...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...
20
		$this->uiName="form";
21
		$this->params["fields"]=[];
22
	}
23
24
	public function addField($identifier){
25
		$this->params["fields"][$identifier]=new FieldValidation($identifier);
26
	}
27
28
	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...
29
		return $this->setParam("inline", true);
30
	}
31
32
	public function setOn($value){
33
		return $this->setParam("on", $value);
34
	}
35
36
37
38
	/**
39
	 * @param string $identifier
40
	 * @param Rule|string $type
41
	 * @param mixed $value
42
	 * @param string|NULL $prompt
43
	 */
44
	public function addFieldRule($identifier,$type,$prompt=NULL,$value=NULL){
45
		if(isset($this->params["fields"][$identifier])===false){
46
			$this->addField($identifier);
47
		}
48
		$this->params["fields"][$identifier]->addRule($type,$prompt,$value);
49
	}
50
51
	/**
52
	 * @param FieldValidation $fieldValidation
53
	 */
54
	public function addFieldValidation($fieldValidation){
55
		$this->params["fields"][$fieldValidation->getIdentifier()]=$fieldValidation;
56
	}
57
58
	public function setJs(JsUtils $js){
59
		$this->js=$js;
60
	}
61
62 View Code Duplication
	public function getScript() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
		$allParams=$this->params;
64
		$this->jquery_code_for_compile=array ();
65
		$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).{$this->uiName}(".$this->getParamsAsJSON($allParams).");";
66
		$this->compileEvents();
67
		return $this->compileJQueryCode();
68
	}
69
}