Completed
Push — master ( 70601d...0c3815 )
by Jean-Christophe
03:29
created

HtmlGrid::setValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 9
nc 4
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
use Ajax\semantic\html\content\HtmlGridCol;
12
13
/**
14
 * Semantic Grid component
15
 * @see http://semantic-ui.com/collections/grid.html
16
 * @author jc
17
 * @version 1.001
18
 */
19
class HtmlGrid extends HtmlSemCollection{
20
	use TextAlignmentTrait;
21
	private $_createCols;
22
	private $_colSizing=true;
23
	private $_implicitRows=false;
24
	public function __construct( $identifier,$numRows=1,$numCols=NULL,$createCols=true,$implicitRows=false){
25
		parent::__construct( $identifier, "div","ui grid");
26
		$this->_implicitRows=$implicitRows;
27
		$this->_createCols=$createCols;
28
		if(isset($numCols)){
29
			//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...
30
				$this->_colSizing=false;
31
			//}
32
			$this->setWide($numCols);
33
		}
34
		$this->setRowsCount($numRows,$numCols);
35
	}
36
37
	public function setWide($wide){
38
		$wide=Wide::getConstants()["W".$wide];
39
		$this->addToPropertyCtrl("class", $wide, Wide::getConstants());
40
		return $this->addToPropertyCtrl("class","column",array("column"));
41
	}
42
43
	public function addRow($colsCount=NULL){
44
		return $this->addItem($colsCount);
45
	}
46
47
	/**
48
	 * Create $rowsCount rows
49
	 * @param int $rowsCount
50
	 * @param int $colsCount
51
	 * @return \Ajax\semantic\html\collections\HtmlGrid
52
	 */
53
	public function setRowsCount($rowsCount,$colsCount=NULL){
54
		$count=$this->count();
55
		if($rowsCount<2){
56 View Code Duplication
			for($i=$count;$i<$colsCount;$i++){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57
				$this->addItem(new HtmlGridCol("col-".$this->identifier."-".$i));
58
			}
59
		}else{
60
			if($this->hasOnlyCols($count)){
61
				$tmpContent=$this->content;
62
				$item=$this->addItem($colsCount);
63
				$item->setContent($tmpContent);
64
				$this->content=array();
65
				$count=1;
66
			}
67
			for($i=$count;$i<$rowsCount;$i++){
68
				$this->addItem($colsCount);
69
			}
70
		}
71
		return $this;
72
	}
73
74
	protected function hasOnlyCols($count){
75
		return $count>0 && $this->content[0] instanceof HtmlGridCol;
76
	}
77
78
	public function setColsCount($numCols,$toCreate=true){
79
		$this->setWide($numCols);
80
		if($toCreate==true){
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
81
			$count=$this->count();
82
			if($count==0 || $this->hasOnlyCols($count)){
83 View Code Duplication
				for($i=$count;$i<$numCols;$i++){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
84
					$this->addItem(new HtmlGridCol("col-".$this->identifier."-".$i));
85
				}
86
			}else{
87
				for($i=0;$i<$count;$i++){
88
					$this->getItem($i)->setNumCols($numCols);
0 ignored issues
show
Bug introduced by
The method setNumCols() does not seem to exist on object<Ajax\common\html\HtmlDoubleElement>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
				}
90
			}
91
		}
92
		return $this;
93
	}
94
95
	/**
96
	 * return the row at $index
97
	 * @param int $index
98
	 * @return \Ajax\semantic\html\collections\HtmlGridRow
99
	 */
100
	public function getRow($index){
101
		return $this->getItem($index);
102
	}
103
104
	/**
105
	 * @param int $row
106
	 * @param int $col
107
	 * @return \Ajax\semantic\html\collections\HtmlGridCol
108
	 */
109
	public function getCell($row,$col){
110
		if($row<2 && $this->hasOnlyCols($this->count()))
111
			return $this->getItem($col);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->getItem($col); (Ajax\common\html\HtmlDoubleElement) is incompatible with the return type documented by Ajax\semantic\html\collections\HtmlGrid::getCell of type Ajax\semantic\html\collections\HtmlGridCol.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
112
		$row=$this->getItem($row);
113
		if(isset($row)){
114
			$col=$row->getItem($col);
115
		}
116
		return $col;
117
	}
118
119
	/**
120
	 * Adds dividers between columns ($vertically=false) or between rows ($vertically=true)
121
	 * @param boolean $vertically
122
	 * @return \Ajax\semantic\html\collections\HtmlGrid
123
	 */
124
	public function setDivided($vertically=false){
125
		$value=($vertically===true)?"vertically divided":"divided";
126
		return $this->addToPropertyCtrl("class", $value,array("divided"));
127
	}
128
129
	/**
130
	 * Divides rows into cells
131
	 * @param boolean $internally true for internal cells
132
	 * @return \Ajax\semantic\html\collections\HtmlGrid
133
	 */
134
	public function setCelled($internally=false){
135
		$value=($internally===true)?"internally celled":"celled";
136
		return $this->addToPropertyCtrl("class", $value,array("celled","internally celled"));
137
	}
138
139
	/**
140
	 * automatically resize all elements to split the available width evenly
141
	 * @return \Ajax\semantic\html\collections\HtmlGrid
142
	 */
143
	public function setEqualWidth(){
144
		return $this->addToProperty("class", "equal width");
145
	}
146
147
	/**
148
	 * Adds vertical or/and horizontal gutters
149
	 * @param string $value
150
	 * @return \Ajax\semantic\html\collections\HtmlGrid
151
	 */
152
	public function setPadded($value=NULL){
153
		if(isset($value))
154
			$this->addToPropertyCtrl("class", $value,array("vertically","horizontally"));
155
		return $this->addToProperty("class", "padded");
156
	}
157
158
	/**
159
	 * @param boolean $very
160
	 * @return \Ajax\semantic\html\collections\HtmlGrid
161
	 */
162
	public function setRelaxed($very=false){
163
		$value=($very===true)?"very relaxed":"relaxed";
164
		return $this->addToPropertyCtrl("class", $value,array("relaxed","very relaxed"));
165
	}
166
167
	public function setVerticalAlignment($value=VerticalAlignment::MIDDLE){
168
		return $this->addToPropertyCtrl("class", $value." aligned",VerticalAlignment::getConstantValues("aligned"));
169
	}
170
171
	/**
172
	 * {@inheritDoc}
173
	 * @see \Ajax\common\html\HtmlCollection::createItem()
174
	 */
175
	protected function createItem($value){
176
		if($this->_createCols===false)
177
			$value=null;
178
		$item=new HtmlGridRow($this->identifier."-row-".($this->count()+1),$value,$this->_colSizing,$this->_implicitRows);
179
		return $item;
180
	}
181
182
	public function setValues($values){
183
		$count=$this->count();
184
		if($this->_createCols===false){
185
			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...
186
				$colSize=\sizeof($values[$i]);
187
				$this->addItem(new HtmlGridRow($this->identifier."-row-".($this->count()+1),$colSize,$this->_colSizing,$this->_implicitRows));
188
			}
189
		}
190
		$count=\min(array($this->count(),\sizeof($values)));
191
		for($i=0;$i<$count;$i++){
192
			$this->content[$i]->setValues($values[$i],$this->_createCols===false);
193
		}
194
	}
195
196
	public function rowCount(){
197
		return $this->count();
198
	}
199
200
	public function colCount(){
201
		$result=0;
202
		$count=$this->count();
203
		if($count>0){
204
			if($this->content[0] instanceof HtmlGridCol)
205
				$result=$count;
206
			else
207
				$result=$this->content[0]->count();
208
		}
209
		return $result;
210
	}
211
212
	/**
213
	 * stretch the row contents to take up the entire column height
214
	 * @return \Ajax\semantic\html\content\HtmlGridRow
215
	 */
216
	public function setStretched(){
217
		return $this->addToProperty("class", "stretched");
218
	}
219
220
}