Completed
Push — master ( db82f0...0ba4b1 )
by Jean-Christophe
03:22
created

HtmlGrid::setPadded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Ajax\semantic\html\collections;
4
5
use Ajax\common\html\HtmlCollection;
6
use Ajax\semantic\html\content\HtmlGridRow;
7
use Ajax\semantic\html\base\constants\Wide;
8
use Ajax\semantic\html\base\constants\VerticalAlignment;
9
use Ajax\semantic\html\base\HtmlSemCollection;
10
use Ajax\semantic\html\base\traits\TextAlignmentTrait;
11
12
/**
13
 * Semantic Grid component
14
 * @see http://semantic-ui.com/collections/grid.html
15
 * @author jc
16
 * @version 1.001
17
 */
18
class HtmlGrid extends HtmlSemCollection{
19
	use TextAlignmentTrait;
20
	private $_createCols;
21
	private $_colSizing=true;
22
	private $_implicitRows=false;
23
	public function __construct( $identifier,$numRows=1,$numCols=NULL,$createCols=true,$implicitRows=false){
24
		parent::__construct( $identifier, "div","ui grid");
25
		$this->_implicitRows=$implicitRows;
26
		$this->_createCols=$createCols;
27
		if(isset($numCols)){
28
			//if($this->_createCols){
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
				$this->_colSizing=false;
30
			//}
31
			$cols=Wide::getConstants()["W".$numCols];
32
			$this->addToProperty("class",$cols." column");
33
		}
34
		$this->setNumRows($numRows,$numCols);
35
	}
36
37
	/**
38
	 * Create $numRows rows
39
	 * @param int $numRows
40
	 * @param int $numCols
41
	 * @return \Ajax\semantic\html\collections\HtmlGrid
42
	 */
43
	public function setNumRows($numRows,$numCols=NULL){
44
		$count=$this->count();
45
		for($i=$count;$i<$numRows;$i++){
46
			$this->addItem($numCols);
47
		}
48
		return $this;
49
	}
50
51
	/**
52
	 * return the row at $index
53
	 * @param int $index
54
	 * @return \Ajax\semantic\html\collections\HtmlGridRow
55
	 */
56
	public function getRow($index){
57
		return $this->getItem($index);
58
	}
59
60
	/**
61
	 * @param int $row
62
	 * @param int $col
63
	 * @return \Ajax\semantic\html\collections\HtmlGridCol
64
	 */
65
	public function getCell($row,$col){
66
		$row=$this->getItem($row);
67
		if(isset($row)){
68
			$col=$row->getItem($col);
69
		}
70
		return $col;
71
	}
72
73
	/**
74
	 * Adds dividers between columns ($vertically=false) or between rows ($vertically=true)
75
	 * @param boolean $vertically
76
	 * @return \Ajax\semantic\html\collections\HtmlGrid
77
	 */
78
	public function setDivided($vertically=false){
79
		$value=($vertically===true)?"vertically divided":"divided";
80
		return $this->addToProperty("class", $value);
81
	}
82
83
	/**
84
	 * Divides rows into cells
85
	 * @param boolean $internal true for internal cells
86
	 * @return \Ajax\semantic\html\collections\HtmlGrid
87
	 */
88
	public function setCelled($internal=false){
89
		$value=($internal===true)?"internal celled":"celled";
90
		return $this->addToProperty("class", $value);
91
	}
92
93
	/**
94
	 * automatically resize all elements to split the available width evenly
95
	 * @return \Ajax\semantic\html\collections\HtmlGrid
96
	 */
97
	public function setEqualWidth(){
98
		return $this->addToProperty("class", "equal width");
99
	}
100
101
	/**
102
	 * Adds vertical or/and horizontal gutters
103
	 * @param string $value
104
	 * @return \Ajax\semantic\html\collections\HtmlGrid
105
	 */
106
	public function setPadded($value=NULL){
107
		if(isset($value))
108
			$this->addToPropertyCtrl("class", $value,array("vertically","horizontally"));
109
		return $this->addToProperty("class", "padded");
110
	}
111
112
	/**
113
	 * @param boolean $very
114
	 * @return \Ajax\semantic\html\collections\HtmlGrid
115
	 */
116
	public function setRelaxed($very=false){
117
		$value=($very===true)?"very relaxed":"relaxed";
118
		return $this->addToProperty("class", $value);
119
	}
120
121
	public function setVerticalAlignment($value=VerticalAlignment::MIDDLE){
122
		return $this->addToPropertyCtrl("class", $value." aligned",VerticalAlignment::getConstantValues("aligned"));
123
	}
124
125
	/**
126
	 * {@inheritDoc}
127
	 * @see \Ajax\common\html\HtmlCollection::createItem()
128
	 */
129
	protected function createItem($value){
130
		if($this->_createCols===false)
131
			$value=null;
132
		$item=new HtmlGridRow($this->identifier."-row-".($this->count()+1),$value,$this->_colSizing,$this->_implicitRows);
133
		return $item;
134
	}
135
136
	public function setValues($values){
137
		$count=$this->count();
138
		if($this->_createCols===false){
139
			for($i=$count;$i<\sizeof($values);$i++){
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
140
				$colSize=\sizeof($values[$i]);
141
				$this->addItem(new HtmlGridRow($this->identifier."-row-".($this->count()+1),$colSize,$this->_colSizing,$this->_implicitRows));
142
			}
143
		}
144
		$count=\min(array($this->count(),\sizeof($values)));
145
		for($i=0;$i<$count;$i++){
146
			$this->content[$i]->setValues($values[$i],$this->_createCols===false);
147
		}
148
	}
149
150
}