Completed
Push — master ( 22ba15...552305 )
by Jean-Christophe
03:10
created

FieldTrait::setName()   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
3
namespace Ajax\semantic\html\collections\form\traits;
4
5
use Ajax\semantic\html\modules\HtmlDropdown;
6
use Ajax\semantic\html\elements\HtmlButton;
7
use Ajax\semantic\html\base\constants\Direction;
8
use Ajax\semantic\html\base\constants\State;
9
10
trait FieldTrait {
11
12
	abstract public function addToProperty($name, $value, $separator=" ");
13
	abstract public function addLabel($caption, $style="label-default", $leftSeparator="&nbsp;");
14
	abstract public function addContent($content,$before=false);
15
	abstract public function getField();
16
	public function setFocus() {
17
		$this->getField()->addToProperty("class", State::FOCUS);
18
	}
19
20
	public function addLoading() {
21
		if ($this->_hasIcon === false) {
0 ignored issues
show
Bug introduced by
The property _hasIcon does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
22
			throw new \Exception("Input must have an icon for showing a loader, use addIcon before");
23
		}
24
		return $this->addToProperty("class", State::LOADING);
25
	}
26
27
	public function labeled($label, $direction=Direction::LEFT, $icon=NULL) {
28
		$field=$this->getField();
29
		$labelO=$field->addLabel($label,$direction===Direction::LEFT,$icon);
30
		$field->addToProperty("class", $direction . " labeled");
31
		return $labelO;
32
	}
33
34
	public function labeledToCorner($icon, $direction=Direction::LEFT) {
35
		return $this->labeled("", $direction . " corner", $icon)->toCorner($direction);
36
	}
37
38
	public function addAction($action, $direction=Direction::RIGHT, $icon=NULL, $labeled=false) {
39
		$field=$this->getField();
40
		$actionO=$action;
41
		if (\is_object($action) === false) {
42
			$actionO=new HtmlButton("action-" . $this->identifier, $action);
0 ignored issues
show
Bug introduced by
The property identifier does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
			if (isset($icon))
44
				$actionO->addIcon($icon, true, $labeled);
45
		}
46
		$field->addToProperty("class", $direction . " action");
47
		$field->addContent($actionO, \strstr($direction, Direction::LEFT) !== false);
48
		return $actionO;
49
	}
50
51
	public function addDropdown($label="", $items=array(),$direction=Direction::RIGHT){
52
		$labelO=new HtmlDropdown("dd-".$this->identifier,$label,$items);
53
		$labelO->asSelect("select-".$this->identifier,false,true);
54
		return $this->labeled($labelO,$direction);
55
	}
56
57
	public function setTransparent() {
58
		return $this->getField()->addToProperty("class", "transparent");
59
	}
60
61
	public function setReadonly(){
62
		$this->getDataField()->setProperty("readonly", "");
0 ignored issues
show
Bug introduced by
It seems like getDataField() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
63
	}
64
65
	public function setName($name){
66
		$this->getDataField()->setProperty("name",$name);
0 ignored issues
show
Bug introduced by
It seems like getDataField() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
67
	}
68
69
}