Completed
Push — master ( d19955...af891e )
by Sam
22s
created

Tab::fieldByName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Implements a single tab in a {@link TabSet}.
4
 *
5
 * Here is a simple implementation of a Tab. Obviously, you can include as much fields
6
 * inside as you want. A tab can contain other tabs as well.
7
 *
8
 * <code>
9
 * new Tab(
10
 * 	$title='Tab one',
11
 * 	new HeaderField("A header"),
12
 * 	new LiteralField("Lipsum","Lorem ipsum dolor sit amet enim.")
13
 * )
14
 * </code>
15
 *
16
 * @package forms
17
 * @subpackage fields-structural
18
 */
19
class Tab extends CompositeField {
20
21
	/**
22
	 * @var TabSet
23
	 */
24
	protected $tabSet;
25
26
	/**
27
	 * @var string
28
	 */
29
	protected $id;
30
31
	/**
32
	 * @uses FormField::name_to_label()
33
	 *
34
	 * @param string $name Identifier of the tab, without characters like dots or spaces
35
	 * @param string|FormField $titleOrField Natural language title of the tabset, or first tab.
36
	 * If its left out, the class uses {@link FormField::name_to_label()} to produce a title
37
	 * from the {@link $name} parameter.
38
	 * @param FormField ...$fields All following parameters are inserted as children to this tab
39
	 */
40
	public function __construct($name, $titleOrField = null, $fields = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $fields 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...
41
		if(!is_string($name)) {
42
			throw new InvalidArgumentException('Invalid string parameter for $name');
43
		}
44
45
		// Get following arguments
46
		$fields = func_get_args();
47
		array_shift($fields);
48
49
		// Detect title from second argument, if it is a string
50
		if($titleOrField && is_string($titleOrField)) {
51
			$title = $titleOrField;
52
			array_shift($fields);
53
		} else {
54
			$title = static::name_to_label($name);
55
		}
56
57
		// Remaining arguments are child fields
58
		parent::__construct($fields);
59
60
		// Assign name and title (not assigned by parent constructor)
61
		$this->setName($name);
62
		$this->setTitle($title);
63
		$this->setID(Convert::raw2htmlid($name));
0 ignored issues
show
Bug introduced by
It seems like \Convert::raw2htmlid($name) targeting Convert::raw2htmlid() can also be of type array; however, Tab::setID() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
64
	}
65
66
	public function ID() {
67
		if($this->tabSet) {
68
			return $this->tabSet->ID() . '_' . $this->id;
69
		} else {
70
			return $this->id;
71
		}
72
	}
73
74
	/**
75
	 * Set custom HTML ID to use for this tabset
76
	 *
77
	 * @param string $id
78
	 * @return $this
79
	 */
80
	public function setID($id) {
81
		$this->id = $id;
82
		return $this;
83
	}
84
85
	/**
86
	 * Get child fields
87
	 *
88
	 * @return FieldList
89
	 */
90
	public function Fields() {
91
		return $this->children;
92
	}
93
94
	/**
95
	 * Assign to a TabSet instance
96
	 *
97
	 * @param TabSet $val
98
	 * @return $this
99
	 */
100
	public function setTabSet($val) {
101
		$this->tabSet = $val;
102
		return $this;
103
	}
104
105
	/**
106
	 * Get parent tabset
107
	 *
108
	 * @return TabSet
109
	 */
110
	public function getTabSet() {
111
		return $this->tabSet;
112
	}
113
114
	public function extraClass() {
115
		return implode(' ', (array)$this->extraClasses);
116
	}
117
118
	public function getAttributes() {
119
		return array_merge(
120
			$this->attributes,
121
			array(
122
				'id' => $this->ID(),
123
				'class' => 'tab ' . $this->extraClass()
124
			)
125
		);
126
	}
127
}
128