@@ -76,6 +76,7 @@ discard block |
||
| 76 | 76 | * Constructor. |
| 77 | 77 | * @param array|Iterator the initial data. Default is null, meaning no initialization. |
| 78 | 78 | * @param boolean whether the list is read-only. Always true for paged list. |
| 79 | + * @param integer[] $data |
|
| 79 | 80 | */ |
| 80 | 81 | public function __construct($data=null,$readOnly=false) |
| 81 | 82 | { |
@@ -92,6 +93,7 @@ discard block |
||
| 92 | 93 | |
| 93 | 94 | /** |
| 94 | 95 | * @param boolean whether to allow custom paging |
| 96 | + * @param boolean $value |
|
| 95 | 97 | */ |
| 96 | 98 | public function setCustomPaging($value) |
| 97 | 99 | { |
@@ -139,6 +141,7 @@ discard block |
||
| 139 | 141 | * Raises <b>OnPageIndexChanged</b> event. |
| 140 | 142 | * This event is raised each time when the list changes to a different page. |
| 141 | 143 | * @param TPagedListPageChangedEventParameter event parameter |
| 144 | + * @param TPagedListPageChangedEventParameter|null $param |
|
| 142 | 145 | */ |
| 143 | 146 | public function onPageIndexChanged($param) |
| 144 | 147 | { |
@@ -151,6 +154,7 @@ discard block |
||
| 151 | 154 | * and needs the new page of data. This event can only be raised when |
| 152 | 155 | * {@link setCustomPaging CustomPaging} is true. |
| 153 | 156 | * @param TPagedListFetchDataEventParameter event parameter |
| 157 | + * @param TPagedListFetchDataEventParameter $param |
|
| 154 | 158 | */ |
| 155 | 159 | public function onFetchData($param) |
| 156 | 160 | { |
@@ -316,6 +320,7 @@ discard block |
||
| 316 | 320 | |
| 317 | 321 | /** |
| 318 | 322 | * @param mixed the item |
| 323 | + * @param integer $item |
|
| 319 | 324 | * @return integer the index of the item in the list (0 based), -1 if not found. |
| 320 | 325 | */ |
| 321 | 326 | public function indexOf($item) |
@@ -380,6 +385,7 @@ discard block |
||
| 380 | 385 | /** |
| 381 | 386 | * Constructor. |
| 382 | 387 | * @param integer old page index |
| 388 | + * @param integer $oldPage |
|
| 383 | 389 | */ |
| 384 | 390 | public function __construct($oldPage) |
| 385 | 391 | { |
@@ -58,28 +58,28 @@ discard block |
||
| 58 | 58 | /** |
| 59 | 59 | * @var boolean whether to allow custom paging |
| 60 | 60 | */ |
| 61 | - private $_customPaging=false; |
|
| 61 | + private $_customPaging = false; |
|
| 62 | 62 | /** |
| 63 | 63 | * @var integer number of items in each page |
| 64 | 64 | */ |
| 65 | - private $_pageSize=10; |
|
| 65 | + private $_pageSize = 10; |
|
| 66 | 66 | /** |
| 67 | 67 | * @var integer current page index |
| 68 | 68 | */ |
| 69 | - private $_currentPageIndex=-1; |
|
| 69 | + private $_currentPageIndex = -1; |
|
| 70 | 70 | /** |
| 71 | 71 | * @var integer user-assigned number of items in data source |
| 72 | 72 | */ |
| 73 | - private $_virtualCount=-1; |
|
| 73 | + private $_virtualCount = -1; |
|
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * Constructor. |
| 77 | 77 | * @param array|Iterator the initial data. Default is null, meaning no initialization. |
| 78 | 78 | * @param boolean whether the list is read-only. Always true for paged list. |
| 79 | 79 | */ |
| 80 | - public function __construct($data=null,$readOnly=false) |
|
| 80 | + public function __construct($data = null, $readOnly = false) |
|
| 81 | 81 | { |
| 82 | - parent::__construct($data,true); |
|
| 82 | + parent::__construct($data, true); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -95,7 +95,7 @@ discard block |
||
| 95 | 95 | */ |
| 96 | 96 | public function setCustomPaging($value) |
| 97 | 97 | { |
| 98 | - $this->_customPaging=TPropertyValue::ensureBoolean($value); |
|
| 98 | + $this->_customPaging = TPropertyValue::ensureBoolean($value); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
@@ -111,8 +111,8 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | public function setPageSize($value) |
| 113 | 113 | { |
| 114 | - if(($value=TPropertyValue::ensureInteger($value))>0) |
|
| 115 | - $this->_pageSize=$value; |
|
| 114 | + if (($value = TPropertyValue::ensureInteger($value)) > 0) |
|
| 115 | + $this->_pageSize = $value; |
|
| 116 | 116 | else |
| 117 | 117 | throw new TInvalidDataValueException('pagedlist_pagesize_invalid'); |
| 118 | 118 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | */ |
| 132 | 132 | public function setCurrentPageIndex($value) |
| 133 | 133 | { |
| 134 | - if($this->gotoPage($value=TPropertyValue::ensureInteger($value))===false) |
|
| 134 | + if ($this->gotoPage($value = TPropertyValue::ensureInteger($value)) === false) |
|
| 135 | 135 | throw new TInvalidDataValueException('pagedlist_currentpageindex_invalid'); |
| 136 | 136 | } |
| 137 | 137 | |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | */ |
| 143 | 143 | public function onPageIndexChanged($param) |
| 144 | 144 | { |
| 145 | - $this->raiseEvent('OnPageIndexChanged',$this,$param); |
|
| 145 | + $this->raiseEvent('OnPageIndexChanged', $this, $param); |
|
| 146 | 146 | } |
| 147 | 147 | |
| 148 | 148 | /** |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | */ |
| 155 | 155 | public function onFetchData($param) |
| 156 | 156 | { |
| 157 | - $this->raiseEvent('OnFetchData',$this,$param); |
|
| 157 | + $this->raiseEvent('OnFetchData', $this, $param); |
|
| 158 | 158 | } |
| 159 | 159 | |
| 160 | 160 | /** |
@@ -164,21 +164,21 @@ discard block |
||
| 164 | 164 | */ |
| 165 | 165 | public function gotoPage($pageIndex) |
| 166 | 166 | { |
| 167 | - if($pageIndex===$this->_currentPageIndex) |
|
| 167 | + if ($pageIndex === $this->_currentPageIndex) |
|
| 168 | 168 | return $pageIndex; |
| 169 | - if($this->_customPaging) |
|
| 169 | + if ($this->_customPaging) |
|
| 170 | 170 | { |
| 171 | - if($pageIndex>=0 && ($this->_virtualCount<0 || $pageIndex<$this->getPageCount())) |
|
| 171 | + if ($pageIndex >= 0 && ($this->_virtualCount < 0 || $pageIndex < $this->getPageCount())) |
|
| 172 | 172 | { |
| 173 | - $param=new TPagedListFetchDataEventParameter($pageIndex,$this->_pageSize*$pageIndex,$this->_pageSize); |
|
| 173 | + $param = new TPagedListFetchDataEventParameter($pageIndex, $this->_pageSize * $pageIndex, $this->_pageSize); |
|
| 174 | 174 | $this->onFetchData($param); |
| 175 | - if(($data=$param->getData())!==null) |
|
| 175 | + if (($data = $param->getData()) !== null) |
|
| 176 | 176 | { |
| 177 | 177 | $this->setReadOnly(false); |
| 178 | 178 | $this->copyFrom($data); |
| 179 | 179 | $this->setReadOnly(true); |
| 180 | - $oldPage=$this->_currentPageIndex; |
|
| 181 | - $this->_currentPageIndex=$pageIndex; |
|
| 180 | + $oldPage = $this->_currentPageIndex; |
|
| 181 | + $this->_currentPageIndex = $pageIndex; |
|
| 182 | 182 | $this->onPageIndexChanged(new TPagedListPageChangedEventParameter($oldPage)); |
| 183 | 183 | return $pageIndex; |
| 184 | 184 | } |
@@ -190,9 +190,9 @@ discard block |
||
| 190 | 190 | } |
| 191 | 191 | else |
| 192 | 192 | { |
| 193 | - if($pageIndex>=0 && $pageIndex<$this->getPageCount()) |
|
| 193 | + if ($pageIndex >= 0 && $pageIndex < $this->getPageCount()) |
|
| 194 | 194 | { |
| 195 | - $this->_currentPageIndex=$pageIndex; |
|
| 195 | + $this->_currentPageIndex = $pageIndex; |
|
| 196 | 196 | $this->onPageIndexChanged(null); |
| 197 | 197 | return $pageIndex; |
| 198 | 198 | } |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | */ |
| 208 | 208 | public function nextPage() |
| 209 | 209 | { |
| 210 | - return $this->gotoPage($this->_currentPageIndex+1); |
|
| 210 | + return $this->gotoPage($this->_currentPageIndex + 1); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | /** |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | */ |
| 217 | 217 | public function previousPage() |
| 218 | 218 | { |
| 219 | - return $this->gotoPage($this->_currentPageIndex-1); |
|
| 219 | + return $this->gotoPage($this->_currentPageIndex - 1); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | /** |
@@ -232,9 +232,9 @@ discard block |
||
| 232 | 232 | */ |
| 233 | 233 | public function setVirtualCount($value) |
| 234 | 234 | { |
| 235 | - if(($value=TPropertyValue::ensureInteger($value))<0) |
|
| 236 | - $value=-1; |
|
| 237 | - $this->_virtualCount=$value; |
|
| 235 | + if (($value = TPropertyValue::ensureInteger($value)) < 0) |
|
| 236 | + $value = -1; |
|
| 237 | + $this->_virtualCount = $value; |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | /** |
@@ -242,15 +242,15 @@ discard block |
||
| 242 | 242 | */ |
| 243 | 243 | public function getPageCount() |
| 244 | 244 | { |
| 245 | - if($this->_customPaging) |
|
| 245 | + if ($this->_customPaging) |
|
| 246 | 246 | { |
| 247 | - if($this->_virtualCount>=0) |
|
| 248 | - return (int)(($this->_virtualCount+$this->_pageSize-1)/$this->_pageSize); |
|
| 247 | + if ($this->_virtualCount >= 0) |
|
| 248 | + return (int) (($this->_virtualCount + $this->_pageSize - 1) / $this->_pageSize); |
|
| 249 | 249 | else |
| 250 | 250 | return -1; |
| 251 | 251 | } |
| 252 | 252 | else |
| 253 | - return (int)((parent::getCount()+$this->_pageSize-1)/$this->_pageSize); |
|
| 253 | + return (int) ((parent::getCount() + $this->_pageSize - 1) / $this->_pageSize); |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | /** |
@@ -258,7 +258,7 @@ discard block |
||
| 258 | 258 | */ |
| 259 | 259 | public function getIsFirstPage() |
| 260 | 260 | { |
| 261 | - return $this->_currentPageIndex===0; |
|
| 261 | + return $this->_currentPageIndex === 0; |
|
| 262 | 262 | } |
| 263 | 263 | |
| 264 | 264 | /** |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | */ |
| 267 | 267 | public function getIsLastPage() |
| 268 | 268 | { |
| 269 | - return $this->_currentPageIndex===$this->getPageCount()-1; |
|
| 269 | + return $this->_currentPageIndex === $this->getPageCount() - 1; |
|
| 270 | 270 | } |
| 271 | 271 | |
| 272 | 272 | /** |
@@ -274,12 +274,12 @@ discard block |
||
| 274 | 274 | */ |
| 275 | 275 | public function getCount() |
| 276 | 276 | { |
| 277 | - if($this->_customPaging) |
|
| 277 | + if ($this->_customPaging) |
|
| 278 | 278 | return parent::getCount(); |
| 279 | 279 | else |
| 280 | 280 | { |
| 281 | - if($this->_currentPageIndex===$this->getPageCount()-1) |
|
| 282 | - return parent::getCount()-$this->_pageSize*$this->_currentPageIndex; |
|
| 281 | + if ($this->_currentPageIndex === $this->getPageCount() - 1) |
|
| 282 | + return parent::getCount() - $this->_pageSize * $this->_currentPageIndex; |
|
| 283 | 283 | else |
| 284 | 284 | return $this->_pageSize; |
| 285 | 285 | } |
@@ -290,11 +290,11 @@ discard block |
||
| 290 | 290 | */ |
| 291 | 291 | public function getIterator() |
| 292 | 292 | { |
| 293 | - if($this->_customPaging) |
|
| 293 | + if ($this->_customPaging) |
|
| 294 | 294 | return parent::getIterator(); |
| 295 | 295 | else |
| 296 | 296 | { |
| 297 | - $data=$this->toArray(); |
|
| 297 | + $data = $this->toArray(); |
|
| 298 | 298 | return new TListIterator($data); |
| 299 | 299 | } |
| 300 | 300 | } |
@@ -308,10 +308,10 @@ discard block |
||
| 308 | 308 | */ |
| 309 | 309 | public function itemAt($index) |
| 310 | 310 | { |
| 311 | - if($this->_customPaging) |
|
| 311 | + if ($this->_customPaging) |
|
| 312 | 312 | return parent::itemAt($index); |
| 313 | 313 | else |
| 314 | - return parent::itemAt($this->_pageSize*$this->_currentPageIndex+$index); |
|
| 314 | + return parent::itemAt($this->_pageSize * $this->_currentPageIndex + $index); |
|
| 315 | 315 | } |
| 316 | 316 | |
| 317 | 317 | /** |
@@ -320,9 +320,9 @@ discard block |
||
| 320 | 320 | */ |
| 321 | 321 | public function indexOf($item) |
| 322 | 322 | { |
| 323 | - $c=$this->getCount(); |
|
| 324 | - for($i=0;$i<$c;++$i) |
|
| 325 | - if($this->itemAt($i)===$item) |
|
| 323 | + $c = $this->getCount(); |
|
| 324 | + for ($i = 0; $i < $c; ++$i) |
|
| 325 | + if ($this->itemAt($i) === $item) |
|
| 326 | 326 | return $i; |
| 327 | 327 | return -1; |
| 328 | 328 | } |
@@ -335,7 +335,7 @@ discard block |
||
| 335 | 335 | */ |
| 336 | 336 | public function offsetExists($offset) |
| 337 | 337 | { |
| 338 | - return ($offset>=0 && $offset<$this->getCount()); |
|
| 338 | + return ($offset >= 0 && $offset < $this->getCount()); |
|
| 339 | 339 | } |
| 340 | 340 | |
| 341 | 341 | /** |
@@ -355,10 +355,10 @@ discard block |
||
| 355 | 355 | */ |
| 356 | 356 | public function toArray() |
| 357 | 357 | { |
| 358 | - $c=$this->getCount(); |
|
| 359 | - $array=array(); |
|
| 360 | - for($i=0;$i<$c;++$i) |
|
| 361 | - $array[$i]=$this->itemAt($i); |
|
| 358 | + $c = $this->getCount(); |
|
| 359 | + $array = array(); |
|
| 360 | + for ($i = 0; $i < $c; ++$i) |
|
| 361 | + $array[$i] = $this->itemAt($i); |
|
| 362 | 362 | return $array; |
| 363 | 363 | } |
| 364 | 364 | } |
@@ -383,7 +383,7 @@ discard block |
||
| 383 | 383 | */ |
| 384 | 384 | public function __construct($oldPage) |
| 385 | 385 | { |
| 386 | - $this->_oldPage=$oldPage; |
|
| 386 | + $this->_oldPage = $oldPage; |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | /** |
@@ -415,7 +415,7 @@ discard block |
||
| 415 | 415 | private $_pageIndex; |
| 416 | 416 | private $_offset; |
| 417 | 417 | private $_limit; |
| 418 | - private $_data=null; |
|
| 418 | + private $_data = null; |
|
| 419 | 419 | |
| 420 | 420 | /** |
| 421 | 421 | * Constructor. |
@@ -423,11 +423,11 @@ discard block |
||
| 423 | 423 | * @param integer offset of the first item in the new page |
| 424 | 424 | * @param integer number of items in the new page desired |
| 425 | 425 | */ |
| 426 | - public function __construct($pageIndex,$offset,$limit) |
|
| 426 | + public function __construct($pageIndex, $offset, $limit) |
|
| 427 | 427 | { |
| 428 | - $this->_pageIndex=$pageIndex; |
|
| 429 | - $this->_offset=$offset; |
|
| 430 | - $this->_limit=$limit; |
|
| 428 | + $this->_pageIndex = $pageIndex; |
|
| 429 | + $this->_offset = $offset; |
|
| 430 | + $this->_limit = $limit; |
|
| 431 | 431 | } |
| 432 | 432 | |
| 433 | 433 | /** |
@@ -467,7 +467,7 @@ discard block |
||
| 467 | 467 | */ |
| 468 | 468 | public function setData($value) |
| 469 | 469 | { |
| 470 | - $this->_data=$value; |
|
| 470 | + $this->_data = $value; |
|
| 471 | 471 | } |
| 472 | 472 | } |
| 473 | 473 | |
@@ -111,10 +111,11 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | public function setPageSize($value) |
| 113 | 113 | { |
| 114 | - if(($value=TPropertyValue::ensureInteger($value))>0) |
|
| 115 | - $this->_pageSize=$value; |
|
| 116 | - else |
|
| 117 | - throw new TInvalidDataValueException('pagedlist_pagesize_invalid'); |
|
| 114 | + if(($value=TPropertyValue::ensureInteger($value))>0) { |
|
| 115 | + $this->_pageSize=$value; |
|
| 116 | + } else { |
|
| 117 | + throw new TInvalidDataValueException('pagedlist_pagesize_invalid'); |
|
| 118 | + } |
|
| 118 | 119 | } |
| 119 | 120 | |
| 120 | 121 | /** |
@@ -131,8 +132,9 @@ discard block |
||
| 131 | 132 | */ |
| 132 | 133 | public function setCurrentPageIndex($value) |
| 133 | 134 | { |
| 134 | - if($this->gotoPage($value=TPropertyValue::ensureInteger($value))===false) |
|
| 135 | - throw new TInvalidDataValueException('pagedlist_currentpageindex_invalid'); |
|
| 135 | + if($this->gotoPage($value=TPropertyValue::ensureInteger($value))===false) { |
|
| 136 | + throw new TInvalidDataValueException('pagedlist_currentpageindex_invalid'); |
|
| 137 | + } |
|
| 136 | 138 | } |
| 137 | 139 | |
| 138 | 140 | /** |
@@ -164,8 +166,9 @@ discard block |
||
| 164 | 166 | */ |
| 165 | 167 | public function gotoPage($pageIndex) |
| 166 | 168 | { |
| 167 | - if($pageIndex===$this->_currentPageIndex) |
|
| 168 | - return $pageIndex; |
|
| 169 | + if($pageIndex===$this->_currentPageIndex) { |
|
| 170 | + return $pageIndex; |
|
| 171 | + } |
|
| 169 | 172 | if($this->_customPaging) |
| 170 | 173 | { |
| 171 | 174 | if($pageIndex>=0 && ($this->_virtualCount<0 || $pageIndex<$this->getPageCount())) |
@@ -181,23 +184,22 @@ discard block |
||
| 181 | 184 | $this->_currentPageIndex=$pageIndex; |
| 182 | 185 | $this->onPageIndexChanged(new TPagedListPageChangedEventParameter($oldPage)); |
| 183 | 186 | return $pageIndex; |
| 187 | + } else { |
|
| 188 | + return false; |
|
| 184 | 189 | } |
| 185 | - else |
|
| 186 | - return false; |
|
| 190 | + } else { |
|
| 191 | + return false; |
|
| 187 | 192 | } |
| 188 | - else |
|
| 189 | - return false; |
|
| 190 | - } |
|
| 191 | - else |
|
| 193 | + } else |
|
| 192 | 194 | { |
| 193 | 195 | if($pageIndex>=0 && $pageIndex<$this->getPageCount()) |
| 194 | 196 | { |
| 195 | 197 | $this->_currentPageIndex=$pageIndex; |
| 196 | 198 | $this->onPageIndexChanged(null); |
| 197 | 199 | return $pageIndex; |
| 200 | + } else { |
|
| 201 | + return false; |
|
| 198 | 202 | } |
| 199 | - else |
|
| 200 | - return false; |
|
| 201 | 203 | } |
| 202 | 204 | } |
| 203 | 205 | |
@@ -232,8 +234,9 @@ discard block |
||
| 232 | 234 | */ |
| 233 | 235 | public function setVirtualCount($value) |
| 234 | 236 | { |
| 235 | - if(($value=TPropertyValue::ensureInteger($value))<0) |
|
| 236 | - $value=-1; |
|
| 237 | + if(($value=TPropertyValue::ensureInteger($value))<0) { |
|
| 238 | + $value=-1; |
|
| 239 | + } |
|
| 237 | 240 | $this->_virtualCount=$value; |
| 238 | 241 | } |
| 239 | 242 | |
@@ -244,13 +247,14 @@ discard block |
||
| 244 | 247 | { |
| 245 | 248 | if($this->_customPaging) |
| 246 | 249 | { |
| 247 | - if($this->_virtualCount>=0) |
|
| 248 | - return (int)(($this->_virtualCount+$this->_pageSize-1)/$this->_pageSize); |
|
| 249 | - else |
|
| 250 | - return -1; |
|
| 250 | + if($this->_virtualCount>=0) { |
|
| 251 | + return (int)(($this->_virtualCount+$this->_pageSize-1)/$this->_pageSize); |
|
| 252 | + } else { |
|
| 253 | + return -1; |
|
| 254 | + } |
|
| 255 | + } else { |
|
| 256 | + return (int)((parent::getCount()+$this->_pageSize-1)/$this->_pageSize); |
|
| 251 | 257 | } |
| 252 | - else |
|
| 253 | - return (int)((parent::getCount()+$this->_pageSize-1)/$this->_pageSize); |
|
| 254 | 258 | } |
| 255 | 259 | |
| 256 | 260 | /** |
@@ -274,14 +278,15 @@ discard block |
||
| 274 | 278 | */ |
| 275 | 279 | public function getCount() |
| 276 | 280 | { |
| 277 | - if($this->_customPaging) |
|
| 278 | - return parent::getCount(); |
|
| 279 | - else |
|
| 281 | + if($this->_customPaging) { |
|
| 282 | + return parent::getCount(); |
|
| 283 | + } else |
|
| 280 | 284 | { |
| 281 | - if($this->_currentPageIndex===$this->getPageCount()-1) |
|
| 282 | - return parent::getCount()-$this->_pageSize*$this->_currentPageIndex; |
|
| 283 | - else |
|
| 284 | - return $this->_pageSize; |
|
| 285 | + if($this->_currentPageIndex===$this->getPageCount()-1) { |
|
| 286 | + return parent::getCount()-$this->_pageSize*$this->_currentPageIndex; |
|
| 287 | + } else { |
|
| 288 | + return $this->_pageSize; |
|
| 289 | + } |
|
| 285 | 290 | } |
| 286 | 291 | } |
| 287 | 292 | |
@@ -290,9 +295,9 @@ discard block |
||
| 290 | 295 | */ |
| 291 | 296 | public function getIterator() |
| 292 | 297 | { |
| 293 | - if($this->_customPaging) |
|
| 294 | - return parent::getIterator(); |
|
| 295 | - else |
|
| 298 | + if($this->_customPaging) { |
|
| 299 | + return parent::getIterator(); |
|
| 300 | + } else |
|
| 296 | 301 | { |
| 297 | 302 | $data=$this->toArray(); |
| 298 | 303 | return new TListIterator($data); |
@@ -308,10 +313,11 @@ discard block |
||
| 308 | 313 | */ |
| 309 | 314 | public function itemAt($index) |
| 310 | 315 | { |
| 311 | - if($this->_customPaging) |
|
| 312 | - return parent::itemAt($index); |
|
| 313 | - else |
|
| 314 | - return parent::itemAt($this->_pageSize*$this->_currentPageIndex+$index); |
|
| 316 | + if($this->_customPaging) { |
|
| 317 | + return parent::itemAt($index); |
|
| 318 | + } else { |
|
| 319 | + return parent::itemAt($this->_pageSize*$this->_currentPageIndex+$index); |
|
| 320 | + } |
|
| 315 | 321 | } |
| 316 | 322 | |
| 317 | 323 | /** |
@@ -321,9 +327,10 @@ discard block |
||
| 321 | 327 | public function indexOf($item) |
| 322 | 328 | { |
| 323 | 329 | $c=$this->getCount(); |
| 324 | - for($i=0;$i<$c;++$i) |
|
| 325 | - if($this->itemAt($i)===$item) |
|
| 330 | + for($i=0;$i<$c;++$i) { |
|
| 331 | + if($this->itemAt($i)===$item) |
|
| 326 | 332 | return $i; |
| 333 | + } |
|
| 327 | 334 | return -1; |
| 328 | 335 | } |
| 329 | 336 | |
@@ -357,8 +364,9 @@ discard block |
||
| 357 | 364 | { |
| 358 | 365 | $c=$this->getCount(); |
| 359 | 366 | $array=array(); |
| 360 | - for($i=0;$i<$c;++$i) |
|
| 361 | - $array[$i]=$this->itemAt($i); |
|
| 367 | + for($i=0;$i<$c;++$i) { |
|
| 368 | + $array[$i]=$this->itemAt($i); |
|
| 369 | + } |
|
| 362 | 370 | return $array; |
| 363 | 371 | } |
| 364 | 372 | } |
@@ -113,6 +113,7 @@ discard block |
||
| 113 | 113 | /** |
| 114 | 114 | * Gets the number of items at a priority within the list |
| 115 | 115 | * @param numeric optional priority at which to count items. if no parameter, it will be set to the default {@link getDefaultPriority} |
| 116 | + * @param integer $priority |
|
| 116 | 117 | * @return integer the number of items in the list at the specified priority |
| 117 | 118 | */ |
| 118 | 119 | public function getPriorityCount($priority=null) |
@@ -137,6 +138,7 @@ discard block |
||
| 137 | 138 | /** |
| 138 | 139 | * This must be called internally or when instantiated. |
| 139 | 140 | * @param numeric sets the default priority of inserted items without a specified priority |
| 141 | + * @param integer $value |
|
| 140 | 142 | */ |
| 141 | 143 | protected function setDefaultPriority($value) |
| 142 | 144 | { |
@@ -154,6 +156,7 @@ discard block |
||
| 154 | 156 | /** |
| 155 | 157 | * This must be called internally or when instantiated. |
| 156 | 158 | * @param integer The precision of numeric priorities. |
| 159 | + * @param integer $value |
|
| 157 | 160 | */ |
| 158 | 161 | protected function setPrecision($value) |
| 159 | 162 | { |
@@ -241,6 +244,7 @@ discard block |
||
| 241 | 244 | * Returns the item at an index within a priority |
| 242 | 245 | * @param integer the index into the list of items at priority |
| 243 | 246 | * @param numeric the priority which to index. no parameter or null will result in the default priority |
| 247 | + * @param integer $index |
|
| 244 | 248 | * @return mixed the element at the offset, false if no element is found at the offset |
| 245 | 249 | */ |
| 246 | 250 | public function itemAtIndexInPriority($index,$priority=null) |
@@ -275,6 +279,7 @@ discard block |
||
| 275 | 279 | * and then inserts the item at that priority-index. |
| 276 | 280 | * @param integer the specified position in the flattened list. |
| 277 | 281 | * @param mixed new item to add |
| 282 | + * @param integer $index |
|
| 278 | 283 | * @throws TInvalidDataValueException If the index specified exceeds the bound |
| 279 | 284 | * @throws TInvalidOperationException if the list is read-only |
| 280 | 285 | */ |
@@ -456,6 +461,7 @@ discard block |
||
| 456 | 461 | |
| 457 | 462 | /** |
| 458 | 463 | * @param mixed item |
| 464 | + * @param PriorityListItem $item |
|
| 459 | 465 | * @return boolean whether the list contains the item |
| 460 | 466 | */ |
| 461 | 467 | public function contains($item) |
@@ -590,6 +596,7 @@ discard block |
||
| 590 | 596 | * Combines the map elements which have a priority below the parameter value |
| 591 | 597 | * @param numeric the cut-off priority. All items of priority less than this are returned. |
| 592 | 598 | * @param boolean whether or not the input cut-off priority is inclusive. Default: false, not inclusive. |
| 599 | + * @param integer $priority |
|
| 593 | 600 | * @return array the array of priorities keys with values of arrays of items that are below a specified priority. |
| 594 | 601 | * The priorities are sorted so important priorities, lower numerics, are first. |
| 595 | 602 | */ |
@@ -610,6 +617,7 @@ discard block |
||
| 610 | 617 | * Combines the map elements which have a priority above the parameter value |
| 611 | 618 | * @param numeric the cut-off priority. All items of priority greater than this are returned. |
| 612 | 619 | * @param boolean whether or not the input cut-off priority is inclusive. Default: true, inclusive. |
| 620 | + * @param integer $priority |
|
| 613 | 621 | * @return array the array of priorities keys with values of arrays of items that are above a specified priority. |
| 614 | 622 | * The priorities are sorted so important priorities, lower numerics, are first. |
| 615 | 623 | */ |
@@ -714,6 +722,8 @@ discard block |
||
| 714 | 722 | * an item into a priority list doesn't mean the item is at the end of the list. |
| 715 | 723 | * @param integer the offset to set element |
| 716 | 724 | * @param mixed the element value |
| 725 | + * @param null|integer $offset |
|
| 726 | + * @param integer $item |
|
| 717 | 727 | */ |
| 718 | 728 | public function offsetSet($offset,$item) |
| 719 | 729 | { |
@@ -733,6 +743,7 @@ discard block |
||
| 733 | 743 | * Unsets the element at the specified offset. |
| 734 | 744 | * This method is required by the interface ArrayAccess. |
| 735 | 745 | * @param mixed the offset to unset element |
| 746 | + * @param integer $offset |
|
| 736 | 747 | */ |
| 737 | 748 | public function offsetUnset($offset) |
| 738 | 749 | { |
@@ -1,13 +1,13 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * TPriorityList, TPriorityListIterator classes |
|
| 4 | - * |
|
| 5 | - * @author Brad Anderson <[email protected]> |
|
| 6 | - * @link https://github.com/pradosoft/prado |
|
| 7 | - * @copyright Copyright © 2005-2015 The PRADO Group |
|
| 8 | - * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | - * @package System.Collections |
|
| 10 | - */ |
|
| 3 | + * TPriorityList, TPriorityListIterator classes |
|
| 4 | + * |
|
| 5 | + * @author Brad Anderson <[email protected]> |
|
| 6 | + * @link https://github.com/pradosoft/prado |
|
| 7 | + * @copyright Copyright © 2005-2015 The PRADO Group |
|
| 8 | + * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | + * @package System.Collections |
|
| 10 | + */ |
|
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * TPriorityList class |
@@ -50,27 +50,27 @@ discard block |
||
| 50 | 50 | /** |
| 51 | 51 | * @var array internal data storage |
| 52 | 52 | */ |
| 53 | - private $_d=array(); |
|
| 53 | + private $_d = array(); |
|
| 54 | 54 | /** |
| 55 | 55 | * @var boolean indicates if the _d is currently ordered. |
| 56 | 56 | */ |
| 57 | - private $_o=false; |
|
| 57 | + private $_o = false; |
|
| 58 | 58 | /** |
| 59 | 59 | * @var array cached flattened internal data storage |
| 60 | 60 | */ |
| 61 | - private $_fd=null; |
|
| 61 | + private $_fd = null; |
|
| 62 | 62 | /** |
| 63 | 63 | * @var integer number of items contain within the list |
| 64 | 64 | */ |
| 65 | - private $_c=0; |
|
| 65 | + private $_c = 0; |
|
| 66 | 66 | /** |
| 67 | 67 | * @var numeric the default priority of items without specified priorities |
| 68 | 68 | */ |
| 69 | - private $_dp=10; |
|
| 69 | + private $_dp = 10; |
|
| 70 | 70 | /** |
| 71 | 71 | * @var integer the precision of the numeric priorities within this priority list. |
| 72 | 72 | */ |
| 73 | - private $_p=8; |
|
| 73 | + private $_p = 8; |
|
| 74 | 74 | |
| 75 | 75 | /** |
| 76 | 76 | * Constructor. |
@@ -81,10 +81,10 @@ discard block |
||
| 81 | 81 | * @param integer the precision of the numeric priorities |
| 82 | 82 | * @throws TInvalidDataTypeException If data is not null and is neither an array nor an iterator. |
| 83 | 83 | */ |
| 84 | - public function __construct($data=null,$readOnly=false,$defaultPriority=10,$precision=8) |
|
| 84 | + public function __construct($data = null, $readOnly = false, $defaultPriority = 10, $precision = 8) |
|
| 85 | 85 | { |
| 86 | 86 | parent::__construct(); |
| 87 | - if($data!==null) |
|
| 87 | + if ($data !== null) |
|
| 88 | 88 | $this->copyFrom($data); |
| 89 | 89 | $this->setReadOnly($readOnly); |
| 90 | 90 | $this->setPrecision($precision); |
@@ -115,13 +115,13 @@ discard block |
||
| 115 | 115 | * @param numeric optional priority at which to count items. if no parameter, it will be set to the default {@link getDefaultPriority} |
| 116 | 116 | * @return integer the number of items in the list at the specified priority |
| 117 | 117 | */ |
| 118 | - public function getPriorityCount($priority=null) |
|
| 118 | + public function getPriorityCount($priority = null) |
|
| 119 | 119 | { |
| 120 | - if($priority===null) |
|
| 121 | - $priority=$this->getDefaultPriority(); |
|
| 122 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 120 | + if ($priority === null) |
|
| 121 | + $priority = $this->getDefaultPriority(); |
|
| 122 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 123 | 123 | |
| 124 | - if(!isset($this->_d[$priority]) || !is_array($this->_d[$priority])) |
|
| 124 | + if (!isset($this->_d[$priority]) || !is_array($this->_d[$priority])) |
|
| 125 | 125 | return false; |
| 126 | 126 | return count($this->_d[$priority]); |
| 127 | 127 | } |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | protected function setDefaultPriority($value) |
| 142 | 142 | { |
| 143 | - $this->_dp=(string)round(TPropertyValue::ensureFloat($value),$this->_p); |
|
| 143 | + $this->_dp = (string) round(TPropertyValue::ensureFloat($value), $this->_p); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | /** |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | */ |
| 158 | 158 | protected function setPrecision($value) |
| 159 | 159 | { |
| 160 | - $this->_p=TPropertyValue::ensureInteger($value); |
|
| 160 | + $this->_p = TPropertyValue::ensureInteger($value); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -185,9 +185,9 @@ discard block |
||
| 185 | 185 | * This orders the priority list internally. |
| 186 | 186 | */ |
| 187 | 187 | protected function sortPriorities() { |
| 188 | - if(!$this->_o) { |
|
| 189 | - ksort($this->_d,SORT_NUMERIC); |
|
| 190 | - $this->_o=true; |
|
| 188 | + if (!$this->_o) { |
|
| 189 | + ksort($this->_d, SORT_NUMERIC); |
|
| 190 | + $this->_o = true; |
|
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | 193 | |
@@ -196,13 +196,13 @@ discard block |
||
| 196 | 196 | * @return array array of items in the list in priority and index order |
| 197 | 197 | */ |
| 198 | 198 | protected function flattenPriorities() { |
| 199 | - if(is_array($this->_fd)) |
|
| 199 | + if (is_array($this->_fd)) |
|
| 200 | 200 | return $this->_fd; |
| 201 | 201 | |
| 202 | 202 | $this->sortPriorities(); |
| 203 | - $this->_fd=array(); |
|
| 204 | - foreach($this->_d as $priority => $itemsatpriority) |
|
| 205 | - $this->_fd=array_merge($this->_fd,$itemsatpriority); |
|
| 203 | + $this->_fd = array(); |
|
| 204 | + foreach ($this->_d as $priority => $itemsatpriority) |
|
| 205 | + $this->_fd = array_merge($this->_fd, $itemsatpriority); |
|
| 206 | 206 | return $this->_fd; |
| 207 | 207 | } |
| 208 | 208 | |
@@ -216,11 +216,11 @@ discard block |
||
| 216 | 216 | */ |
| 217 | 217 | public function itemAt($index) |
| 218 | 218 | { |
| 219 | - if($index>=0&&$index<$this->getCount()) { |
|
| 220 | - $arr=$this->flattenPriorities(); |
|
| 219 | + if ($index >= 0 && $index < $this->getCount()) { |
|
| 220 | + $arr = $this->flattenPriorities(); |
|
| 221 | 221 | return $arr[$index]; |
| 222 | 222 | } else |
| 223 | - throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 223 | + throw new TInvalidDataValueException('list_index_invalid', $index); |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | /** |
@@ -228,13 +228,13 @@ discard block |
||
| 228 | 228 | * @param numeric priority of the items to get. Defaults to null, filled in with the default priority, if left blank. |
| 229 | 229 | * @return array all items at priority in index order, null if there are no items at that priority |
| 230 | 230 | */ |
| 231 | - public function itemsAtPriority($priority=null) |
|
| 231 | + public function itemsAtPriority($priority = null) |
|
| 232 | 232 | { |
| 233 | - if($priority===null) |
|
| 234 | - $priority=$this->getDefaultPriority(); |
|
| 235 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 233 | + if ($priority === null) |
|
| 234 | + $priority = $this->getDefaultPriority(); |
|
| 235 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 236 | 236 | |
| 237 | - return isset($this->_d[$priority])?$this->_d[$priority]:null; |
|
| 237 | + return isset($this->_d[$priority]) ? $this->_d[$priority] : null; |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | /** |
@@ -243,14 +243,14 @@ discard block |
||
| 243 | 243 | * @param numeric the priority which to index. no parameter or null will result in the default priority |
| 244 | 244 | * @return mixed the element at the offset, false if no element is found at the offset |
| 245 | 245 | */ |
| 246 | - public function itemAtIndexInPriority($index,$priority=null) |
|
| 246 | + public function itemAtIndexInPriority($index, $priority = null) |
|
| 247 | 247 | { |
| 248 | - if($priority===null) |
|
| 249 | - $priority=$this->getDefaultPriority(); |
|
| 250 | - $priority=(string)round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 248 | + if ($priority === null) |
|
| 249 | + $priority = $this->getDefaultPriority(); |
|
| 250 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 251 | 251 | |
| 252 | - return !isset($this->_d[$priority])?false:( |
|
| 253 | - isset($this->_d[$priority][$index])?$this->_d[$priority][$index]:false |
|
| 252 | + return !isset($this->_d[$priority]) ? false : ( |
|
| 253 | + isset($this->_d[$priority][$index]) ? $this->_d[$priority][$index] : false |
|
| 254 | 254 | ); |
| 255 | 255 | } |
| 256 | 256 | |
@@ -262,12 +262,12 @@ discard block |
||
| 262 | 262 | * @return int the index within the flattened array |
| 263 | 263 | * @throws TInvalidOperationException if the map is read-only |
| 264 | 264 | */ |
| 265 | - public function add($item,$priority=null) |
|
| 265 | + public function add($item, $priority = null) |
|
| 266 | 266 | { |
| 267 | - if($this->getReadOnly()) |
|
| 268 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 267 | + if ($this->getReadOnly()) |
|
| 268 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 269 | 269 | |
| 270 | - return $this->insertAtIndexInPriority($item,false,$priority,true); |
|
| 270 | + return $this->insertAtIndexInPriority($item, false, $priority, true); |
|
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | /** |
@@ -278,15 +278,15 @@ discard block |
||
| 278 | 278 | * @throws TInvalidDataValueException If the index specified exceeds the bound |
| 279 | 279 | * @throws TInvalidOperationException if the list is read-only |
| 280 | 280 | */ |
| 281 | - public function insertAt($index,$item) |
|
| 281 | + public function insertAt($index, $item) |
|
| 282 | 282 | { |
| 283 | - if($this->getReadOnly()) |
|
| 284 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 283 | + if ($this->getReadOnly()) |
|
| 284 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 285 | 285 | |
| 286 | - if(($priority=$this->priorityAt($index,true))!==false) |
|
| 287 | - $this->insertAtIndexInPriority($item,$priority[1],$priority[0]); |
|
| 286 | + if (($priority = $this->priorityAt($index, true)) !== false) |
|
| 287 | + $this->insertAtIndexInPriority($item, $priority[1], $priority[0]); |
|
| 288 | 288 | else |
| 289 | - throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 289 | + throw new TInvalidDataValueException('list_index_invalid', $index); |
|
| 290 | 290 | } |
| 291 | 291 | |
| 292 | 292 | /** |
@@ -299,56 +299,56 @@ discard block |
||
| 299 | 299 | * @throws TInvalidDataValueException If the index specified exceeds the bound |
| 300 | 300 | * @throws TInvalidOperationException if the list is read-only |
| 301 | 301 | */ |
| 302 | - public function insertAtIndexInPriority($item,$index=false,$priority=null,$preserveCache=false) |
|
| 302 | + public function insertAtIndexInPriority($item, $index = false, $priority = null, $preserveCache = false) |
|
| 303 | 303 | { |
| 304 | - if($this->getReadOnly()) |
|
| 305 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 304 | + if ($this->getReadOnly()) |
|
| 305 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 306 | 306 | |
| 307 | - if($priority===null) |
|
| 308 | - $priority=$this->getDefaultPriority(); |
|
| 309 | - $priority=(string)round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 307 | + if ($priority === null) |
|
| 308 | + $priority = $this->getDefaultPriority(); |
|
| 309 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 310 | 310 | |
| 311 | - if($preserveCache) { |
|
| 311 | + if ($preserveCache) { |
|
| 312 | 312 | $this->sortPriorities(); |
| 313 | - $cc=0; |
|
| 314 | - foreach($this->_d as $prioritykey=>$items) |
|
| 315 | - if($prioritykey>=$priority) |
|
| 313 | + $cc = 0; |
|
| 314 | + foreach ($this->_d as $prioritykey=>$items) |
|
| 315 | + if ($prioritykey >= $priority) |
|
| 316 | 316 | break; |
| 317 | 317 | else |
| 318 | - $cc+=count($items); |
|
| 319 | - |
|
| 320 | - if($index===false&&isset($this->_d[$priority])) { |
|
| 321 | - $c=count($this->_d[$priority]); |
|
| 322 | - $c+=$cc; |
|
| 323 | - $this->_d[$priority][]=$item; |
|
| 324 | - } else if(isset($this->_d[$priority])) { |
|
| 325 | - $c=$index+$cc; |
|
| 326 | - array_splice($this->_d[$priority],$index,0,array($item)); |
|
| 318 | + $cc += count($items); |
|
| 319 | + |
|
| 320 | + if ($index === false && isset($this->_d[$priority])) { |
|
| 321 | + $c = count($this->_d[$priority]); |
|
| 322 | + $c += $cc; |
|
| 323 | + $this->_d[$priority][] = $item; |
|
| 324 | + } else if (isset($this->_d[$priority])) { |
|
| 325 | + $c = $index + $cc; |
|
| 326 | + array_splice($this->_d[$priority], $index, 0, array($item)); |
|
| 327 | 327 | } else { |
| 328 | 328 | $c = $cc; |
| 329 | 329 | $this->_o = false; |
| 330 | - $this->_d[$priority]=array($item); |
|
| 330 | + $this->_d[$priority] = array($item); |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | - if($this->_fd&&is_array($this->_fd)) // if there is a flattened array cache |
|
| 334 | - array_splice($this->_fd,$c,0,array($item)); |
|
| 333 | + if ($this->_fd && is_array($this->_fd)) // if there is a flattened array cache |
|
| 334 | + array_splice($this->_fd, $c, 0, array($item)); |
|
| 335 | 335 | } else { |
| 336 | - $c=null; |
|
| 337 | - if($index===false&&isset($this->_d[$priority])) { |
|
| 338 | - $cc=count($this->_d[$priority]); |
|
| 339 | - $this->_d[$priority][]=$item; |
|
| 340 | - } else if(isset($this->_d[$priority])) { |
|
| 341 | - $cc=$index; |
|
| 342 | - array_splice($this->_d[$priority],$index,0,array($item)); |
|
| 336 | + $c = null; |
|
| 337 | + if ($index === false && isset($this->_d[$priority])) { |
|
| 338 | + $cc = count($this->_d[$priority]); |
|
| 339 | + $this->_d[$priority][] = $item; |
|
| 340 | + } else if (isset($this->_d[$priority])) { |
|
| 341 | + $cc = $index; |
|
| 342 | + array_splice($this->_d[$priority], $index, 0, array($item)); |
|
| 343 | 343 | } else { |
| 344 | - $cc=0; |
|
| 345 | - $this->_o=false; |
|
| 346 | - $this->_d[$priority]=array($item); |
|
| 344 | + $cc = 0; |
|
| 345 | + $this->_o = false; |
|
| 346 | + $this->_d[$priority] = array($item); |
|
| 347 | 347 | } |
| 348 | - if($this->_fd&&is_array($this->_fd)&&count($this->_d)==1) |
|
| 349 | - array_splice($this->_fd,$cc,0,array($item)); |
|
| 348 | + if ($this->_fd && is_array($this->_fd) && count($this->_d) == 1) |
|
| 349 | + array_splice($this->_fd, $cc, 0, array($item)); |
|
| 350 | 350 | else |
| 351 | - $this->_fd=null; |
|
| 351 | + $this->_fd = null; |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | $this->_c++; |
@@ -367,22 +367,22 @@ discard block |
||
| 367 | 367 | * @return integer index within the flattened list at which the item is being removed |
| 368 | 368 | * @throws TInvalidDataValueException If the item does not exist |
| 369 | 369 | */ |
| 370 | - public function remove($item,$priority=false) |
|
| 370 | + public function remove($item, $priority = false) |
|
| 371 | 371 | { |
| 372 | - if($this->getReadOnly()) |
|
| 373 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 372 | + if ($this->getReadOnly()) |
|
| 373 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 374 | 374 | |
| 375 | - if(($p=$this->priorityOf($item,true))!==false) |
|
| 375 | + if (($p = $this->priorityOf($item, true)) !== false) |
|
| 376 | 376 | { |
| 377 | - if($priority!==false) { |
|
| 378 | - if($priority===null) |
|
| 379 | - $priority=$this->getDefaultPriority(); |
|
| 380 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 377 | + if ($priority !== false) { |
|
| 378 | + if ($priority === null) |
|
| 379 | + $priority = $this->getDefaultPriority(); |
|
| 380 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 381 | 381 | |
| 382 | - if($p[0]!=$priority) |
|
| 382 | + if ($p[0] != $priority) |
|
| 383 | 383 | throw new TInvalidDataValueException('list_item_inexistent'); |
| 384 | 384 | } |
| 385 | - $this->removeAtIndexInPriority($p[1],$p[0]); |
|
| 385 | + $this->removeAtIndexInPriority($p[1], $p[0]); |
|
| 386 | 386 | return $p[2]; |
| 387 | 387 | } |
| 388 | 388 | else |
@@ -398,12 +398,12 @@ discard block |
||
| 398 | 398 | */ |
| 399 | 399 | public function removeAt($index) |
| 400 | 400 | { |
| 401 | - if($this->getReadOnly()) |
|
| 402 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 401 | + if ($this->getReadOnly()) |
|
| 402 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 403 | 403 | |
| 404 | - if(($priority=$this->priorityAt($index, true))!==false) |
|
| 405 | - return $this->removeAtIndexInPriority($priority[1],$priority[0]); |
|
| 406 | - throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 404 | + if (($priority = $this->priorityAt($index, true)) !== false) |
|
| 405 | + return $this->removeAtIndexInPriority($priority[1], $priority[0]); |
|
| 406 | + throw new TInvalidDataValueException('list_index_invalid', $index); |
|
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | /** |
@@ -414,27 +414,27 @@ discard block |
||
| 414 | 414 | * @return mixed the removed item. |
| 415 | 415 | * @throws TInvalidDataValueException If the item does not exist |
| 416 | 416 | */ |
| 417 | - public function removeAtIndexInPriority($index, $priority=null) |
|
| 417 | + public function removeAtIndexInPriority($index, $priority = null) |
|
| 418 | 418 | { |
| 419 | - if($this->getReadOnly()) |
|
| 420 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 419 | + if ($this->getReadOnly()) |
|
| 420 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 421 | 421 | |
| 422 | - if($priority===null) |
|
| 423 | - $priority=$this->getDefaultPriority(); |
|
| 424 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 422 | + if ($priority === null) |
|
| 423 | + $priority = $this->getDefaultPriority(); |
|
| 424 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 425 | 425 | |
| 426 | - if(!isset($this->_d[$priority])||$index<0||$index>=count($this->_d[$priority])) |
|
| 426 | + if (!isset($this->_d[$priority]) || $index < 0 || $index >= count($this->_d[$priority])) |
|
| 427 | 427 | throw new TInvalidDataValueException('list_item_inexistent'); |
| 428 | 428 | |
| 429 | 429 | // $value is an array of elements removed, only one |
| 430 | - $value=array_splice($this->_d[$priority],$index,1); |
|
| 431 | - $value=$value[0]; |
|
| 430 | + $value = array_splice($this->_d[$priority], $index, 1); |
|
| 431 | + $value = $value[0]; |
|
| 432 | 432 | |
| 433 | - if(!count($this->_d[$priority])) |
|
| 433 | + if (!count($this->_d[$priority])) |
|
| 434 | 434 | unset($this->_d[$priority]); |
| 435 | 435 | |
| 436 | 436 | $this->_c--; |
| 437 | - $this->_fd=null; |
|
| 437 | + $this->_fd = null; |
|
| 438 | 438 | return $value; |
| 439 | 439 | } |
| 440 | 440 | |
@@ -443,13 +443,13 @@ discard block |
||
| 443 | 443 | */ |
| 444 | 444 | public function clear() |
| 445 | 445 | { |
| 446 | - if($this->getReadOnly()) |
|
| 447 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 446 | + if ($this->getReadOnly()) |
|
| 447 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 448 | 448 | |
| 449 | - $d=array_reverse($this->_d,true); |
|
| 450 | - foreach($this->_d as $priority=>$items) { |
|
| 451 | - for($index=count($items)-1;$index>=0;$index--) |
|
| 452 | - $this->removeAtIndexInPriority($index,$priority); |
|
| 449 | + $d = array_reverse($this->_d, true); |
|
| 450 | + foreach ($this->_d as $priority=>$items) { |
|
| 451 | + for ($index = count($items) - 1; $index >= 0; $index--) |
|
| 452 | + $this->removeAtIndexInPriority($index, $priority); |
|
| 453 | 453 | unset($this->_d[$priority]); |
| 454 | 454 | } |
| 455 | 455 | } |
@@ -460,7 +460,7 @@ discard block |
||
| 460 | 460 | */ |
| 461 | 461 | public function contains($item) |
| 462 | 462 | { |
| 463 | - return $this->indexOf($item)>=0; |
|
| 463 | + return $this->indexOf($item) >= 0; |
|
| 464 | 464 | } |
| 465 | 465 | |
| 466 | 466 | /** |
@@ -469,7 +469,7 @@ discard block |
||
| 469 | 469 | */ |
| 470 | 470 | public function indexOf($item) |
| 471 | 471 | { |
| 472 | - if(($index=array_search($item,$this->flattenPriorities(),true))===false) |
|
| 472 | + if (($index = array_search($item, $this->flattenPriorities(), true)) === false) |
|
| 473 | 473 | return -1; |
| 474 | 474 | else |
| 475 | 475 | return $index; |
@@ -484,18 +484,18 @@ discard block |
||
| 484 | 484 | * if withindex is true, an array is returned of [0 => $priority, 1 => $priorityIndex, 2 => flattenedIndex, |
| 485 | 485 | * 'priority' => $priority, 'index' => $priorityIndex, 'absindex' => flattenedIndex] |
| 486 | 486 | */ |
| 487 | - public function priorityOf($item,$withindex = false) |
|
| 487 | + public function priorityOf($item, $withindex = false) |
|
| 488 | 488 | { |
| 489 | 489 | $this->sortPriorities(); |
| 490 | 490 | |
| 491 | 491 | $absindex = 0; |
| 492 | - foreach($this->_d as $priority=>$items) { |
|
| 493 | - if(($index=array_search($item,$items,true))!==false) { |
|
| 494 | - $absindex+=$index; |
|
| 495 | - return $withindex?array($priority,$index,$absindex, |
|
| 496 | - 'priority'=>$priority,'index'=>$index,'absindex'=>$absindex):$priority; |
|
| 492 | + foreach ($this->_d as $priority=>$items) { |
|
| 493 | + if (($index = array_search($item, $items, true)) !== false) { |
|
| 494 | + $absindex += $index; |
|
| 495 | + return $withindex ? array($priority, $index, $absindex, |
|
| 496 | + 'priority'=>$priority, 'index'=>$index, 'absindex'=>$absindex) : $priority; |
|
| 497 | 497 | } else |
| 498 | - $absindex+=count($items); |
|
| 498 | + $absindex += count($items); |
|
| 499 | 499 | } |
| 500 | 500 | |
| 501 | 501 | return false; |
@@ -510,19 +510,19 @@ discard block |
||
| 510 | 510 | * if withindex is true, an array is returned of [0 => $priority, 1 => $priorityIndex, 2 => flattenedIndex, |
| 511 | 511 | * 'priority' => $priority, 'index' => $priorityIndex, 'absindex' => flattenedIndex] |
| 512 | 512 | */ |
| 513 | - public function priorityAt($index,$withindex = false) |
|
| 513 | + public function priorityAt($index, $withindex = false) |
|
| 514 | 514 | { |
| 515 | - if($index<0||$index>=$this->getCount()) |
|
| 516 | - throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 515 | + if ($index < 0 || $index >= $this->getCount()) |
|
| 516 | + throw new TInvalidDataValueException('list_index_invalid', $index); |
|
| 517 | 517 | |
| 518 | - $absindex=$index; |
|
| 518 | + $absindex = $index; |
|
| 519 | 519 | $this->sortPriorities(); |
| 520 | - foreach($this->_d as $priority=>$items) { |
|
| 521 | - if($index>=($c=count($items))) |
|
| 522 | - $index-=$c; |
|
| 520 | + foreach ($this->_d as $priority=>$items) { |
|
| 521 | + if ($index >= ($c = count($items))) |
|
| 522 | + $index -= $c; |
|
| 523 | 523 | else |
| 524 | - return $withindex?array($priority,$index,$absindex, |
|
| 525 | - 'priority'=>$priority,'index'=>$index,'absindex'=>$absindex):$priority; |
|
| 524 | + return $withindex ? array($priority, $index, $absindex, |
|
| 525 | + 'priority'=>$priority, 'index'=>$index, 'absindex'=>$absindex) : $priority; |
|
| 526 | 526 | } |
| 527 | 527 | return false; |
| 528 | 528 | } |
@@ -537,13 +537,13 @@ discard block |
||
| 537 | 537 | */ |
| 538 | 538 | public function insertBefore($indexitem, $item) |
| 539 | 539 | { |
| 540 | - if($this->getReadOnly()) |
|
| 541 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 540 | + if ($this->getReadOnly()) |
|
| 541 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 542 | 542 | |
| 543 | - if(($priority=$this->priorityOf($indexitem,true))===false) |
|
| 543 | + if (($priority = $this->priorityOf($indexitem, true)) === false) |
|
| 544 | 544 | throw new TInvalidDataValueException('list_item_inexistent'); |
| 545 | 545 | |
| 546 | - $this->insertAtIndexInPriority($item,$priority[1],$priority[0]); |
|
| 546 | + $this->insertAtIndexInPriority($item, $priority[1], $priority[0]); |
|
| 547 | 547 | |
| 548 | 548 | return $priority[2]; |
| 549 | 549 | } |
@@ -558,15 +558,15 @@ discard block |
||
| 558 | 558 | */ |
| 559 | 559 | public function insertAfter($indexitem, $item) |
| 560 | 560 | { |
| 561 | - if($this->getReadOnly()) |
|
| 562 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 561 | + if ($this->getReadOnly()) |
|
| 562 | + throw new TInvalidOperationException('list_readonly', get_class($this)); |
|
| 563 | 563 | |
| 564 | - if(($priority=$this->priorityOf($indexitem,true))===false) |
|
| 564 | + if (($priority = $this->priorityOf($indexitem, true)) === false) |
|
| 565 | 565 | throw new TInvalidDataValueException('list_item_inexistent'); |
| 566 | 566 | |
| 567 | - $this->insertAtIndexInPriority($item,$priority[1]+1,$priority[0]); |
|
| 567 | + $this->insertAtIndexInPriority($item, $priority[1] + 1, $priority[0]); |
|
| 568 | 568 | |
| 569 | - return $priority[2]+1; |
|
| 569 | + return $priority[2] + 1; |
|
| 570 | 570 | } |
| 571 | 571 | |
| 572 | 572 | /** |
@@ -593,15 +593,15 @@ discard block |
||
| 593 | 593 | * @return array the array of priorities keys with values of arrays of items that are below a specified priority. |
| 594 | 594 | * The priorities are sorted so important priorities, lower numerics, are first. |
| 595 | 595 | */ |
| 596 | - public function toArrayBelowPriority($priority,$inclusive=false) |
|
| 596 | + public function toArrayBelowPriority($priority, $inclusive = false) |
|
| 597 | 597 | { |
| 598 | 598 | $this->sortPriorities(); |
| 599 | - $items=array(); |
|
| 600 | - foreach($this->_d as $itemspriority=>$itemsatpriority) |
|
| 599 | + $items = array(); |
|
| 600 | + foreach ($this->_d as $itemspriority=>$itemsatpriority) |
|
| 601 | 601 | { |
| 602 | - if((!$inclusive&&$itemspriority>=$priority)||$itemspriority>$priority) |
|
| 602 | + if ((!$inclusive && $itemspriority >= $priority) || $itemspriority > $priority) |
|
| 603 | 603 | break; |
| 604 | - $items=array_merge($items,$itemsatpriority); |
|
| 604 | + $items = array_merge($items, $itemsatpriority); |
|
| 605 | 605 | } |
| 606 | 606 | return $items; |
| 607 | 607 | } |
@@ -613,15 +613,15 @@ discard block |
||
| 613 | 613 | * @return array the array of priorities keys with values of arrays of items that are above a specified priority. |
| 614 | 614 | * The priorities are sorted so important priorities, lower numerics, are first. |
| 615 | 615 | */ |
| 616 | - public function toArrayAbovePriority($priority,$inclusive=true) |
|
| 616 | + public function toArrayAbovePriority($priority, $inclusive = true) |
|
| 617 | 617 | { |
| 618 | 618 | $this->sortPriorities(); |
| 619 | - $items=array(); |
|
| 620 | - foreach($this->_d as $itemspriority=>$itemsatpriority) |
|
| 619 | + $items = array(); |
|
| 620 | + foreach ($this->_d as $itemspriority=>$itemsatpriority) |
|
| 621 | 621 | { |
| 622 | - if((!$inclusive&&$itemspriority<=$priority)||$itemspriority<$priority) |
|
| 622 | + if ((!$inclusive && $itemspriority <= $priority) || $itemspriority < $priority) |
|
| 623 | 623 | continue; |
| 624 | - $items=array_merge($items,$itemsatpriority); |
|
| 624 | + $items = array_merge($items, $itemsatpriority); |
|
| 625 | 625 | } |
| 626 | 626 | return $items; |
| 627 | 627 | } |
@@ -635,21 +635,21 @@ discard block |
||
| 635 | 635 | */ |
| 636 | 636 | public function copyFrom($data) |
| 637 | 637 | { |
| 638 | - if($data instanceof TPriorityList) |
|
| 638 | + if ($data instanceof TPriorityList) |
|
| 639 | 639 | { |
| 640 | - if($this->getCount()>0) |
|
| 640 | + if ($this->getCount() > 0) |
|
| 641 | 641 | $this->clear(); |
| 642 | - foreach($data->getPriorities() as $priority) |
|
| 642 | + foreach ($data->getPriorities() as $priority) |
|
| 643 | 643 | { |
| 644 | - foreach($data->itemsAtPriority($priority) as $index=>$item) |
|
| 645 | - $this->insertAtIndexInPriority($item,$index,$priority); |
|
| 644 | + foreach ($data->itemsAtPriority($priority) as $index=>$item) |
|
| 645 | + $this->insertAtIndexInPriority($item, $index, $priority); |
|
| 646 | 646 | } |
| 647 | - } else if(is_array($data)||$data instanceof Traversable) { |
|
| 648 | - if($this->getCount()>0) |
|
| 647 | + } else if (is_array($data) || $data instanceof Traversable) { |
|
| 648 | + if ($this->getCount() > 0) |
|
| 649 | 649 | $this->clear(); |
| 650 | - foreach($data as $key=>$item) |
|
| 650 | + foreach ($data as $key=>$item) |
|
| 651 | 651 | $this->add($item); |
| 652 | - } else if($data!==null) |
|
| 652 | + } else if ($data !== null) |
|
| 653 | 653 | throw new TInvalidDataTypeException('map_data_not_iterable'); |
| 654 | 654 | } |
| 655 | 655 | |
@@ -663,21 +663,21 @@ discard block |
||
| 663 | 663 | */ |
| 664 | 664 | public function mergeWith($data) |
| 665 | 665 | { |
| 666 | - if($data instanceof TPriorityList) |
|
| 666 | + if ($data instanceof TPriorityList) |
|
| 667 | 667 | { |
| 668 | - foreach($data->getPriorities() as $priority) |
|
| 668 | + foreach ($data->getPriorities() as $priority) |
|
| 669 | 669 | { |
| 670 | - foreach($data->itemsAtPriority($priority) as $index=>$item) |
|
| 671 | - $this->insertAtIndexInPriority($item,false,$priority); |
|
| 670 | + foreach ($data->itemsAtPriority($priority) as $index=>$item) |
|
| 671 | + $this->insertAtIndexInPriority($item, false, $priority); |
|
| 672 | 672 | } |
| 673 | 673 | } |
| 674 | - else if(is_array($data)||$data instanceof Traversable) |
|
| 674 | + else if (is_array($data) || $data instanceof Traversable) |
|
| 675 | 675 | { |
| 676 | - foreach($data as $priority=>$item) |
|
| 676 | + foreach ($data as $priority=>$item) |
|
| 677 | 677 | $this->add($item); |
| 678 | 678 | |
| 679 | 679 | } |
| 680 | - else if($data!==null) |
|
| 680 | + else if ($data !== null) |
|
| 681 | 681 | throw new TInvalidDataTypeException('map_data_not_iterable'); |
| 682 | 682 | } |
| 683 | 683 | |
@@ -689,7 +689,7 @@ discard block |
||
| 689 | 689 | */ |
| 690 | 690 | public function offsetExists($offset) |
| 691 | 691 | { |
| 692 | - return ($offset>=0&&$offset<$this->getCount()); |
|
| 692 | + return ($offset >= 0 && $offset < $this->getCount()); |
|
| 693 | 693 | } |
| 694 | 694 | |
| 695 | 695 | /** |
@@ -715,18 +715,18 @@ discard block |
||
| 715 | 715 | * @param integer the offset to set element |
| 716 | 716 | * @param mixed the element value |
| 717 | 717 | */ |
| 718 | - public function offsetSet($offset,$item) |
|
| 718 | + public function offsetSet($offset, $item) |
|
| 719 | 719 | { |
| 720 | - if($offset===null) |
|
| 720 | + if ($offset === null) |
|
| 721 | 721 | return $this->add($item); |
| 722 | - if($offset===$this->getCount()) { |
|
| 723 | - $priority=$this->priorityAt($offset-1,true); |
|
| 722 | + if ($offset === $this->getCount()) { |
|
| 723 | + $priority = $this->priorityAt($offset - 1, true); |
|
| 724 | 724 | $priority[1]++; |
| 725 | 725 | } else { |
| 726 | - $priority=$this->priorityAt($offset,true); |
|
| 727 | - $this->removeAtIndexInPriority($priority[1],$priority[0]); |
|
| 726 | + $priority = $this->priorityAt($offset, true); |
|
| 727 | + $this->removeAtIndexInPriority($priority[1], $priority[0]); |
|
| 728 | 728 | } |
| 729 | - $this->insertAtIndexInPriority($item,$priority[1],$priority[0]); |
|
| 729 | + $this->insertAtIndexInPriority($item, $priority[1], $priority[0]); |
|
| 730 | 730 | } |
| 731 | 731 | |
| 732 | 732 | /** |
@@ -84,8 +84,9 @@ discard block |
||
| 84 | 84 | public function __construct($data=null,$readOnly=false,$defaultPriority=10,$precision=8) |
| 85 | 85 | { |
| 86 | 86 | parent::__construct(); |
| 87 | - if($data!==null) |
|
| 88 | - $this->copyFrom($data); |
|
| 87 | + if($data!==null) { |
|
| 88 | + $this->copyFrom($data); |
|
| 89 | + } |
|
| 89 | 90 | $this->setReadOnly($readOnly); |
| 90 | 91 | $this->setPrecision($precision); |
| 91 | 92 | $this->setDefaultPriority($defaultPriority); |
@@ -117,12 +118,14 @@ discard block |
||
| 117 | 118 | */ |
| 118 | 119 | public function getPriorityCount($priority=null) |
| 119 | 120 | { |
| 120 | - if($priority===null) |
|
| 121 | - $priority=$this->getDefaultPriority(); |
|
| 121 | + if($priority===null) { |
|
| 122 | + $priority=$this->getDefaultPriority(); |
|
| 123 | + } |
|
| 122 | 124 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 123 | 125 | |
| 124 | - if(!isset($this->_d[$priority]) || !is_array($this->_d[$priority])) |
|
| 125 | - return false; |
|
| 126 | + if(!isset($this->_d[$priority]) || !is_array($this->_d[$priority])) { |
|
| 127 | + return false; |
|
| 128 | + } |
|
| 126 | 129 | return count($this->_d[$priority]); |
| 127 | 130 | } |
| 128 | 131 | |
@@ -196,13 +199,15 @@ discard block |
||
| 196 | 199 | * @return array array of items in the list in priority and index order |
| 197 | 200 | */ |
| 198 | 201 | protected function flattenPriorities() { |
| 199 | - if(is_array($this->_fd)) |
|
| 200 | - return $this->_fd; |
|
| 202 | + if(is_array($this->_fd)) { |
|
| 203 | + return $this->_fd; |
|
| 204 | + } |
|
| 201 | 205 | |
| 202 | 206 | $this->sortPriorities(); |
| 203 | 207 | $this->_fd=array(); |
| 204 | - foreach($this->_d as $priority => $itemsatpriority) |
|
| 205 | - $this->_fd=array_merge($this->_fd,$itemsatpriority); |
|
| 208 | + foreach($this->_d as $priority => $itemsatpriority) { |
|
| 209 | + $this->_fd=array_merge($this->_fd,$itemsatpriority); |
|
| 210 | + } |
|
| 206 | 211 | return $this->_fd; |
| 207 | 212 | } |
| 208 | 213 | |
@@ -219,8 +224,9 @@ discard block |
||
| 219 | 224 | if($index>=0&&$index<$this->getCount()) { |
| 220 | 225 | $arr=$this->flattenPriorities(); |
| 221 | 226 | return $arr[$index]; |
| 222 | - } else |
|
| 223 | - throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 227 | + } else { |
|
| 228 | + throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 229 | + } |
|
| 224 | 230 | } |
| 225 | 231 | |
| 226 | 232 | /** |
@@ -230,8 +236,9 @@ discard block |
||
| 230 | 236 | */ |
| 231 | 237 | public function itemsAtPriority($priority=null) |
| 232 | 238 | { |
| 233 | - if($priority===null) |
|
| 234 | - $priority=$this->getDefaultPriority(); |
|
| 239 | + if($priority===null) { |
|
| 240 | + $priority=$this->getDefaultPriority(); |
|
| 241 | + } |
|
| 235 | 242 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 236 | 243 | |
| 237 | 244 | return isset($this->_d[$priority])?$this->_d[$priority]:null; |
@@ -245,8 +252,9 @@ discard block |
||
| 245 | 252 | */ |
| 246 | 253 | public function itemAtIndexInPriority($index,$priority=null) |
| 247 | 254 | { |
| 248 | - if($priority===null) |
|
| 249 | - $priority=$this->getDefaultPriority(); |
|
| 255 | + if($priority===null) { |
|
| 256 | + $priority=$this->getDefaultPriority(); |
|
| 257 | + } |
|
| 250 | 258 | $priority=(string)round(TPropertyValue::ensureFloat($priority), $this->_p); |
| 251 | 259 | |
| 252 | 260 | return !isset($this->_d[$priority])?false:( |
@@ -264,8 +272,9 @@ discard block |
||
| 264 | 272 | */ |
| 265 | 273 | public function add($item,$priority=null) |
| 266 | 274 | { |
| 267 | - if($this->getReadOnly()) |
|
| 268 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 275 | + if($this->getReadOnly()) { |
|
| 276 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 277 | + } |
|
| 269 | 278 | |
| 270 | 279 | return $this->insertAtIndexInPriority($item,false,$priority,true); |
| 271 | 280 | } |
@@ -280,13 +289,15 @@ discard block |
||
| 280 | 289 | */ |
| 281 | 290 | public function insertAt($index,$item) |
| 282 | 291 | { |
| 283 | - if($this->getReadOnly()) |
|
| 284 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 292 | + if($this->getReadOnly()) { |
|
| 293 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 294 | + } |
|
| 285 | 295 | |
| 286 | - if(($priority=$this->priorityAt($index,true))!==false) |
|
| 287 | - $this->insertAtIndexInPriority($item,$priority[1],$priority[0]); |
|
| 288 | - else |
|
| 289 | - throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 296 | + if(($priority=$this->priorityAt($index,true))!==false) { |
|
| 297 | + $this->insertAtIndexInPriority($item,$priority[1],$priority[0]); |
|
| 298 | + } else { |
|
| 299 | + throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 300 | + } |
|
| 290 | 301 | } |
| 291 | 302 | |
| 292 | 303 | /** |
@@ -301,21 +312,25 @@ discard block |
||
| 301 | 312 | */ |
| 302 | 313 | public function insertAtIndexInPriority($item,$index=false,$priority=null,$preserveCache=false) |
| 303 | 314 | { |
| 304 | - if($this->getReadOnly()) |
|
| 305 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 315 | + if($this->getReadOnly()) { |
|
| 316 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 317 | + } |
|
| 306 | 318 | |
| 307 | - if($priority===null) |
|
| 308 | - $priority=$this->getDefaultPriority(); |
|
| 319 | + if($priority===null) { |
|
| 320 | + $priority=$this->getDefaultPriority(); |
|
| 321 | + } |
|
| 309 | 322 | $priority=(string)round(TPropertyValue::ensureFloat($priority), $this->_p); |
| 310 | 323 | |
| 311 | 324 | if($preserveCache) { |
| 312 | 325 | $this->sortPriorities(); |
| 313 | 326 | $cc=0; |
| 314 | - foreach($this->_d as $prioritykey=>$items) |
|
| 315 | - if($prioritykey>=$priority) |
|
| 327 | + foreach($this->_d as $prioritykey=>$items) { |
|
| 328 | + if($prioritykey>=$priority) |
|
| 316 | 329 | break; |
| 317 | - else |
|
| 318 | - $cc+=count($items); |
|
| 330 | + } |
|
| 331 | + else { |
|
| 332 | + $cc+=count($items); |
|
| 333 | + } |
|
| 319 | 334 | |
| 320 | 335 | if($index===false&&isset($this->_d[$priority])) { |
| 321 | 336 | $c=count($this->_d[$priority]); |
@@ -330,8 +345,10 @@ discard block |
||
| 330 | 345 | $this->_d[$priority]=array($item); |
| 331 | 346 | } |
| 332 | 347 | |
| 333 | - if($this->_fd&&is_array($this->_fd)) // if there is a flattened array cache |
|
| 348 | + if($this->_fd&&is_array($this->_fd)) { |
|
| 349 | + // if there is a flattened array cache |
|
| 334 | 350 | array_splice($this->_fd,$c,0,array($item)); |
| 351 | + } |
|
| 335 | 352 | } else { |
| 336 | 353 | $c=null; |
| 337 | 354 | if($index===false&&isset($this->_d[$priority])) { |
@@ -345,10 +362,11 @@ discard block |
||
| 345 | 362 | $this->_o=false; |
| 346 | 363 | $this->_d[$priority]=array($item); |
| 347 | 364 | } |
| 348 | - if($this->_fd&&is_array($this->_fd)&&count($this->_d)==1) |
|
| 349 | - array_splice($this->_fd,$cc,0,array($item)); |
|
| 350 | - else |
|
| 351 | - $this->_fd=null; |
|
| 365 | + if($this->_fd&&is_array($this->_fd)&&count($this->_d)==1) { |
|
| 366 | + array_splice($this->_fd,$cc,0,array($item)); |
|
| 367 | + } else { |
|
| 368 | + $this->_fd=null; |
|
| 369 | + } |
|
| 352 | 370 | } |
| 353 | 371 | |
| 354 | 372 | $this->_c++; |
@@ -369,24 +387,27 @@ discard block |
||
| 369 | 387 | */ |
| 370 | 388 | public function remove($item,$priority=false) |
| 371 | 389 | { |
| 372 | - if($this->getReadOnly()) |
|
| 373 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 390 | + if($this->getReadOnly()) { |
|
| 391 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 392 | + } |
|
| 374 | 393 | |
| 375 | 394 | if(($p=$this->priorityOf($item,true))!==false) |
| 376 | 395 | { |
| 377 | 396 | if($priority!==false) { |
| 378 | - if($priority===null) |
|
| 379 | - $priority=$this->getDefaultPriority(); |
|
| 397 | + if($priority===null) { |
|
| 398 | + $priority=$this->getDefaultPriority(); |
|
| 399 | + } |
|
| 380 | 400 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 381 | 401 | |
| 382 | - if($p[0]!=$priority) |
|
| 383 | - throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 402 | + if($p[0]!=$priority) { |
|
| 403 | + throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 404 | + } |
|
| 384 | 405 | } |
| 385 | 406 | $this->removeAtIndexInPriority($p[1],$p[0]); |
| 386 | 407 | return $p[2]; |
| 408 | + } else { |
|
| 409 | + throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 387 | 410 | } |
| 388 | - else |
|
| 389 | - throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 390 | 411 | } |
| 391 | 412 | |
| 392 | 413 | /** |
@@ -398,11 +419,13 @@ discard block |
||
| 398 | 419 | */ |
| 399 | 420 | public function removeAt($index) |
| 400 | 421 | { |
| 401 | - if($this->getReadOnly()) |
|
| 402 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 422 | + if($this->getReadOnly()) { |
|
| 423 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 424 | + } |
|
| 403 | 425 | |
| 404 | - if(($priority=$this->priorityAt($index, true))!==false) |
|
| 405 | - return $this->removeAtIndexInPriority($priority[1],$priority[0]); |
|
| 426 | + if(($priority=$this->priorityAt($index, true))!==false) { |
|
| 427 | + return $this->removeAtIndexInPriority($priority[1],$priority[0]); |
|
| 428 | + } |
|
| 406 | 429 | throw new TInvalidDataValueException('list_index_invalid',$index); |
| 407 | 430 | } |
| 408 | 431 | |
@@ -416,22 +439,26 @@ discard block |
||
| 416 | 439 | */ |
| 417 | 440 | public function removeAtIndexInPriority($index, $priority=null) |
| 418 | 441 | { |
| 419 | - if($this->getReadOnly()) |
|
| 420 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 442 | + if($this->getReadOnly()) { |
|
| 443 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 444 | + } |
|
| 421 | 445 | |
| 422 | - if($priority===null) |
|
| 423 | - $priority=$this->getDefaultPriority(); |
|
| 446 | + if($priority===null) { |
|
| 447 | + $priority=$this->getDefaultPriority(); |
|
| 448 | + } |
|
| 424 | 449 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 425 | 450 | |
| 426 | - if(!isset($this->_d[$priority])||$index<0||$index>=count($this->_d[$priority])) |
|
| 427 | - throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 451 | + if(!isset($this->_d[$priority])||$index<0||$index>=count($this->_d[$priority])) { |
|
| 452 | + throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 453 | + } |
|
| 428 | 454 | |
| 429 | 455 | // $value is an array of elements removed, only one |
| 430 | 456 | $value=array_splice($this->_d[$priority],$index,1); |
| 431 | 457 | $value=$value[0]; |
| 432 | 458 | |
| 433 | - if(!count($this->_d[$priority])) |
|
| 434 | - unset($this->_d[$priority]); |
|
| 459 | + if(!count($this->_d[$priority])) { |
|
| 460 | + unset($this->_d[$priority]); |
|
| 461 | + } |
|
| 435 | 462 | |
| 436 | 463 | $this->_c--; |
| 437 | 464 | $this->_fd=null; |
@@ -443,13 +470,15 @@ discard block |
||
| 443 | 470 | */ |
| 444 | 471 | public function clear() |
| 445 | 472 | { |
| 446 | - if($this->getReadOnly()) |
|
| 447 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 473 | + if($this->getReadOnly()) { |
|
| 474 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 475 | + } |
|
| 448 | 476 | |
| 449 | 477 | $d=array_reverse($this->_d,true); |
| 450 | 478 | foreach($this->_d as $priority=>$items) { |
| 451 | - for($index=count($items)-1;$index>=0;$index--) |
|
| 452 | - $this->removeAtIndexInPriority($index,$priority); |
|
| 479 | + for($index=count($items)-1;$index>=0;$index--) { |
|
| 480 | + $this->removeAtIndexInPriority($index,$priority); |
|
| 481 | + } |
|
| 453 | 482 | unset($this->_d[$priority]); |
| 454 | 483 | } |
| 455 | 484 | } |
@@ -469,10 +498,11 @@ discard block |
||
| 469 | 498 | */ |
| 470 | 499 | public function indexOf($item) |
| 471 | 500 | { |
| 472 | - if(($index=array_search($item,$this->flattenPriorities(),true))===false) |
|
| 473 | - return -1; |
|
| 474 | - else |
|
| 475 | - return $index; |
|
| 501 | + if(($index=array_search($item,$this->flattenPriorities(),true))===false) { |
|
| 502 | + return -1; |
|
| 503 | + } else { |
|
| 504 | + return $index; |
|
| 505 | + } |
|
| 476 | 506 | } |
| 477 | 507 | |
| 478 | 508 | /** |
@@ -494,8 +524,9 @@ discard block |
||
| 494 | 524 | $absindex+=$index; |
| 495 | 525 | return $withindex?array($priority,$index,$absindex, |
| 496 | 526 | 'priority'=>$priority,'index'=>$index,'absindex'=>$absindex):$priority; |
| 497 | - } else |
|
| 498 | - $absindex+=count($items); |
|
| 527 | + } else { |
|
| 528 | + $absindex+=count($items); |
|
| 529 | + } |
|
| 499 | 530 | } |
| 500 | 531 | |
| 501 | 532 | return false; |
@@ -512,17 +543,19 @@ discard block |
||
| 512 | 543 | */ |
| 513 | 544 | public function priorityAt($index,$withindex = false) |
| 514 | 545 | { |
| 515 | - if($index<0||$index>=$this->getCount()) |
|
| 516 | - throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 546 | + if($index<0||$index>=$this->getCount()) { |
|
| 547 | + throw new TInvalidDataValueException('list_index_invalid',$index); |
|
| 548 | + } |
|
| 517 | 549 | |
| 518 | 550 | $absindex=$index; |
| 519 | 551 | $this->sortPriorities(); |
| 520 | 552 | foreach($this->_d as $priority=>$items) { |
| 521 | - if($index>=($c=count($items))) |
|
| 522 | - $index-=$c; |
|
| 523 | - else |
|
| 524 | - return $withindex?array($priority,$index,$absindex, |
|
| 553 | + if($index>=($c=count($items))) { |
|
| 554 | + $index-=$c; |
|
| 555 | + } else { |
|
| 556 | + return $withindex?array($priority,$index,$absindex, |
|
| 525 | 557 | 'priority'=>$priority,'index'=>$index,'absindex'=>$absindex):$priority; |
| 558 | + } |
|
| 526 | 559 | } |
| 527 | 560 | return false; |
| 528 | 561 | } |
@@ -537,11 +570,13 @@ discard block |
||
| 537 | 570 | */ |
| 538 | 571 | public function insertBefore($indexitem, $item) |
| 539 | 572 | { |
| 540 | - if($this->getReadOnly()) |
|
| 541 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 573 | + if($this->getReadOnly()) { |
|
| 574 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 575 | + } |
|
| 542 | 576 | |
| 543 | - if(($priority=$this->priorityOf($indexitem,true))===false) |
|
| 544 | - throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 577 | + if(($priority=$this->priorityOf($indexitem,true))===false) { |
|
| 578 | + throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 579 | + } |
|
| 545 | 580 | |
| 546 | 581 | $this->insertAtIndexInPriority($item,$priority[1],$priority[0]); |
| 547 | 582 | |
@@ -558,11 +593,13 @@ discard block |
||
| 558 | 593 | */ |
| 559 | 594 | public function insertAfter($indexitem, $item) |
| 560 | 595 | { |
| 561 | - if($this->getReadOnly()) |
|
| 562 | - throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 596 | + if($this->getReadOnly()) { |
|
| 597 | + throw new TInvalidOperationException('list_readonly',get_class($this)); |
|
| 598 | + } |
|
| 563 | 599 | |
| 564 | - if(($priority=$this->priorityOf($indexitem,true))===false) |
|
| 565 | - throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 600 | + if(($priority=$this->priorityOf($indexitem,true))===false) { |
|
| 601 | + throw new TInvalidDataValueException('list_item_inexistent'); |
|
| 602 | + } |
|
| 566 | 603 | |
| 567 | 604 | $this->insertAtIndexInPriority($item,$priority[1]+1,$priority[0]); |
| 568 | 605 | |
@@ -599,8 +636,9 @@ discard block |
||
| 599 | 636 | $items=array(); |
| 600 | 637 | foreach($this->_d as $itemspriority=>$itemsatpriority) |
| 601 | 638 | { |
| 602 | - if((!$inclusive&&$itemspriority>=$priority)||$itemspriority>$priority) |
|
| 603 | - break; |
|
| 639 | + if((!$inclusive&&$itemspriority>=$priority)||$itemspriority>$priority) { |
|
| 640 | + break; |
|
| 641 | + } |
|
| 604 | 642 | $items=array_merge($items,$itemsatpriority); |
| 605 | 643 | } |
| 606 | 644 | return $items; |
@@ -619,8 +657,9 @@ discard block |
||
| 619 | 657 | $items=array(); |
| 620 | 658 | foreach($this->_d as $itemspriority=>$itemsatpriority) |
| 621 | 659 | { |
| 622 | - if((!$inclusive&&$itemspriority<=$priority)||$itemspriority<$priority) |
|
| 623 | - continue; |
|
| 660 | + if((!$inclusive&&$itemspriority<=$priority)||$itemspriority<$priority) { |
|
| 661 | + continue; |
|
| 662 | + } |
|
| 624 | 663 | $items=array_merge($items,$itemsatpriority); |
| 625 | 664 | } |
| 626 | 665 | return $items; |
@@ -637,20 +676,25 @@ discard block |
||
| 637 | 676 | { |
| 638 | 677 | if($data instanceof TPriorityList) |
| 639 | 678 | { |
| 640 | - if($this->getCount()>0) |
|
| 641 | - $this->clear(); |
|
| 679 | + if($this->getCount()>0) { |
|
| 680 | + $this->clear(); |
|
| 681 | + } |
|
| 642 | 682 | foreach($data->getPriorities() as $priority) |
| 643 | 683 | { |
| 644 | - foreach($data->itemsAtPriority($priority) as $index=>$item) |
|
| 645 | - $this->insertAtIndexInPriority($item,$index,$priority); |
|
| 684 | + foreach($data->itemsAtPriority($priority) as $index=>$item) { |
|
| 685 | + $this->insertAtIndexInPriority($item,$index,$priority); |
|
| 686 | + } |
|
| 646 | 687 | } |
| 647 | 688 | } else if(is_array($data)||$data instanceof Traversable) { |
| 648 | - if($this->getCount()>0) |
|
| 649 | - $this->clear(); |
|
| 650 | - foreach($data as $key=>$item) |
|
| 651 | - $this->add($item); |
|
| 652 | - } else if($data!==null) |
|
| 653 | - throw new TInvalidDataTypeException('map_data_not_iterable'); |
|
| 689 | + if($this->getCount()>0) { |
|
| 690 | + $this->clear(); |
|
| 691 | + } |
|
| 692 | + foreach($data as $key=>$item) { |
|
| 693 | + $this->add($item); |
|
| 694 | + } |
|
| 695 | + } else if($data!==null) { |
|
| 696 | + throw new TInvalidDataTypeException('map_data_not_iterable'); |
|
| 697 | + } |
|
| 654 | 698 | } |
| 655 | 699 | |
| 656 | 700 | /** |
@@ -667,18 +711,19 @@ discard block |
||
| 667 | 711 | { |
| 668 | 712 | foreach($data->getPriorities() as $priority) |
| 669 | 713 | { |
| 670 | - foreach($data->itemsAtPriority($priority) as $index=>$item) |
|
| 671 | - $this->insertAtIndexInPriority($item,false,$priority); |
|
| 714 | + foreach($data->itemsAtPriority($priority) as $index=>$item) { |
|
| 715 | + $this->insertAtIndexInPriority($item,false,$priority); |
|
| 716 | + } |
|
| 672 | 717 | } |
| 673 | - } |
|
| 674 | - else if(is_array($data)||$data instanceof Traversable) |
|
| 718 | + } else if(is_array($data)||$data instanceof Traversable) |
|
| 675 | 719 | { |
| 676 | - foreach($data as $priority=>$item) |
|
| 677 | - $this->add($item); |
|
| 720 | + foreach($data as $priority=>$item) { |
|
| 721 | + $this->add($item); |
|
| 722 | + } |
|
| 678 | 723 | |
| 724 | + } else if($data!==null) { |
|
| 725 | + throw new TInvalidDataTypeException('map_data_not_iterable'); |
|
| 679 | 726 | } |
| 680 | - else if($data!==null) |
|
| 681 | - throw new TInvalidDataTypeException('map_data_not_iterable'); |
|
| 682 | 727 | } |
| 683 | 728 | |
| 684 | 729 | /** |
@@ -717,8 +762,9 @@ discard block |
||
| 717 | 762 | */ |
| 718 | 763 | public function offsetSet($offset,$item) |
| 719 | 764 | { |
| 720 | - if($offset===null) |
|
| 721 | - return $this->add($item); |
|
| 765 | + if($offset===null) { |
|
| 766 | + return $this->add($item); |
|
| 767 | + } |
|
| 722 | 768 | if($offset===$this->getCount()) { |
| 723 | 769 | $priority=$this->priorityAt($offset-1,true); |
| 724 | 770 | $priority[1]++; |
@@ -114,6 +114,7 @@ discard block |
||
| 114 | 114 | |
| 115 | 115 | /** |
| 116 | 116 | * @param boolean whether this list is read-only or not |
| 117 | + * @param boolean $value |
|
| 117 | 118 | */ |
| 118 | 119 | protected function setReadOnly($value) |
| 119 | 120 | { |
@@ -131,6 +132,7 @@ discard block |
||
| 131 | 132 | /** |
| 132 | 133 | * This must be called internally or when instantiated. |
| 133 | 134 | * @param numeric sets the default priority of inserted items without a specified priority |
| 135 | + * @param integer $value |
|
| 134 | 136 | */ |
| 135 | 137 | protected function setDefaultPriority($value) |
| 136 | 138 | { |
@@ -148,6 +150,7 @@ discard block |
||
| 148 | 150 | /** |
| 149 | 151 | * This must be called internally or when instantiated. |
| 150 | 152 | * @param integer The precision of numeric priorities. |
| 153 | + * @param integer $value |
|
| 151 | 154 | */ |
| 152 | 155 | protected function setPrecision($value) |
| 153 | 156 | { |
@@ -270,6 +273,8 @@ discard block |
||
| 270 | 273 | * This method is exactly the same as {@link offsetGet}. |
| 271 | 274 | * @param mixed the key |
| 272 | 275 | * @param numeric|null the priority. default: null, filled in with the default priority numeric. |
| 276 | + * @param string $key |
|
| 277 | + * @param integer $priority |
|
| 273 | 278 | * @return numeric old priority of the item |
| 274 | 279 | */ |
| 275 | 280 | public function setPriorityAt($key,$priority=null) |
@@ -338,7 +343,7 @@ discard block |
||
| 338 | 343 | * @param mixed key |
| 339 | 344 | * @param mixed value |
| 340 | 345 | * @param numeric|null priority, default: null, filled in with default priority |
| 341 | - * @return numeric priority at which the pair was added |
|
| 346 | + * @return string priority at which the pair was added |
|
| 342 | 347 | * @throws TInvalidOperationException if the map is read-only |
| 343 | 348 | */ |
| 344 | 349 | public function add($key,$value,$priority=null) |
@@ -467,6 +472,7 @@ discard block |
||
| 467 | 472 | * Combines the map elements which have a priority below the parameter value |
| 468 | 473 | * @param numeric the cut-off priority. All items of priority less than this are returned. |
| 469 | 474 | * @param boolean whether or not the input cut-off priority is inclusive. Default: false, not inclusive. |
| 475 | + * @param integer $priority |
|
| 470 | 476 | * @return array the array of priorities keys with values of arrays of items that are below a specified priority. |
| 471 | 477 | * The priorities are sorted so important priorities, lower numerics, are first. |
| 472 | 478 | */ |
@@ -487,6 +493,7 @@ discard block |
||
| 487 | 493 | * Combines the map elements which have a priority above the parameter value |
| 488 | 494 | * @param numeric the cut-off priority. All items of priority greater than this are returned. |
| 489 | 495 | * @param boolean whether or not the input cut-off priority is inclusive. Default: true, inclusive. |
| 496 | + * @param integer $priority |
|
| 490 | 497 | * @return array the array of priorities keys with values of arrays of items that are above a specified priority. |
| 491 | 498 | * The priorities are sorted so important priorities, lower numerics, are first. |
| 492 | 499 | */ |
@@ -60,31 +60,31 @@ discard block |
||
| 60 | 60 | /** |
| 61 | 61 | * @var array internal data storage |
| 62 | 62 | */ |
| 63 | - private $_d=array(); |
|
| 63 | + private $_d = array(); |
|
| 64 | 64 | /** |
| 65 | 65 | * @var boolean whether this list is read-only |
| 66 | 66 | */ |
| 67 | - private $_r=false; |
|
| 67 | + private $_r = false; |
|
| 68 | 68 | /** |
| 69 | 69 | * @var boolean indicates if the _d is currently ordered. |
| 70 | 70 | */ |
| 71 | - private $_o=false; |
|
| 71 | + private $_o = false; |
|
| 72 | 72 | /** |
| 73 | 73 | * @var array cached flattened internal data storage |
| 74 | 74 | */ |
| 75 | - private $_fd=null; |
|
| 75 | + private $_fd = null; |
|
| 76 | 76 | /** |
| 77 | 77 | * @var integer number of items contain within the map |
| 78 | 78 | */ |
| 79 | - private $_c=0; |
|
| 79 | + private $_c = 0; |
|
| 80 | 80 | /** |
| 81 | 81 | * @var numeric the default priority of items without specified priorities |
| 82 | 82 | */ |
| 83 | - private $_dp=10; |
|
| 83 | + private $_dp = 10; |
|
| 84 | 84 | /** |
| 85 | 85 | * @var integer the precision of the numeric priorities within this priority list. |
| 86 | 86 | */ |
| 87 | - private $_p=8; |
|
| 87 | + private $_p = 8; |
|
| 88 | 88 | |
| 89 | 89 | /** |
| 90 | 90 | * Constructor. |
@@ -95,9 +95,9 @@ discard block |
||
| 95 | 95 | * @param integer the precision of the numeric priorities |
| 96 | 96 | * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator. |
| 97 | 97 | */ |
| 98 | - public function __construct($data=null,$readOnly=false,$defaultPriority=10,$precision=8) |
|
| 98 | + public function __construct($data = null, $readOnly = false, $defaultPriority = 10, $precision = 8) |
|
| 99 | 99 | { |
| 100 | - if($data!==null) |
|
| 100 | + if ($data !== null) |
|
| 101 | 101 | $this->copyFrom($data); |
| 102 | 102 | $this->setReadOnly($readOnly); |
| 103 | 103 | $this->setPrecision($precision); |
@@ -117,7 +117,7 @@ discard block |
||
| 117 | 117 | */ |
| 118 | 118 | protected function setReadOnly($value) |
| 119 | 119 | { |
| 120 | - $this->_r=TPropertyValue::ensureBoolean($value); |
|
| 120 | + $this->_r = TPropertyValue::ensureBoolean($value); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -134,7 +134,7 @@ discard block |
||
| 134 | 134 | */ |
| 135 | 135 | protected function setDefaultPriority($value) |
| 136 | 136 | { |
| 137 | - $this->_dp = (string)round(TPropertyValue::ensureFloat($value), $this->_p); |
|
| 137 | + $this->_dp = (string) round(TPropertyValue::ensureFloat($value), $this->_p); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | protected function setPrecision($value) |
| 153 | 153 | { |
| 154 | - $this->_p=TPropertyValue::ensureInteger($value); |
|
| 154 | + $this->_p = TPropertyValue::ensureInteger($value); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -169,9 +169,9 @@ discard block |
||
| 169 | 169 | * Orders the priority list internally. |
| 170 | 170 | */ |
| 171 | 171 | protected function sortPriorities() { |
| 172 | - if(!$this->_o) { |
|
| 172 | + if (!$this->_o) { |
|
| 173 | 173 | ksort($this->_d, SORT_NUMERIC); |
| 174 | - $this->_o=true; |
|
| 174 | + $this->_o = true; |
|
| 175 | 175 | } |
| 176 | 176 | } |
| 177 | 177 | |
@@ -180,12 +180,12 @@ discard block |
||
| 180 | 180 | * @return array array of items in the list in priority and index order |
| 181 | 181 | */ |
| 182 | 182 | protected function flattenPriorities() { |
| 183 | - if(is_array($this->_fd)) |
|
| 183 | + if (is_array($this->_fd)) |
|
| 184 | 184 | return $this->_fd; |
| 185 | 185 | |
| 186 | 186 | $this->sortPriorities(); |
| 187 | 187 | $this->_fd = array(); |
| 188 | - foreach($this->_d as $priority => $itemsatpriority) |
|
| 188 | + foreach ($this->_d as $priority => $itemsatpriority) |
|
| 189 | 189 | $this->_fd = array_merge($this->_fd, $itemsatpriority); |
| 190 | 190 | return $this->_fd; |
| 191 | 191 | } |
@@ -214,13 +214,13 @@ discard block |
||
| 214 | 214 | * it will be set to the default {@link getDefaultPriority} |
| 215 | 215 | * @return integer the number of items in the map at the specified priority |
| 216 | 216 | */ |
| 217 | - public function getPriorityCount($priority=null) |
|
| 217 | + public function getPriorityCount($priority = null) |
|
| 218 | 218 | { |
| 219 | - if($priority===null) |
|
| 220 | - $priority=$this->getDefaultPriority(); |
|
| 221 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 219 | + if ($priority === null) |
|
| 220 | + $priority = $this->getDefaultPriority(); |
|
| 221 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 222 | 222 | |
| 223 | - if(!isset($this->_d[$priority])||!is_array($this->_d[$priority])) |
|
| 223 | + if (!isset($this->_d[$priority]) || !is_array($this->_d[$priority])) |
|
| 224 | 224 | return false; |
| 225 | 225 | return count($this->_d[$priority]); |
| 226 | 226 | } |
@@ -252,16 +252,16 @@ discard block |
||
| 252 | 252 | * and numeric is a specific priority. default: false, any priority. |
| 253 | 253 | * @return mixed the element at the offset, null if no element is found at the offset |
| 254 | 254 | */ |
| 255 | - public function itemAt($key,$priority=false) |
|
| 255 | + public function itemAt($key, $priority = false) |
|
| 256 | 256 | { |
| 257 | - if($priority===false){ |
|
| 258 | - $map=$this->flattenPriorities(); |
|
| 259 | - return isset($map[$key])?$map[$key]:null; |
|
| 257 | + if ($priority === false) { |
|
| 258 | + $map = $this->flattenPriorities(); |
|
| 259 | + return isset($map[$key]) ? $map[$key] : null; |
|
| 260 | 260 | } else { |
| 261 | - if($priority===null) |
|
| 262 | - $priority=$this->getDefaultPriority(); |
|
| 263 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 264 | - return (isset($this->_d[$priority])&&isset($this->_d[$priority][$key]))?$this->_d[$priority][$key]:null; |
|
| 261 | + if ($priority === null) |
|
| 262 | + $priority = $this->getDefaultPriority(); |
|
| 263 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 264 | + return (isset($this->_d[$priority]) && isset($this->_d[$priority][$key])) ? $this->_d[$priority][$key] : null; |
|
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | |
@@ -272,16 +272,16 @@ discard block |
||
| 272 | 272 | * @param numeric|null the priority. default: null, filled in with the default priority numeric. |
| 273 | 273 | * @return numeric old priority of the item |
| 274 | 274 | */ |
| 275 | - public function setPriorityAt($key,$priority=null) |
|
| 275 | + public function setPriorityAt($key, $priority = null) |
|
| 276 | 276 | { |
| 277 | - if($priority===null) |
|
| 278 | - $priority=$this->getDefaultPriority(); |
|
| 279 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 280 | - |
|
| 281 | - $oldpriority=$this->priorityAt($key); |
|
| 282 | - if($oldpriority!==false&&$oldpriority!=$priority) { |
|
| 283 | - $value=$this->remove($key,$oldpriority); |
|
| 284 | - $this->add($key,$value,$priority); |
|
| 277 | + if ($priority === null) |
|
| 278 | + $priority = $this->getDefaultPriority(); |
|
| 279 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 280 | + |
|
| 281 | + $oldpriority = $this->priorityAt($key); |
|
| 282 | + if ($oldpriority !== false && $oldpriority != $priority) { |
|
| 283 | + $value = $this->remove($key, $oldpriority); |
|
| 284 | + $this->add($key, $value, $priority); |
|
| 285 | 285 | } |
| 286 | 286 | return $oldpriority; |
| 287 | 287 | } |
@@ -291,13 +291,13 @@ discard block |
||
| 291 | 291 | * @param numeric priority of the items to get. Defaults to null, filled in with the default priority, if left blank. |
| 292 | 292 | * @return array all items at priority in index order, null if there are no items at that priority |
| 293 | 293 | */ |
| 294 | - public function itemsAtPriority($priority=null) |
|
| 294 | + public function itemsAtPriority($priority = null) |
|
| 295 | 295 | { |
| 296 | - if($priority===null) |
|
| 297 | - $priority=$this->getDefaultPriority(); |
|
| 298 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 296 | + if ($priority === null) |
|
| 297 | + $priority = $this->getDefaultPriority(); |
|
| 298 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 299 | 299 | |
| 300 | - return isset($this->_d[$priority])?$this->_d[$priority]:null; |
|
| 300 | + return isset($this->_d[$priority]) ? $this->_d[$priority] : null; |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | /** |
@@ -308,8 +308,8 @@ discard block |
||
| 308 | 308 | public function priorityOf($item) |
| 309 | 309 | { |
| 310 | 310 | $this->sortPriorities(); |
| 311 | - foreach($this->_d as $priority=>$items) |
|
| 312 | - if(($index=array_search($item,$items,true))!==false) |
|
| 311 | + foreach ($this->_d as $priority=>$items) |
|
| 312 | + if (($index = array_search($item, $items, true)) !== false) |
|
| 313 | 313 | return $priority; |
| 314 | 314 | return false; |
| 315 | 315 | } |
@@ -322,8 +322,8 @@ discard block |
||
| 322 | 322 | public function priorityAt($key) |
| 323 | 323 | { |
| 324 | 324 | $this->sortPriorities(); |
| 325 | - foreach($this->_d as $priority=>$items) |
|
| 326 | - if(array_key_exists($key,$items)) |
|
| 325 | + foreach ($this->_d as $priority=>$items) |
|
| 326 | + if (array_key_exists($key, $items)) |
|
| 327 | 327 | return $priority; |
| 328 | 328 | return false; |
| 329 | 329 | } |
@@ -341,33 +341,33 @@ discard block |
||
| 341 | 341 | * @return numeric priority at which the pair was added |
| 342 | 342 | * @throws TInvalidOperationException if the map is read-only |
| 343 | 343 | */ |
| 344 | - public function add($key,$value,$priority=null) |
|
| 344 | + public function add($key, $value, $priority = null) |
|
| 345 | 345 | { |
| 346 | - if($priority===null) |
|
| 347 | - $priority=$this->getDefaultPriority(); |
|
| 348 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 346 | + if ($priority === null) |
|
| 347 | + $priority = $this->getDefaultPriority(); |
|
| 348 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 349 | 349 | |
| 350 | - if(!$this->_r) |
|
| 350 | + if (!$this->_r) |
|
| 351 | 351 | { |
| 352 | - foreach($this->_d as $innerpriority=>$items) |
|
| 353 | - if(array_key_exists($key,$items)) |
|
| 352 | + foreach ($this->_d as $innerpriority=>$items) |
|
| 353 | + if (array_key_exists($key, $items)) |
|
| 354 | 354 | { |
| 355 | 355 | unset($this->_d[$innerpriority][$key]); |
| 356 | 356 | $this->_c--; |
| 357 | - if(count($this->_d[$innerpriority])===0) |
|
| 357 | + if (count($this->_d[$innerpriority]) === 0) |
|
| 358 | 358 | unset($this->_d[$innerpriority]); |
| 359 | 359 | } |
| 360 | - if(!isset($this->_d[$priority])) { |
|
| 361 | - $this->_d[$priority]=array($key=>$value); |
|
| 362 | - $this->_o=false; |
|
| 360 | + if (!isset($this->_d[$priority])) { |
|
| 361 | + $this->_d[$priority] = array($key=>$value); |
|
| 362 | + $this->_o = false; |
|
| 363 | 363 | } |
| 364 | 364 | else |
| 365 | - $this->_d[$priority][$key]=$value; |
|
| 365 | + $this->_d[$priority][$key] = $value; |
|
| 366 | 366 | $this->_c++; |
| 367 | - $this->_fd=null; |
|
| 367 | + $this->_fd = null; |
|
| 368 | 368 | } |
| 369 | 369 | else |
| 370 | - throw new TInvalidOperationException('map_readonly',get_class($this)); |
|
| 370 | + throw new TInvalidOperationException('map_readonly', get_class($this)); |
|
| 371 | 371 | return $priority; |
| 372 | 372 | } |
| 373 | 373 | |
@@ -383,45 +383,45 @@ discard block |
||
| 383 | 383 | * @return mixed the removed value, null if no such key exists. |
| 384 | 384 | * @throws TInvalidOperationException if the map is read-only |
| 385 | 385 | */ |
| 386 | - public function remove($key,$priority=false) |
|
| 386 | + public function remove($key, $priority = false) |
|
| 387 | 387 | { |
| 388 | - if(!$this->_r) |
|
| 388 | + if (!$this->_r) |
|
| 389 | 389 | { |
| 390 | - if($priority===null) |
|
| 391 | - $priority=$this->getDefaultPriority(); |
|
| 390 | + if ($priority === null) |
|
| 391 | + $priority = $this->getDefaultPriority(); |
|
| 392 | 392 | |
| 393 | - if($priority===false) |
|
| 393 | + if ($priority === false) |
|
| 394 | 394 | { |
| 395 | 395 | $this->sortPriorities(); |
| 396 | - foreach($this->_d as $priority=>$items) |
|
| 397 | - if(array_key_exists($key,$items)) |
|
| 396 | + foreach ($this->_d as $priority=>$items) |
|
| 397 | + if (array_key_exists($key, $items)) |
|
| 398 | 398 | { |
| 399 | - $value=$this->_d[$priority][$key]; |
|
| 399 | + $value = $this->_d[$priority][$key]; |
|
| 400 | 400 | unset($this->_d[$priority][$key]); |
| 401 | 401 | $this->_c--; |
| 402 | - if(count($this->_d[$priority])===0) |
|
| 402 | + if (count($this->_d[$priority]) === 0) |
|
| 403 | 403 | { |
| 404 | 404 | unset($this->_d[$priority]); |
| 405 | - $this->_o=false; |
|
| 405 | + $this->_o = false; |
|
| 406 | 406 | } |
| 407 | - $this->_fd=null; |
|
| 407 | + $this->_fd = null; |
|
| 408 | 408 | return $value; |
| 409 | 409 | } |
| 410 | 410 | return null; |
| 411 | 411 | } |
| 412 | 412 | else |
| 413 | 413 | { |
| 414 | - $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
|
| 415 | - if(isset($this->_d[$priority])&&(isset($this->_d[$priority][$key])||array_key_exists($key,$this->_d[$priority]))) |
|
| 414 | + $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p); |
|
| 415 | + if (isset($this->_d[$priority]) && (isset($this->_d[$priority][$key]) || array_key_exists($key, $this->_d[$priority]))) |
|
| 416 | 416 | { |
| 417 | - $value=$this->_d[$priority][$key]; |
|
| 417 | + $value = $this->_d[$priority][$key]; |
|
| 418 | 418 | unset($this->_d[$priority][$key]); |
| 419 | 419 | $this->_c--; |
| 420 | - if(count($this->_d[$priority])===0) { |
|
| 420 | + if (count($this->_d[$priority]) === 0) { |
|
| 421 | 421 | unset($this->_d[$priority]); |
| 422 | - $this->_o=false; |
|
| 422 | + $this->_o = false; |
|
| 423 | 423 | } |
| 424 | - $this->_fd=null; |
|
| 424 | + $this->_fd = null; |
|
| 425 | 425 | return $value; |
| 426 | 426 | } |
| 427 | 427 | else |
@@ -429,7 +429,7 @@ discard block |
||
| 429 | 429 | } |
| 430 | 430 | } |
| 431 | 431 | else |
| 432 | - throw new TInvalidOperationException('map_readonly',get_class($this)); |
|
| 432 | + throw new TInvalidOperationException('map_readonly', get_class($this)); |
|
| 433 | 433 | } |
| 434 | 434 | |
| 435 | 435 | /** |
@@ -437,8 +437,8 @@ discard block |
||
| 437 | 437 | */ |
| 438 | 438 | public function clear() |
| 439 | 439 | { |
| 440 | - foreach($this->_d as $priority=>$items) |
|
| 441 | - foreach(array_keys($items) as $key) |
|
| 440 | + foreach ($this->_d as $priority=>$items) |
|
| 441 | + foreach (array_keys($items) as $key) |
|
| 442 | 442 | $this->remove($key); |
| 443 | 443 | } |
| 444 | 444 | |
@@ -448,8 +448,8 @@ discard block |
||
| 448 | 448 | */ |
| 449 | 449 | public function contains($key) |
| 450 | 450 | { |
| 451 | - $map=$this->flattenPriorities(); |
|
| 452 | - return isset($map[$key])||array_key_exists($key,$map); |
|
| 451 | + $map = $this->flattenPriorities(); |
|
| 452 | + return isset($map[$key]) || array_key_exists($key, $map); |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -470,15 +470,15 @@ discard block |
||
| 470 | 470 | * @return array the array of priorities keys with values of arrays of items that are below a specified priority. |
| 471 | 471 | * The priorities are sorted so important priorities, lower numerics, are first. |
| 472 | 472 | */ |
| 473 | - public function toArrayBelowPriority($priority,$inclusive=false) |
|
| 473 | + public function toArrayBelowPriority($priority, $inclusive = false) |
|
| 474 | 474 | { |
| 475 | 475 | $this->sortPriorities(); |
| 476 | - $items=array(); |
|
| 477 | - foreach($this->_d as $itemspriority=>$itemsatpriority) |
|
| 476 | + $items = array(); |
|
| 477 | + foreach ($this->_d as $itemspriority=>$itemsatpriority) |
|
| 478 | 478 | { |
| 479 | - if((!$inclusive&&$itemspriority>=$priority)||$itemspriority>$priority) |
|
| 479 | + if ((!$inclusive && $itemspriority >= $priority) || $itemspriority > $priority) |
|
| 480 | 480 | break; |
| 481 | - $items=array_merge($items,$itemsatpriority); |
|
| 481 | + $items = array_merge($items, $itemsatpriority); |
|
| 482 | 482 | } |
| 483 | 483 | return $items; |
| 484 | 484 | } |
@@ -490,15 +490,15 @@ discard block |
||
| 490 | 490 | * @return array the array of priorities keys with values of arrays of items that are above a specified priority. |
| 491 | 491 | * The priorities are sorted so important priorities, lower numerics, are first. |
| 492 | 492 | */ |
| 493 | - public function toArrayAbovePriority($priority,$inclusive=true) |
|
| 493 | + public function toArrayAbovePriority($priority, $inclusive = true) |
|
| 494 | 494 | { |
| 495 | 495 | $this->sortPriorities(); |
| 496 | - $items=array(); |
|
| 497 | - foreach($this->_d as $itemspriority=>$itemsatpriority) |
|
| 496 | + $items = array(); |
|
| 497 | + foreach ($this->_d as $itemspriority=>$itemsatpriority) |
|
| 498 | 498 | { |
| 499 | - if((!$inclusive&&$itemspriority<=$priority)||$itemspriority<$priority) |
|
| 499 | + if ((!$inclusive && $itemspriority <= $priority) || $itemspriority < $priority) |
|
| 500 | 500 | continue; |
| 501 | - $items=array_merge($items,$itemsatpriority); |
|
| 501 | + $items = array_merge($items, $itemsatpriority); |
|
| 502 | 502 | } |
| 503 | 503 | return $items; |
| 504 | 504 | } |
@@ -512,24 +512,24 @@ discard block |
||
| 512 | 512 | */ |
| 513 | 513 | public function copyFrom($data) |
| 514 | 514 | { |
| 515 | - if($data instanceof TPriorityMap) |
|
| 515 | + if ($data instanceof TPriorityMap) |
|
| 516 | 516 | { |
| 517 | - if($this->getCount()>0) |
|
| 517 | + if ($this->getCount() > 0) |
|
| 518 | 518 | $this->clear(); |
| 519 | - foreach($data->getPriorities() as $priority) { |
|
| 520 | - foreach($data->itemsAtPriority($priority) as $key => $value) { |
|
| 521 | - $this->add($key,$value,$priority); |
|
| 519 | + foreach ($data->getPriorities() as $priority) { |
|
| 520 | + foreach ($data->itemsAtPriority($priority) as $key => $value) { |
|
| 521 | + $this->add($key, $value, $priority); |
|
| 522 | 522 | } |
| 523 | 523 | } |
| 524 | 524 | } |
| 525 | - else if(is_array($data)||$data instanceof Traversable) |
|
| 525 | + else if (is_array($data) || $data instanceof Traversable) |
|
| 526 | 526 | { |
| 527 | - if($this->getCount()>0) |
|
| 527 | + if ($this->getCount() > 0) |
|
| 528 | 528 | $this->clear(); |
| 529 | - foreach($data as $key=>$value) |
|
| 530 | - $this->add($key,$value); |
|
| 529 | + foreach ($data as $key=>$value) |
|
| 530 | + $this->add($key, $value); |
|
| 531 | 531 | } |
| 532 | - else if($data!==null) |
|
| 532 | + else if ($data !== null) |
|
| 533 | 533 | throw new TInvalidDataTypeException('map_data_not_iterable'); |
| 534 | 534 | } |
| 535 | 535 | |
@@ -542,20 +542,20 @@ discard block |
||
| 542 | 542 | */ |
| 543 | 543 | public function mergeWith($data) |
| 544 | 544 | { |
| 545 | - if($data instanceof TPriorityMap) |
|
| 545 | + if ($data instanceof TPriorityMap) |
|
| 546 | 546 | { |
| 547 | - foreach($data->getPriorities() as $priority) |
|
| 547 | + foreach ($data->getPriorities() as $priority) |
|
| 548 | 548 | { |
| 549 | - foreach($data->itemsAtPriority($priority) as $key => $value) |
|
| 550 | - $this->add($key,$value,$priority); |
|
| 549 | + foreach ($data->itemsAtPriority($priority) as $key => $value) |
|
| 550 | + $this->add($key, $value, $priority); |
|
| 551 | 551 | } |
| 552 | 552 | } |
| 553 | - else if(is_array($data)||$data instanceof Traversable) |
|
| 553 | + else if (is_array($data) || $data instanceof Traversable) |
|
| 554 | 554 | { |
| 555 | - foreach($data as $key=>$value) |
|
| 556 | - $this->add($key,$value); |
|
| 555 | + foreach ($data as $key=>$value) |
|
| 556 | + $this->add($key, $value); |
|
| 557 | 557 | } |
| 558 | - else if($data!==null) |
|
| 558 | + else if ($data !== null) |
|
| 559 | 559 | throw new TInvalidDataTypeException('map_data_not_iterable'); |
| 560 | 560 | } |
| 561 | 561 | |
@@ -587,9 +587,9 @@ discard block |
||
| 587 | 587 | * @param integer the offset to set element |
| 588 | 588 | * @param mixed the element value |
| 589 | 589 | */ |
| 590 | - public function offsetSet($offset,$item) |
|
| 590 | + public function offsetSet($offset, $item) |
|
| 591 | 591 | { |
| 592 | - $this->add($offset,$item); |
|
| 592 | + $this->add($offset, $item); |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | /** |
@@ -97,8 +97,9 @@ discard block |
||
| 97 | 97 | */ |
| 98 | 98 | public function __construct($data=null,$readOnly=false,$defaultPriority=10,$precision=8) |
| 99 | 99 | { |
| 100 | - if($data!==null) |
|
| 101 | - $this->copyFrom($data); |
|
| 100 | + if($data!==null) { |
|
| 101 | + $this->copyFrom($data); |
|
| 102 | + } |
|
| 102 | 103 | $this->setReadOnly($readOnly); |
| 103 | 104 | $this->setPrecision($precision); |
| 104 | 105 | $this->setDefaultPriority($defaultPriority); |
@@ -180,13 +181,15 @@ discard block |
||
| 180 | 181 | * @return array array of items in the list in priority and index order |
| 181 | 182 | */ |
| 182 | 183 | protected function flattenPriorities() { |
| 183 | - if(is_array($this->_fd)) |
|
| 184 | - return $this->_fd; |
|
| 184 | + if(is_array($this->_fd)) { |
|
| 185 | + return $this->_fd; |
|
| 186 | + } |
|
| 185 | 187 | |
| 186 | 188 | $this->sortPriorities(); |
| 187 | 189 | $this->_fd = array(); |
| 188 | - foreach($this->_d as $priority => $itemsatpriority) |
|
| 189 | - $this->_fd = array_merge($this->_fd, $itemsatpriority); |
|
| 190 | + foreach($this->_d as $priority => $itemsatpriority) { |
|
| 191 | + $this->_fd = array_merge($this->_fd, $itemsatpriority); |
|
| 192 | + } |
|
| 190 | 193 | return $this->_fd; |
| 191 | 194 | } |
| 192 | 195 | |
@@ -216,12 +219,14 @@ discard block |
||
| 216 | 219 | */ |
| 217 | 220 | public function getPriorityCount($priority=null) |
| 218 | 221 | { |
| 219 | - if($priority===null) |
|
| 220 | - $priority=$this->getDefaultPriority(); |
|
| 222 | + if($priority===null) { |
|
| 223 | + $priority=$this->getDefaultPriority(); |
|
| 224 | + } |
|
| 221 | 225 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 222 | 226 | |
| 223 | - if(!isset($this->_d[$priority])||!is_array($this->_d[$priority])) |
|
| 224 | - return false; |
|
| 227 | + if(!isset($this->_d[$priority])||!is_array($this->_d[$priority])) { |
|
| 228 | + return false; |
|
| 229 | + } |
|
| 225 | 230 | return count($this->_d[$priority]); |
| 226 | 231 | } |
| 227 | 232 | |
@@ -258,8 +263,9 @@ discard block |
||
| 258 | 263 | $map=$this->flattenPriorities(); |
| 259 | 264 | return isset($map[$key])?$map[$key]:null; |
| 260 | 265 | } else { |
| 261 | - if($priority===null) |
|
| 262 | - $priority=$this->getDefaultPriority(); |
|
| 266 | + if($priority===null) { |
|
| 267 | + $priority=$this->getDefaultPriority(); |
|
| 268 | + } |
|
| 263 | 269 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 264 | 270 | return (isset($this->_d[$priority])&&isset($this->_d[$priority][$key]))?$this->_d[$priority][$key]:null; |
| 265 | 271 | } |
@@ -274,8 +280,9 @@ discard block |
||
| 274 | 280 | */ |
| 275 | 281 | public function setPriorityAt($key,$priority=null) |
| 276 | 282 | { |
| 277 | - if($priority===null) |
|
| 278 | - $priority=$this->getDefaultPriority(); |
|
| 283 | + if($priority===null) { |
|
| 284 | + $priority=$this->getDefaultPriority(); |
|
| 285 | + } |
|
| 279 | 286 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 280 | 287 | |
| 281 | 288 | $oldpriority=$this->priorityAt($key); |
@@ -293,8 +300,9 @@ discard block |
||
| 293 | 300 | */ |
| 294 | 301 | public function itemsAtPriority($priority=null) |
| 295 | 302 | { |
| 296 | - if($priority===null) |
|
| 297 | - $priority=$this->getDefaultPriority(); |
|
| 303 | + if($priority===null) { |
|
| 304 | + $priority=$this->getDefaultPriority(); |
|
| 305 | + } |
|
| 298 | 306 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 299 | 307 | |
| 300 | 308 | return isset($this->_d[$priority])?$this->_d[$priority]:null; |
@@ -308,9 +316,10 @@ discard block |
||
| 308 | 316 | public function priorityOf($item) |
| 309 | 317 | { |
| 310 | 318 | $this->sortPriorities(); |
| 311 | - foreach($this->_d as $priority=>$items) |
|
| 312 | - if(($index=array_search($item,$items,true))!==false) |
|
| 319 | + foreach($this->_d as $priority=>$items) { |
|
| 320 | + if(($index=array_search($item,$items,true))!==false) |
|
| 313 | 321 | return $priority; |
| 322 | + } |
|
| 314 | 323 | return false; |
| 315 | 324 | } |
| 316 | 325 | |
@@ -322,9 +331,10 @@ discard block |
||
| 322 | 331 | public function priorityAt($key) |
| 323 | 332 | { |
| 324 | 333 | $this->sortPriorities(); |
| 325 | - foreach($this->_d as $priority=>$items) |
|
| 326 | - if(array_key_exists($key,$items)) |
|
| 334 | + foreach($this->_d as $priority=>$items) { |
|
| 335 | + if(array_key_exists($key,$items)) |
|
| 327 | 336 | return $priority; |
| 337 | + } |
|
| 328 | 338 | return false; |
| 329 | 339 | } |
| 330 | 340 | |
@@ -343,31 +353,34 @@ discard block |
||
| 343 | 353 | */ |
| 344 | 354 | public function add($key,$value,$priority=null) |
| 345 | 355 | { |
| 346 | - if($priority===null) |
|
| 347 | - $priority=$this->getDefaultPriority(); |
|
| 356 | + if($priority===null) { |
|
| 357 | + $priority=$this->getDefaultPriority(); |
|
| 358 | + } |
|
| 348 | 359 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 349 | 360 | |
| 350 | 361 | if(!$this->_r) |
| 351 | 362 | { |
| 352 | - foreach($this->_d as $innerpriority=>$items) |
|
| 353 | - if(array_key_exists($key,$items)) |
|
| 363 | + foreach($this->_d as $innerpriority=>$items) { |
|
| 364 | + if(array_key_exists($key,$items)) |
|
| 354 | 365 | { |
| 355 | 366 | unset($this->_d[$innerpriority][$key]); |
| 367 | + } |
|
| 356 | 368 | $this->_c--; |
| 357 | - if(count($this->_d[$innerpriority])===0) |
|
| 358 | - unset($this->_d[$innerpriority]); |
|
| 369 | + if(count($this->_d[$innerpriority])===0) { |
|
| 370 | + unset($this->_d[$innerpriority]); |
|
| 371 | + } |
|
| 359 | 372 | } |
| 360 | 373 | if(!isset($this->_d[$priority])) { |
| 361 | 374 | $this->_d[$priority]=array($key=>$value); |
| 362 | 375 | $this->_o=false; |
| 376 | + } else { |
|
| 377 | + $this->_d[$priority][$key]=$value; |
|
| 363 | 378 | } |
| 364 | - else |
|
| 365 | - $this->_d[$priority][$key]=$value; |
|
| 366 | 379 | $this->_c++; |
| 367 | 380 | $this->_fd=null; |
| 381 | + } else { |
|
| 382 | + throw new TInvalidOperationException('map_readonly',get_class($this)); |
|
| 368 | 383 | } |
| 369 | - else |
|
| 370 | - throw new TInvalidOperationException('map_readonly',get_class($this)); |
|
| 371 | 384 | return $priority; |
| 372 | 385 | } |
| 373 | 386 | |
@@ -387,16 +400,18 @@ discard block |
||
| 387 | 400 | { |
| 388 | 401 | if(!$this->_r) |
| 389 | 402 | { |
| 390 | - if($priority===null) |
|
| 391 | - $priority=$this->getDefaultPriority(); |
|
| 403 | + if($priority===null) { |
|
| 404 | + $priority=$this->getDefaultPriority(); |
|
| 405 | + } |
|
| 392 | 406 | |
| 393 | 407 | if($priority===false) |
| 394 | 408 | { |
| 395 | 409 | $this->sortPriorities(); |
| 396 | - foreach($this->_d as $priority=>$items) |
|
| 397 | - if(array_key_exists($key,$items)) |
|
| 410 | + foreach($this->_d as $priority=>$items) { |
|
| 411 | + if(array_key_exists($key,$items)) |
|
| 398 | 412 | { |
| 399 | 413 | $value=$this->_d[$priority][$key]; |
| 414 | + } |
|
| 400 | 415 | unset($this->_d[$priority][$key]); |
| 401 | 416 | $this->_c--; |
| 402 | 417 | if(count($this->_d[$priority])===0) |
@@ -408,8 +423,7 @@ discard block |
||
| 408 | 423 | return $value; |
| 409 | 424 | } |
| 410 | 425 | return null; |
| 411 | - } |
|
| 412 | - else |
|
| 426 | + } else |
|
| 413 | 427 | { |
| 414 | 428 | $priority=(string)round(TPropertyValue::ensureFloat($priority),$this->_p); |
| 415 | 429 | if(isset($this->_d[$priority])&&(isset($this->_d[$priority][$key])||array_key_exists($key,$this->_d[$priority]))) |
@@ -423,13 +437,13 @@ discard block |
||
| 423 | 437 | } |
| 424 | 438 | $this->_fd=null; |
| 425 | 439 | return $value; |
| 440 | + } else { |
|
| 441 | + return null; |
|
| 426 | 442 | } |
| 427 | - else |
|
| 428 | - return null; |
|
| 429 | 443 | } |
| 444 | + } else { |
|
| 445 | + throw new TInvalidOperationException('map_readonly',get_class($this)); |
|
| 430 | 446 | } |
| 431 | - else |
|
| 432 | - throw new TInvalidOperationException('map_readonly',get_class($this)); |
|
| 433 | 447 | } |
| 434 | 448 | |
| 435 | 449 | /** |
@@ -437,9 +451,10 @@ discard block |
||
| 437 | 451 | */ |
| 438 | 452 | public function clear() |
| 439 | 453 | { |
| 440 | - foreach($this->_d as $priority=>$items) |
|
| 441 | - foreach(array_keys($items) as $key) |
|
| 454 | + foreach($this->_d as $priority=>$items) { |
|
| 455 | + foreach(array_keys($items) as $key) |
|
| 442 | 456 | $this->remove($key); |
| 457 | + } |
|
| 443 | 458 | } |
| 444 | 459 | |
| 445 | 460 | /** |
@@ -476,8 +491,9 @@ discard block |
||
| 476 | 491 | $items=array(); |
| 477 | 492 | foreach($this->_d as $itemspriority=>$itemsatpriority) |
| 478 | 493 | { |
| 479 | - if((!$inclusive&&$itemspriority>=$priority)||$itemspriority>$priority) |
|
| 480 | - break; |
|
| 494 | + if((!$inclusive&&$itemspriority>=$priority)||$itemspriority>$priority) { |
|
| 495 | + break; |
|
| 496 | + } |
|
| 481 | 497 | $items=array_merge($items,$itemsatpriority); |
| 482 | 498 | } |
| 483 | 499 | return $items; |
@@ -496,8 +512,9 @@ discard block |
||
| 496 | 512 | $items=array(); |
| 497 | 513 | foreach($this->_d as $itemspriority=>$itemsatpriority) |
| 498 | 514 | { |
| 499 | - if((!$inclusive&&$itemspriority<=$priority)||$itemspriority<$priority) |
|
| 500 | - continue; |
|
| 515 | + if((!$inclusive&&$itemspriority<=$priority)||$itemspriority<$priority) { |
|
| 516 | + continue; |
|
| 517 | + } |
|
| 501 | 518 | $items=array_merge($items,$itemsatpriority); |
| 502 | 519 | } |
| 503 | 520 | return $items; |
@@ -514,23 +531,25 @@ discard block |
||
| 514 | 531 | { |
| 515 | 532 | if($data instanceof TPriorityMap) |
| 516 | 533 | { |
| 517 | - if($this->getCount()>0) |
|
| 518 | - $this->clear(); |
|
| 534 | + if($this->getCount()>0) { |
|
| 535 | + $this->clear(); |
|
| 536 | + } |
|
| 519 | 537 | foreach($data->getPriorities() as $priority) { |
| 520 | 538 | foreach($data->itemsAtPriority($priority) as $key => $value) { |
| 521 | 539 | $this->add($key,$value,$priority); |
| 522 | 540 | } |
| 523 | 541 | } |
| 524 | - } |
|
| 525 | - else if(is_array($data)||$data instanceof Traversable) |
|
| 542 | + } else if(is_array($data)||$data instanceof Traversable) |
|
| 526 | 543 | { |
| 527 | - if($this->getCount()>0) |
|
| 528 | - $this->clear(); |
|
| 529 | - foreach($data as $key=>$value) |
|
| 530 | - $this->add($key,$value); |
|
| 544 | + if($this->getCount()>0) { |
|
| 545 | + $this->clear(); |
|
| 546 | + } |
|
| 547 | + foreach($data as $key=>$value) { |
|
| 548 | + $this->add($key,$value); |
|
| 549 | + } |
|
| 550 | + } else if($data!==null) { |
|
| 551 | + throw new TInvalidDataTypeException('map_data_not_iterable'); |
|
| 531 | 552 | } |
| 532 | - else if($data!==null) |
|
| 533 | - throw new TInvalidDataTypeException('map_data_not_iterable'); |
|
| 534 | 553 | } |
| 535 | 554 | |
| 536 | 555 | /** |
@@ -546,17 +565,18 @@ discard block |
||
| 546 | 565 | { |
| 547 | 566 | foreach($data->getPriorities() as $priority) |
| 548 | 567 | { |
| 549 | - foreach($data->itemsAtPriority($priority) as $key => $value) |
|
| 550 | - $this->add($key,$value,$priority); |
|
| 568 | + foreach($data->itemsAtPriority($priority) as $key => $value) { |
|
| 569 | + $this->add($key,$value,$priority); |
|
| 570 | + } |
|
| 551 | 571 | } |
| 552 | - } |
|
| 553 | - else if(is_array($data)||$data instanceof Traversable) |
|
| 572 | + } else if(is_array($data)||$data instanceof Traversable) |
|
| 554 | 573 | { |
| 555 | - foreach($data as $key=>$value) |
|
| 556 | - $this->add($key,$value); |
|
| 574 | + foreach($data as $key=>$value) { |
|
| 575 | + $this->add($key,$value); |
|
| 576 | + } |
|
| 577 | + } else if($data!==null) { |
|
| 578 | + throw new TInvalidDataTypeException('map_data_not_iterable'); |
|
| 557 | 579 | } |
| 558 | - else if($data!==null) |
|
| 559 | - throw new TInvalidDataTypeException('map_data_not_iterable'); |
|
| 560 | 580 | } |
| 561 | 581 | |
| 562 | 582 | /** |
@@ -49,6 +49,7 @@ discard block |
||
| 49 | 49 | * Constructor. |
| 50 | 50 | * Initializes the queue with an array or an iterable object. |
| 51 | 51 | * @param array|Iterator the intial data. Default is null, meaning no initialization. |
| 52 | + * @param integer[] $data |
|
| 52 | 53 | * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator. |
| 53 | 54 | */ |
| 54 | 55 | public function __construct($data=null) |
@@ -97,6 +98,7 @@ discard block |
||
| 97 | 98 | |
| 98 | 99 | /** |
| 99 | 100 | * @param mixed the item |
| 101 | + * @param integer $item |
|
| 100 | 102 | * @return boolean whether the queue contains the item |
| 101 | 103 | */ |
| 102 | 104 | public function contains($item) |
@@ -137,6 +139,7 @@ discard block |
||
| 137 | 139 | /** |
| 138 | 140 | * Adds an object to the end of the queue. |
| 139 | 141 | * @param mixed the item to be appended into the queue |
| 142 | + * @param integer $item |
|
| 140 | 143 | */ |
| 141 | 144 | public function enqueue($item) |
| 142 | 145 | { |
@@ -32,18 +32,18 @@ discard block |
||
| 32 | 32 | * @package System.Collections |
| 33 | 33 | * @since 3.1 |
| 34 | 34 | */ |
| 35 | -class TQueue extends TComponent implements IteratorAggregate,Countable |
|
| 35 | +class TQueue extends TComponent implements IteratorAggregate, Countable |
|
| 36 | 36 | { |
| 37 | 37 | /** |
| 38 | 38 | * internal data storage |
| 39 | 39 | * @var array |
| 40 | 40 | */ |
| 41 | - private $_d=array(); |
|
| 41 | + private $_d = array(); |
|
| 42 | 42 | /** |
| 43 | 43 | * number of items |
| 44 | 44 | * @var integer |
| 45 | 45 | */ |
| 46 | - private $_c=0; |
|
| 46 | + private $_c = 0; |
|
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 49 | * Constructor. |
@@ -51,9 +51,9 @@ discard block |
||
| 51 | 51 | * @param array|Iterator the intial data. Default is null, meaning no initialization. |
| 52 | 52 | * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator. |
| 53 | 53 | */ |
| 54 | - public function __construct($data=null) |
|
| 54 | + public function __construct($data = null) |
|
| 55 | 55 | { |
| 56 | - if($data!==null) |
|
| 56 | + if ($data !== null) |
|
| 57 | 57 | $this->copyFrom($data); |
| 58 | 58 | } |
| 59 | 59 | |
@@ -73,16 +73,16 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | public function copyFrom($data) |
| 75 | 75 | { |
| 76 | - if(is_array($data) || ($data instanceof Traversable)) |
|
| 76 | + if (is_array($data) || ($data instanceof Traversable)) |
|
| 77 | 77 | { |
| 78 | 78 | $this->clear(); |
| 79 | - foreach($data as $item) |
|
| 79 | + foreach ($data as $item) |
|
| 80 | 80 | { |
| 81 | - $this->_d[]=$item; |
|
| 81 | + $this->_d[] = $item; |
|
| 82 | 82 | ++$this->_c; |
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | - else if($data!==null) |
|
| 85 | + else if ($data !== null) |
|
| 86 | 86 | throw new TInvalidDataTypeException('queue_data_not_iterable'); |
| 87 | 87 | } |
| 88 | 88 | |
@@ -91,8 +91,8 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | public function clear() |
| 93 | 93 | { |
| 94 | - $this->_c=0; |
|
| 95 | - $this->_d=array(); |
|
| 94 | + $this->_c = 0; |
|
| 95 | + $this->_d = array(); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | /** |
@@ -101,7 +101,7 @@ discard block |
||
| 101 | 101 | */ |
| 102 | 102 | public function contains($item) |
| 103 | 103 | { |
| 104 | - return array_search($item,$this->_d,true)!==false; |
|
| 104 | + return array_search($item, $this->_d, true) !== false; |
|
| 105 | 105 | } |
| 106 | 106 | |
| 107 | 107 | /** |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | public function peek() |
| 114 | 114 | { |
| 115 | - if($this->_c===0) |
|
| 115 | + if ($this->_c === 0) |
|
| 116 | 116 | throw new TInvalidOperationException('queue_empty'); |
| 117 | 117 | else |
| 118 | 118 | return $this->_d[0]; |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | */ |
| 126 | 126 | public function dequeue() |
| 127 | 127 | { |
| 128 | - if($this->_c===0) |
|
| 128 | + if ($this->_c === 0) |
|
| 129 | 129 | throw new TInvalidOperationException('queue_empty'); |
| 130 | 130 | else |
| 131 | 131 | { |
@@ -151,7 +151,7 @@ discard block |
||
| 151 | 151 | */ |
| 152 | 152 | public function getIterator() |
| 153 | 153 | { |
| 154 | - return new ArrayIterator( $this->_d ); |
|
| 154 | + return new ArrayIterator($this->_d); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -207,9 +207,9 @@ discard block |
||
| 207 | 207 | */ |
| 208 | 208 | public function __construct(&$data) |
| 209 | 209 | { |
| 210 | - $this->_d=&$data; |
|
| 211 | - $this->_i=0; |
|
| 212 | - $this->_c=count($this->_d); |
|
| 210 | + $this->_d = &$data; |
|
| 211 | + $this->_i = 0; |
|
| 212 | + $this->_c = count($this->_d); |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | /** |
@@ -218,7 +218,7 @@ discard block |
||
| 218 | 218 | */ |
| 219 | 219 | public function rewind() |
| 220 | 220 | { |
| 221 | - $this->_i=0; |
|
| 221 | + $this->_i = 0; |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -257,7 +257,7 @@ discard block |
||
| 257 | 257 | */ |
| 258 | 258 | public function valid() |
| 259 | 259 | { |
| 260 | - return $this->_i<$this->_c; |
|
| 260 | + return $this->_i < $this->_c; |
|
| 261 | 261 | } |
| 262 | 262 | } |
| 263 | 263 | |
@@ -53,8 +53,9 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | public function __construct($data=null) |
| 55 | 55 | { |
| 56 | - if($data!==null) |
|
| 57 | - $this->copyFrom($data); |
|
| 56 | + if($data!==null) { |
|
| 57 | + $this->copyFrom($data); |
|
| 58 | + } |
|
| 58 | 59 | } |
| 59 | 60 | |
| 60 | 61 | /** |
@@ -81,9 +82,9 @@ discard block |
||
| 81 | 82 | $this->_d[]=$item; |
| 82 | 83 | ++$this->_c; |
| 83 | 84 | } |
| 85 | + } else if($data!==null) { |
|
| 86 | + throw new TInvalidDataTypeException('queue_data_not_iterable'); |
|
| 84 | 87 | } |
| 85 | - else if($data!==null) |
|
| 86 | - throw new TInvalidDataTypeException('queue_data_not_iterable'); |
|
| 87 | 88 | } |
| 88 | 89 | |
| 89 | 90 | /** |
@@ -112,10 +113,11 @@ discard block |
||
| 112 | 113 | */ |
| 113 | 114 | public function peek() |
| 114 | 115 | { |
| 115 | - if($this->_c===0) |
|
| 116 | - throw new TInvalidOperationException('queue_empty'); |
|
| 117 | - else |
|
| 118 | - return $this->_d[0]; |
|
| 116 | + if($this->_c===0) { |
|
| 117 | + throw new TInvalidOperationException('queue_empty'); |
|
| 118 | + } else { |
|
| 119 | + return $this->_d[0]; |
|
| 120 | + } |
|
| 119 | 121 | } |
| 120 | 122 | |
| 121 | 123 | /** |
@@ -125,9 +127,9 @@ discard block |
||
| 125 | 127 | */ |
| 126 | 128 | public function dequeue() |
| 127 | 129 | { |
| 128 | - if($this->_c===0) |
|
| 129 | - throw new TInvalidOperationException('queue_empty'); |
|
| 130 | - else |
|
| 130 | + if($this->_c===0) { |
|
| 131 | + throw new TInvalidOperationException('queue_empty'); |
|
| 132 | + } else |
|
| 131 | 133 | { |
| 132 | 134 | --$this->_c; |
| 133 | 135 | return array_shift($this->_d); |
@@ -46,6 +46,7 @@ |
||
| 46 | 46 | * Constructor. |
| 47 | 47 | * Initializes the stack with an array or an iterable object. |
| 48 | 48 | * @param array|Iterator the initial data. Default is null, meaning no initialization. |
| 49 | + * @param integer[] $data |
|
| 49 | 50 | * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator. |
| 50 | 51 | */ |
| 51 | 52 | public function __construct($data=null) |
@@ -1,13 +1,13 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * TStack, TStackIterator classes |
|
| 4 | - * |
|
| 5 | - * @author Qiang Xue <[email protected]> |
|
| 6 | - * @link https://github.com/pradosoft/prado |
|
| 7 | - * @copyright Copyright © 2005-2015 The PRADO Group |
|
| 8 | - * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | - * @package System.Collections |
|
| 10 | - */ |
|
| 3 | + * TStack, TStackIterator classes |
|
| 4 | + * |
|
| 5 | + * @author Qiang Xue <[email protected]> |
|
| 6 | + * @link https://github.com/pradosoft/prado |
|
| 7 | + * @copyright Copyright © 2005-2015 The PRADO Group |
|
| 8 | + * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | + * @package System.Collections |
|
| 10 | + */ |
|
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * TStack class |
@@ -29,18 +29,18 @@ discard block |
||
| 29 | 29 | * @package System.Collections |
| 30 | 30 | * @since 3.0 |
| 31 | 31 | */ |
| 32 | -class TStack extends TComponent implements IteratorAggregate,Countable |
|
| 32 | +class TStack extends TComponent implements IteratorAggregate, Countable |
|
| 33 | 33 | { |
| 34 | 34 | /** |
| 35 | 35 | * internal data storage |
| 36 | 36 | * @var array |
| 37 | 37 | */ |
| 38 | - private $_d=array(); |
|
| 38 | + private $_d = array(); |
|
| 39 | 39 | /** |
| 40 | 40 | * number of items |
| 41 | 41 | * @var integer |
| 42 | 42 | */ |
| 43 | - private $_c=0; |
|
| 43 | + private $_c = 0; |
|
| 44 | 44 | |
| 45 | 45 | /** |
| 46 | 46 | * Constructor. |
@@ -48,9 +48,9 @@ discard block |
||
| 48 | 48 | * @param array|Iterator the initial data. Default is null, meaning no initialization. |
| 49 | 49 | * @throws TInvalidDataTypeException If data is not null and neither an array nor an iterator. |
| 50 | 50 | */ |
| 51 | - public function __construct($data=null) |
|
| 51 | + public function __construct($data = null) |
|
| 52 | 52 | { |
| 53 | - if($data!==null) |
|
| 53 | + if ($data !== null) |
|
| 54 | 54 | $this->copyFrom($data); |
| 55 | 55 | } |
| 56 | 56 | |
@@ -70,16 +70,16 @@ discard block |
||
| 70 | 70 | */ |
| 71 | 71 | public function copyFrom($data) |
| 72 | 72 | { |
| 73 | - if(is_array($data) || ($data instanceof Traversable)) |
|
| 73 | + if (is_array($data) || ($data instanceof Traversable)) |
|
| 74 | 74 | { |
| 75 | 75 | $this->clear(); |
| 76 | - foreach($data as $item) |
|
| 76 | + foreach ($data as $item) |
|
| 77 | 77 | { |
| 78 | - $this->_d[]=$item; |
|
| 78 | + $this->_d[] = $item; |
|
| 79 | 79 | ++$this->_c; |
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | - else if($data!==null) |
|
| 82 | + else if ($data !== null) |
|
| 83 | 83 | throw new TInvalidDataTypeException('stack_data_not_iterable'); |
| 84 | 84 | } |
| 85 | 85 | |
@@ -88,8 +88,8 @@ discard block |
||
| 88 | 88 | */ |
| 89 | 89 | public function clear() |
| 90 | 90 | { |
| 91 | - $this->_c=0; |
|
| 92 | - $this->_d=array(); |
|
| 91 | + $this->_c = 0; |
|
| 92 | + $this->_d = array(); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
@@ -98,7 +98,7 @@ discard block |
||
| 98 | 98 | */ |
| 99 | 99 | public function contains($item) |
| 100 | 100 | { |
| 101 | - return array_search($item,$this->_d,true)!==false; |
|
| 101 | + return array_search($item, $this->_d, true) !== false; |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /** |
@@ -109,10 +109,10 @@ discard block |
||
| 109 | 109 | */ |
| 110 | 110 | public function peek() |
| 111 | 111 | { |
| 112 | - if($this->_c===0) |
|
| 112 | + if ($this->_c === 0) |
|
| 113 | 113 | throw new TInvalidOperationException('stack_empty'); |
| 114 | 114 | else |
| 115 | - return $this->_d[$this->_c-1]; |
|
| 115 | + return $this->_d[$this->_c - 1]; |
|
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | /** |
@@ -122,7 +122,7 @@ discard block |
||
| 122 | 122 | */ |
| 123 | 123 | public function pop() |
| 124 | 124 | { |
| 125 | - if($this->_c===0) |
|
| 125 | + if ($this->_c === 0) |
|
| 126 | 126 | throw new TInvalidOperationException('stack_empty'); |
| 127 | 127 | else |
| 128 | 128 | { |
@@ -148,7 +148,7 @@ discard block |
||
| 148 | 148 | */ |
| 149 | 149 | public function getIterator() |
| 150 | 150 | { |
| 151 | - return new ArrayIterator( $this->_d ); |
|
| 151 | + return new ArrayIterator($this->_d); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
@@ -204,9 +204,9 @@ discard block |
||
| 204 | 204 | */ |
| 205 | 205 | public function __construct(&$data) |
| 206 | 206 | { |
| 207 | - $this->_d=&$data; |
|
| 208 | - $this->_i=0; |
|
| 209 | - $this->_c=count($this->_d); |
|
| 207 | + $this->_d = &$data; |
|
| 208 | + $this->_i = 0; |
|
| 209 | + $this->_c = count($this->_d); |
|
| 210 | 210 | } |
| 211 | 211 | |
| 212 | 212 | /** |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | */ |
| 216 | 216 | public function rewind() |
| 217 | 217 | { |
| 218 | - $this->_i=0; |
|
| 218 | + $this->_i = 0; |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | /** |
@@ -254,7 +254,7 @@ discard block |
||
| 254 | 254 | */ |
| 255 | 255 | public function valid() |
| 256 | 256 | { |
| 257 | - return $this->_i<$this->_c; |
|
| 257 | + return $this->_i < $this->_c; |
|
| 258 | 258 | } |
| 259 | 259 | } |
| 260 | 260 | |
@@ -50,8 +50,9 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function __construct($data=null) |
| 52 | 52 | { |
| 53 | - if($data!==null) |
|
| 54 | - $this->copyFrom($data); |
|
| 53 | + if($data!==null) { |
|
| 54 | + $this->copyFrom($data); |
|
| 55 | + } |
|
| 55 | 56 | } |
| 56 | 57 | |
| 57 | 58 | /** |
@@ -78,9 +79,9 @@ discard block |
||
| 78 | 79 | $this->_d[]=$item; |
| 79 | 80 | ++$this->_c; |
| 80 | 81 | } |
| 82 | + } else if($data!==null) { |
|
| 83 | + throw new TInvalidDataTypeException('stack_data_not_iterable'); |
|
| 81 | 84 | } |
| 82 | - else if($data!==null) |
|
| 83 | - throw new TInvalidDataTypeException('stack_data_not_iterable'); |
|
| 84 | 85 | } |
| 85 | 86 | |
| 86 | 87 | /** |
@@ -109,10 +110,11 @@ discard block |
||
| 109 | 110 | */ |
| 110 | 111 | public function peek() |
| 111 | 112 | { |
| 112 | - if($this->_c===0) |
|
| 113 | - throw new TInvalidOperationException('stack_empty'); |
|
| 114 | - else |
|
| 115 | - return $this->_d[$this->_c-1]; |
|
| 113 | + if($this->_c===0) { |
|
| 114 | + throw new TInvalidOperationException('stack_empty'); |
|
| 115 | + } else { |
|
| 116 | + return $this->_d[$this->_c-1]; |
|
| 117 | + } |
|
| 116 | 118 | } |
| 117 | 119 | |
| 118 | 120 | /** |
@@ -122,9 +124,9 @@ discard block |
||
| 122 | 124 | */ |
| 123 | 125 | public function pop() |
| 124 | 126 | { |
| 125 | - if($this->_c===0) |
|
| 126 | - throw new TInvalidOperationException('stack_empty'); |
|
| 127 | - else |
|
| 127 | + if($this->_c===0) { |
|
| 128 | + throw new TInvalidOperationException('stack_empty'); |
|
| 129 | + } else |
|
| 128 | 130 | { |
| 129 | 131 | --$this->_c; |
| 130 | 132 | return array_pop($this->_d); |
@@ -208,6 +208,7 @@ discard block |
||
| 208 | 208 | * @param string active record class name. |
| 209 | 209 | * @param array row data |
| 210 | 210 | * @param array foreign key column names |
| 211 | + * @param string $type |
|
| 211 | 212 | * @return TActiveRecord |
| 212 | 213 | */ |
| 213 | 214 | protected function createFkObject($type,$row,$foreignKeys) |
@@ -227,6 +228,7 @@ discard block |
||
| 227 | 228 | * @param TTableInfo association table info |
| 228 | 229 | * @param array field names |
| 229 | 230 | * @param array field values |
| 231 | + * @param TActiveRecordCriteria $criteria |
|
| 230 | 232 | */ |
| 231 | 233 | public function createCommand($criteria, $foreignKeys,$indexValues,$sourceKeys) |
| 232 | 234 | { |
@@ -341,6 +343,10 @@ discard block |
||
| 341 | 343 | return $this->getCommandBuilder()->onExecuteCommand($command, $command->execute()) > 0; |
| 342 | 344 | } |
| 343 | 345 | |
| 346 | + /** |
|
| 347 | + * @param TActiveRecord $obj |
|
| 348 | + * @param TDbCommandBuilder $builder |
|
| 349 | + */ |
|
| 344 | 350 | private function updateAssociationTable($obj,$fkObjects, $builder) |
| 345 | 351 | { |
| 346 | 352 | $source = $this->getSourceRecordValues($obj); |
@@ -90,10 +90,10 @@ |
||
| 90 | 90 | private $_association_columns=array(); |
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | - * Get the foreign key index values from the results and make calls to the |
|
| 94 | - * database to find the corresponding foreign objects using association table. |
|
| 95 | - * @param array original results. |
|
| 96 | - */ |
|
| 93 | + * Get the foreign key index values from the results and make calls to the |
|
| 94 | + * database to find the corresponding foreign objects using association table. |
|
| 95 | + * @param array original results. |
|
| 96 | + */ |
|
| 97 | 97 | protected function collectForeignObjects(&$results) |
| 98 | 98 | { |
| 99 | 99 | list($sourceKeys, $foreignKeys) = $this->getRelationForeignKeys(); |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | private $_association; |
| 88 | 88 | private $_sourceTable; |
| 89 | 89 | private $_foreignTable; |
| 90 | - private $_association_columns=array(); |
|
| 90 | + private $_association_columns = array(); |
|
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | 93 | * Get the foreign key index values from the results and make calls to the |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | list($sourceKeys, $foreignKeys) = $this->getRelationForeignKeys(); |
| 100 | 100 | $properties = array_values($sourceKeys); |
| 101 | 101 | $indexValues = $this->getIndexValues($properties, $results); |
| 102 | - $this->fetchForeignObjects($results, $foreignKeys,$indexValues,$sourceKeys); |
|
| 102 | + $this->fetchForeignObjects($results, $foreignKeys, $indexValues, $sourceKeys); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
@@ -119,16 +119,16 @@ discard block |
||
| 119 | 119 | */ |
| 120 | 120 | protected function getAssociationTable() |
| 121 | 121 | { |
| 122 | - if($this->_association===null) |
|
| 122 | + if ($this->_association === null) |
|
| 123 | 123 | { |
| 124 | 124 | $gateway = $this->getSourceRecord()->getRecordGateway(); |
| 125 | 125 | $conn = $this->getSourceRecord()->getDbConnection(); |
| 126 | 126 | //table name may include the fk column name separated with a dot. |
| 127 | 127 | $table = explode('.', $this->getContext()->getAssociationTable()); |
| 128 | - if(count($table)>1) |
|
| 128 | + if (count($table) > 1) |
|
| 129 | 129 | { |
| 130 | 130 | $columns = preg_replace('/^\((.*)\)/', '\1', $table[1]); |
| 131 | - $this->_association_columns = preg_split('/\s*[, ]\*/',$columns); |
|
| 131 | + $this->_association_columns = preg_split('/\s*[, ]\*/', $columns); |
|
| 132 | 132 | } |
| 133 | 133 | $this->_association = $gateway->getTableInfo($conn, $table[0]); |
| 134 | 134 | } |
@@ -140,7 +140,7 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | protected function getSourceTable() |
| 142 | 142 | { |
| 143 | - if($this->_sourceTable===null) |
|
| 143 | + if ($this->_sourceTable === null) |
|
| 144 | 144 | { |
| 145 | 145 | $gateway = $this->getSourceRecord()->getRecordGateway(); |
| 146 | 146 | $this->_sourceTable = $gateway->getRecordTableInfo($this->getSourceRecord()); |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | protected function getForeignTable() |
| 155 | 155 | { |
| 156 | - if($this->_foreignTable===null) |
|
| 156 | + if ($this->_foreignTable === null) |
|
| 157 | 157 | { |
| 158 | 158 | $gateway = $this->getSourceRecord()->getRecordGateway(); |
| 159 | 159 | $fkObject = $this->getContext()->getForeignRecordFinder(); |
@@ -185,20 +185,20 @@ discard block |
||
| 185 | 185 | * @param array field names |
| 186 | 186 | * @param array foreign key index values. |
| 187 | 187 | */ |
| 188 | - protected function fetchForeignObjects(&$results,$foreignKeys,$indexValues,$sourceKeys) |
|
| 188 | + protected function fetchForeignObjects(&$results, $foreignKeys, $indexValues, $sourceKeys) |
|
| 189 | 189 | { |
| 190 | 190 | $criteria = $this->getCriteria(); |
| 191 | 191 | $finder = $this->getContext()->getForeignRecordFinder(); |
| 192 | 192 | $type = get_class($finder); |
| 193 | - $command = $this->createCommand($criteria, $foreignKeys,$indexValues,$sourceKeys); |
|
| 193 | + $command = $this->createCommand($criteria, $foreignKeys, $indexValues, $sourceKeys); |
|
| 194 | 194 | $srcProps = array_keys($sourceKeys); |
| 195 | - $collections=array(); |
|
| 196 | - foreach($this->getCommandBuilder()->onExecuteCommand($command, $command->query()) as $row) |
|
| 195 | + $collections = array(); |
|
| 196 | + foreach ($this->getCommandBuilder()->onExecuteCommand($command, $command->query()) as $row) |
|
| 197 | 197 | { |
| 198 | 198 | $hash = $this->getObjectHash($row, $srcProps); |
| 199 | - foreach($srcProps as $column) |
|
| 199 | + foreach ($srcProps as $column) |
|
| 200 | 200 | unset($row[$column]); |
| 201 | - $obj = $this->createFkObject($type,$row,$foreignKeys); |
|
| 201 | + $obj = $this->createFkObject($type, $row, $foreignKeys); |
|
| 202 | 202 | $collections[$hash][] = $obj; |
| 203 | 203 | } |
| 204 | 204 | $this->setResultCollection($results, $collections, array_values($sourceKeys)); |
@@ -210,13 +210,13 @@ discard block |
||
| 210 | 210 | * @param array foreign key column names |
| 211 | 211 | * @return TActiveRecord |
| 212 | 212 | */ |
| 213 | - protected function createFkObject($type,$row,$foreignKeys) |
|
| 213 | + protected function createFkObject($type, $row, $foreignKeys) |
|
| 214 | 214 | { |
| 215 | 215 | $obj = TActiveRecord::createRecord($type, $row); |
| 216 | - if(count($this->_association_columns) > 0) |
|
| 216 | + if (count($this->_association_columns) > 0) |
|
| 217 | 217 | { |
| 218 | - $i=0; |
|
| 219 | - foreach($foreignKeys as $ref=>$fk) |
|
| 218 | + $i = 0; |
|
| 219 | + foreach ($foreignKeys as $ref=>$fk) |
|
| 220 | 220 | $obj->setColumnValue($ref, $row[$this->_association_columns[$i++]]); |
| 221 | 221 | } |
| 222 | 222 | return $obj; |
@@ -228,13 +228,13 @@ discard block |
||
| 228 | 228 | * @param array field names |
| 229 | 229 | * @param array field values |
| 230 | 230 | */ |
| 231 | - public function createCommand($criteria, $foreignKeys,$indexValues,$sourceKeys) |
|
| 231 | + public function createCommand($criteria, $foreignKeys, $indexValues, $sourceKeys) |
|
| 232 | 232 | { |
| 233 | - $innerJoin = $this->getAssociationJoin($foreignKeys,$indexValues,$sourceKeys); |
|
| 233 | + $innerJoin = $this->getAssociationJoin($foreignKeys, $indexValues, $sourceKeys); |
|
| 234 | 234 | $fkTable = $this->getForeignTable()->getTableFullName(); |
| 235 | 235 | $srcColumns = $this->getSourceColumns($sourceKeys); |
| 236 | - if(($where=$criteria->getCondition())===null) |
|
| 237 | - $where='1=1'; |
|
| 236 | + if (($where = $criteria->getCondition()) === null) |
|
| 237 | + $where = '1=1'; |
|
| 238 | 238 | $sql = "SELECT {$fkTable}.*, {$srcColumns} FROM {$fkTable} {$innerJoin} WHERE {$where}"; |
| 239 | 239 | |
| 240 | 240 | $parameters = $criteria->getParameters()->toArray(); |
@@ -243,7 +243,7 @@ discard block |
||
| 243 | 243 | $offset = $criteria->getOffset(); |
| 244 | 244 | |
| 245 | 245 | $builder = $this->getForeignCommandBuilder()->getBuilder(); |
| 246 | - $command = $builder->applyCriterias($sql,$parameters,$ordering,$limit,$offset); |
|
| 246 | + $command = $builder->applyCriterias($sql, $parameters, $ordering, $limit, $offset); |
|
| 247 | 247 | $this->getCommandBuilder()->onCreateCommand($command, $criteria); |
| 248 | 248 | return $command; |
| 249 | 249 | } |
@@ -254,12 +254,12 @@ discard block |
||
| 254 | 254 | */ |
| 255 | 255 | protected function getSourceColumns($sourceKeys) |
| 256 | 256 | { |
| 257 | - $columns=array(); |
|
| 257 | + $columns = array(); |
|
| 258 | 258 | $table = $this->getAssociationTable(); |
| 259 | 259 | $tableName = $table->getTableFullName(); |
| 260 | - $columnNames = array_merge(array_keys($sourceKeys),$this->_association_columns); |
|
| 261 | - foreach($columnNames as $name) |
|
| 262 | - $columns[] = $tableName.'.'.$table->getColumn($name)->getColumnName(); |
|
| 260 | + $columnNames = array_merge(array_keys($sourceKeys), $this->_association_columns); |
|
| 261 | + foreach ($columnNames as $name) |
|
| 262 | + $columns[] = $tableName . '.' . $table->getColumn($name)->getColumnName(); |
|
| 263 | 263 | return implode(', ', $columns); |
| 264 | 264 | } |
| 265 | 265 | |
@@ -270,9 +270,9 @@ discard block |
||
| 270 | 270 | * @param array source table column names. |
| 271 | 271 | * @return string inner join condition for M-N relationship via association table. |
| 272 | 272 | */ |
| 273 | - protected function getAssociationJoin($foreignKeys,$indexValues,$sourceKeys) |
|
| 273 | + protected function getAssociationJoin($foreignKeys, $indexValues, $sourceKeys) |
|
| 274 | 274 | { |
| 275 | - $refInfo= $this->getAssociationTable(); |
|
| 275 | + $refInfo = $this->getAssociationTable(); |
|
| 276 | 276 | $fkInfo = $this->getForeignTable(); |
| 277 | 277 | |
| 278 | 278 | $refTable = $refInfo->getTableFullName(); |
@@ -280,10 +280,10 @@ discard block |
||
| 280 | 280 | |
| 281 | 281 | $joins = array(); |
| 282 | 282 | $hasAssociationColumns = count($this->_association_columns) > 0; |
| 283 | - $i=0; |
|
| 284 | - foreach($foreignKeys as $ref=>$fk) |
|
| 283 | + $i = 0; |
|
| 284 | + foreach ($foreignKeys as $ref=>$fk) |
|
| 285 | 285 | { |
| 286 | - if($hasAssociationColumns) |
|
| 286 | + if ($hasAssociationColumns) |
|
| 287 | 287 | $refField = $refInfo->getColumn($this->_association_columns[$i++])->getColumnName(); |
| 288 | 288 | else |
| 289 | 289 | $refField = $refInfo->getColumn($ref)->getColumnName(); |
@@ -291,7 +291,7 @@ discard block |
||
| 291 | 291 | $joins[] = "{$fkTable}.{$fkField} = {$refTable}.{$refField}"; |
| 292 | 292 | } |
| 293 | 293 | $joinCondition = implode(' AND ', $joins); |
| 294 | - $index = $this->getCommandBuilder()->getIndexKeyCondition($refInfo,array_keys($sourceKeys), $indexValues); |
|
| 294 | + $index = $this->getCommandBuilder()->getIndexKeyCondition($refInfo, array_keys($sourceKeys), $indexValues); |
|
| 295 | 295 | return "INNER JOIN {$refTable} ON ({$joinCondition}) AND {$index}"; |
| 296 | 296 | } |
| 297 | 297 | |
@@ -303,12 +303,12 @@ discard block |
||
| 303 | 303 | { |
| 304 | 304 | $obj = $this->getContext()->getSourceRecord(); |
| 305 | 305 | $fkObjects = &$obj->{$this->getContext()->getProperty()}; |
| 306 | - $success=true; |
|
| 307 | - if(($total = count($fkObjects))> 0) |
|
| 306 | + $success = true; |
|
| 307 | + if (($total = count($fkObjects)) > 0) |
|
| 308 | 308 | { |
| 309 | 309 | $source = $this->getSourceRecord(); |
| 310 | 310 | $builder = $this->getAssociationTableCommandBuilder(); |
| 311 | - for($i=0;$i<$total;$i++) |
|
| 311 | + for ($i = 0; $i < $total; $i++) |
|
| 312 | 312 | $success = $fkObjects[$i]->save() && $success; |
| 313 | 313 | return $this->updateAssociationTable($obj, $fkObjects, $builder) && $success; |
| 314 | 314 | } |
@@ -324,33 +324,33 @@ discard block |
||
| 324 | 324 | return $this->getAssociationTable()->createCommandBuilder($conn); |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | - private function hasAssociationData($builder,$data) |
|
| 327 | + private function hasAssociationData($builder, $data) |
|
| 328 | 328 | { |
| 329 | - $condition=array(); |
|
| 329 | + $condition = array(); |
|
| 330 | 330 | $table = $this->getAssociationTable(); |
| 331 | - foreach($data as $name=>$value) |
|
| 332 | - $condition[] = $table->getColumn($name)->getColumnName().' = ?'; |
|
| 333 | - $command = $builder->createCountCommand(implode(' AND ', $condition),array_values($data)); |
|
| 331 | + foreach ($data as $name=>$value) |
|
| 332 | + $condition[] = $table->getColumn($name)->getColumnName() . ' = ?'; |
|
| 333 | + $command = $builder->createCountCommand(implode(' AND ', $condition), array_values($data)); |
|
| 334 | 334 | $result = $this->getCommandBuilder()->onExecuteCommand($command, intval($command->queryScalar())); |
| 335 | 335 | return intval($result) > 0; |
| 336 | 336 | } |
| 337 | 337 | |
| 338 | - private function addAssociationData($builder,$data) |
|
| 338 | + private function addAssociationData($builder, $data) |
|
| 339 | 339 | { |
| 340 | 340 | $command = $builder->createInsertCommand($data); |
| 341 | 341 | return $this->getCommandBuilder()->onExecuteCommand($command, $command->execute()) > 0; |
| 342 | 342 | } |
| 343 | 343 | |
| 344 | - private function updateAssociationTable($obj,$fkObjects, $builder) |
|
| 344 | + private function updateAssociationTable($obj, $fkObjects, $builder) |
|
| 345 | 345 | { |
| 346 | 346 | $source = $this->getSourceRecordValues($obj); |
| 347 | 347 | $foreignKeys = $this->findForeignKeys($this->getAssociationTable(), $fkObjects[0]); |
| 348 | - $success=true; |
|
| 349 | - foreach($fkObjects as $fkObject) |
|
| 348 | + $success = true; |
|
| 349 | + foreach ($fkObjects as $fkObject) |
|
| 350 | 350 | { |
| 351 | - $data = array_merge($source, $this->getForeignObjectValues($foreignKeys,$fkObject)); |
|
| 352 | - if(!$this->hasAssociationData($builder,$data)) |
|
| 353 | - $success = $this->addAssociationData($builder,$data) && $success; |
|
| 351 | + $data = array_merge($source, $this->getForeignObjectValues($foreignKeys, $fkObject)); |
|
| 352 | + if (!$this->hasAssociationData($builder, $data)) |
|
| 353 | + $success = $this->addAssociationData($builder, $data) && $success; |
|
| 354 | 354 | } |
| 355 | 355 | return $success; |
| 356 | 356 | } |
@@ -360,16 +360,16 @@ discard block |
||
| 360 | 360 | $sourceKeys = $this->findForeignKeys($this->getAssociationTable(), $obj); |
| 361 | 361 | $indexValues = $this->getIndexValues(array_values($sourceKeys), $obj); |
| 362 | 362 | $data = array(); |
| 363 | - $i=0; |
|
| 364 | - foreach($sourceKeys as $name=>$srcKey) |
|
| 363 | + $i = 0; |
|
| 364 | + foreach ($sourceKeys as $name=>$srcKey) |
|
| 365 | 365 | $data[$name] = $indexValues[0][$i++]; |
| 366 | 366 | return $data; |
| 367 | 367 | } |
| 368 | 368 | |
| 369 | - private function getForeignObjectValues($foreignKeys,$fkObject) |
|
| 369 | + private function getForeignObjectValues($foreignKeys, $fkObject) |
|
| 370 | 370 | { |
| 371 | - $data=array(); |
|
| 372 | - foreach($foreignKeys as $name=>$fKey) |
|
| 371 | + $data = array(); |
|
| 372 | + foreach ($foreignKeys as $name=>$fKey) |
|
| 373 | 373 | $data[$name] = $fkObject->getColumnValue($fKey); |
| 374 | 374 | return $data; |
| 375 | 375 | } |
@@ -196,8 +196,9 @@ discard block |
||
| 196 | 196 | foreach($this->getCommandBuilder()->onExecuteCommand($command, $command->query()) as $row) |
| 197 | 197 | { |
| 198 | 198 | $hash = $this->getObjectHash($row, $srcProps); |
| 199 | - foreach($srcProps as $column) |
|
| 200 | - unset($row[$column]); |
|
| 199 | + foreach($srcProps as $column) { |
|
| 200 | + unset($row[$column]); |
|
| 201 | + } |
|
| 201 | 202 | $obj = $this->createFkObject($type,$row,$foreignKeys); |
| 202 | 203 | $collections[$hash][] = $obj; |
| 203 | 204 | } |
@@ -216,8 +217,9 @@ discard block |
||
| 216 | 217 | if(count($this->_association_columns) > 0) |
| 217 | 218 | { |
| 218 | 219 | $i=0; |
| 219 | - foreach($foreignKeys as $ref=>$fk) |
|
| 220 | - $obj->setColumnValue($ref, $row[$this->_association_columns[$i++]]); |
|
| 220 | + foreach($foreignKeys as $ref=>$fk) { |
|
| 221 | + $obj->setColumnValue($ref, $row[$this->_association_columns[$i++]]); |
|
| 222 | + } |
|
| 221 | 223 | } |
| 222 | 224 | return $obj; |
| 223 | 225 | } |
@@ -233,8 +235,9 @@ discard block |
||
| 233 | 235 | $innerJoin = $this->getAssociationJoin($foreignKeys,$indexValues,$sourceKeys); |
| 234 | 236 | $fkTable = $this->getForeignTable()->getTableFullName(); |
| 235 | 237 | $srcColumns = $this->getSourceColumns($sourceKeys); |
| 236 | - if(($where=$criteria->getCondition())===null) |
|
| 237 | - $where='1=1'; |
|
| 238 | + if(($where=$criteria->getCondition())===null) { |
|
| 239 | + $where='1=1'; |
|
| 240 | + } |
|
| 238 | 241 | $sql = "SELECT {$fkTable}.*, {$srcColumns} FROM {$fkTable} {$innerJoin} WHERE {$where}"; |
| 239 | 242 | |
| 240 | 243 | $parameters = $criteria->getParameters()->toArray(); |
@@ -258,8 +261,9 @@ discard block |
||
| 258 | 261 | $table = $this->getAssociationTable(); |
| 259 | 262 | $tableName = $table->getTableFullName(); |
| 260 | 263 | $columnNames = array_merge(array_keys($sourceKeys),$this->_association_columns); |
| 261 | - foreach($columnNames as $name) |
|
| 262 | - $columns[] = $tableName.'.'.$table->getColumn($name)->getColumnName(); |
|
| 264 | + foreach($columnNames as $name) { |
|
| 265 | + $columns[] = $tableName.'.'.$table->getColumn($name)->getColumnName(); |
|
| 266 | + } |
|
| 263 | 267 | return implode(', ', $columns); |
| 264 | 268 | } |
| 265 | 269 | |
@@ -283,10 +287,11 @@ discard block |
||
| 283 | 287 | $i=0; |
| 284 | 288 | foreach($foreignKeys as $ref=>$fk) |
| 285 | 289 | { |
| 286 | - if($hasAssociationColumns) |
|
| 287 | - $refField = $refInfo->getColumn($this->_association_columns[$i++])->getColumnName(); |
|
| 288 | - else |
|
| 289 | - $refField = $refInfo->getColumn($ref)->getColumnName(); |
|
| 290 | + if($hasAssociationColumns) { |
|
| 291 | + $refField = $refInfo->getColumn($this->_association_columns[$i++])->getColumnName(); |
|
| 292 | + } else { |
|
| 293 | + $refField = $refInfo->getColumn($ref)->getColumnName(); |
|
| 294 | + } |
|
| 290 | 295 | $fkField = $fkInfo->getColumn($fk)->getColumnName(); |
| 291 | 296 | $joins[] = "{$fkTable}.{$fkField} = {$refTable}.{$refField}"; |
| 292 | 297 | } |
@@ -308,8 +313,9 @@ discard block |
||
| 308 | 313 | { |
| 309 | 314 | $source = $this->getSourceRecord(); |
| 310 | 315 | $builder = $this->getAssociationTableCommandBuilder(); |
| 311 | - for($i=0;$i<$total;$i++) |
|
| 312 | - $success = $fkObjects[$i]->save() && $success; |
|
| 316 | + for($i=0;$i<$total;$i++) { |
|
| 317 | + $success = $fkObjects[$i]->save() && $success; |
|
| 318 | + } |
|
| 313 | 319 | return $this->updateAssociationTable($obj, $fkObjects, $builder) && $success; |
| 314 | 320 | } |
| 315 | 321 | return $success; |
@@ -328,8 +334,9 @@ discard block |
||
| 328 | 334 | { |
| 329 | 335 | $condition=array(); |
| 330 | 336 | $table = $this->getAssociationTable(); |
| 331 | - foreach($data as $name=>$value) |
|
| 332 | - $condition[] = $table->getColumn($name)->getColumnName().' = ?'; |
|
| 337 | + foreach($data as $name=>$value) { |
|
| 338 | + $condition[] = $table->getColumn($name)->getColumnName().' = ?'; |
|
| 339 | + } |
|
| 333 | 340 | $command = $builder->createCountCommand(implode(' AND ', $condition),array_values($data)); |
| 334 | 341 | $result = $this->getCommandBuilder()->onExecuteCommand($command, intval($command->queryScalar())); |
| 335 | 342 | return intval($result) > 0; |
@@ -349,8 +356,9 @@ discard block |
||
| 349 | 356 | foreach($fkObjects as $fkObject) |
| 350 | 357 | { |
| 351 | 358 | $data = array_merge($source, $this->getForeignObjectValues($foreignKeys,$fkObject)); |
| 352 | - if(!$this->hasAssociationData($builder,$data)) |
|
| 353 | - $success = $this->addAssociationData($builder,$data) && $success; |
|
| 359 | + if(!$this->hasAssociationData($builder,$data)) { |
|
| 360 | + $success = $this->addAssociationData($builder,$data) && $success; |
|
| 361 | + } |
|
| 354 | 362 | } |
| 355 | 363 | return $success; |
| 356 | 364 | } |
@@ -361,16 +369,18 @@ discard block |
||
| 361 | 369 | $indexValues = $this->getIndexValues(array_values($sourceKeys), $obj); |
| 362 | 370 | $data = array(); |
| 363 | 371 | $i=0; |
| 364 | - foreach($sourceKeys as $name=>$srcKey) |
|
| 365 | - $data[$name] = $indexValues[0][$i++]; |
|
| 372 | + foreach($sourceKeys as $name=>$srcKey) { |
|
| 373 | + $data[$name] = $indexValues[0][$i++]; |
|
| 374 | + } |
|
| 366 | 375 | return $data; |
| 367 | 376 | } |
| 368 | 377 | |
| 369 | 378 | private function getForeignObjectValues($foreignKeys,$fkObject) |
| 370 | 379 | { |
| 371 | 380 | $data=array(); |
| 372 | - foreach($foreignKeys as $name=>$fKey) |
|
| 373 | - $data[$name] = $fkObject->getColumnValue($fKey); |
|
| 381 | + foreach($foreignKeys as $name=>$fKey) { |
|
| 382 | + $data[$name] = $fkObject->getColumnValue($fKey); |
|
| 383 | + } |
|
| 374 | 384 | return $data; |
| 375 | 385 | } |
| 376 | 386 | } |
@@ -92,6 +92,7 @@ discard block |
||
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | 94 | * Fetch results for current relationship. |
| 95 | + * @param TActiveRecord $obj |
|
| 95 | 96 | * @return boolean always true. |
| 96 | 97 | */ |
| 97 | 98 | public function fetchResultsInto($obj) |
@@ -104,7 +105,6 @@ discard block |
||
| 104 | 105 | * Returns foreign keys in $fromRecord with source column names as key |
| 105 | 106 | * and foreign column names in the corresponding $matchesRecord as value. |
| 106 | 107 | * The method returns the first matching foreign key between these 2 records. |
| 107 | - * @param TActiveRecord $fromRecord |
|
| 108 | 108 | * @param TActiveRecord $matchesRecord |
| 109 | 109 | * @return array foreign keys with source column names as key and foreign column names as value. |
| 110 | 110 | */ |
@@ -212,6 +212,7 @@ discard block |
||
| 212 | 212 | * @param array source property names |
| 213 | 213 | * @param array foreign objects |
| 214 | 214 | * @param array foreign object field names. |
| 215 | + * @param TActiveRecord[] $fkObjects |
|
| 215 | 216 | */ |
| 216 | 217 | protected function populateResult(&$results,$properties,&$fkObjects,$fields) |
| 217 | 218 | { |
@@ -71,21 +71,21 @@ discard block |
||
| 71 | 71 | * @param array method arguments |
| 72 | 72 | * @return mixed TActiveRecord or array of TActiveRecord results depending on the method called. |
| 73 | 73 | */ |
| 74 | - public function __call($method,$args) |
|
| 74 | + public function __call($method, $args) |
|
| 75 | 75 | { |
| 76 | - static $stack=array(); |
|
| 76 | + static $stack = array(); |
|
| 77 | 77 | |
| 78 | - $results = call_user_func_array(array($this->getSourceRecord(),$method),$args); |
|
| 78 | + $results = call_user_func_array(array($this->getSourceRecord(), $method), $args); |
|
| 79 | 79 | $validArray = is_array($results) && count($results) > 0; |
| 80 | - if($validArray || $results instanceof ArrayAccess || $results instanceof TActiveRecord) |
|
| 80 | + if ($validArray || $results instanceof ArrayAccess || $results instanceof TActiveRecord) |
|
| 81 | 81 | { |
| 82 | 82 | $this->collectForeignObjects($results); |
| 83 | - while($obj = array_pop($stack)) |
|
| 83 | + while ($obj = array_pop($stack)) |
|
| 84 | 84 | $obj->collectForeignObjects($results); |
| 85 | 85 | } |
| 86 | - else if($results instanceof TActiveRecordRelation) |
|
| 86 | + else if ($results instanceof TActiveRecordRelation) |
|
| 87 | 87 | $stack[] = $this; //call it later |
| 88 | - else if($results === null || !$validArray) |
|
| 88 | + else if ($results === null || !$validArray) |
|
| 89 | 89 | $stack = array(); |
| 90 | 90 | return $results; |
| 91 | 91 | } |
@@ -108,24 +108,24 @@ discard block |
||
| 108 | 108 | * @param TActiveRecord $matchesRecord |
| 109 | 109 | * @return array foreign keys with source column names as key and foreign column names as value. |
| 110 | 110 | */ |
| 111 | - protected function findForeignKeys($from, $matchesRecord, $loose=false) |
|
| 111 | + protected function findForeignKeys($from, $matchesRecord, $loose = false) |
|
| 112 | 112 | { |
| 113 | 113 | $gateway = $matchesRecord->getRecordGateway(); |
| 114 | 114 | $recordTableInfo = $gateway->getRecordTableInfo($matchesRecord); |
| 115 | 115 | $matchingTableName = strtolower($recordTableInfo->getTableName()); |
| 116 | 116 | $matchingFullTableName = strtolower($recordTableInfo->getTableFullName()); |
| 117 | - $tableInfo=$from; |
|
| 118 | - if($from instanceof TActiveRecord) |
|
| 117 | + $tableInfo = $from; |
|
| 118 | + if ($from instanceof TActiveRecord) |
|
| 119 | 119 | $tableInfo = $gateway->getRecordTableInfo($from); |
| 120 | 120 | //find first non-empty FK |
| 121 | - foreach($tableInfo->getForeignKeys() as $fkeys) |
|
| 121 | + foreach ($tableInfo->getForeignKeys() as $fkeys) |
|
| 122 | 122 | { |
| 123 | 123 | $fkTable = strtolower($fkeys['table']); |
| 124 | - if($fkTable===$matchingTableName || $fkTable===$matchingFullTableName) |
|
| 124 | + if ($fkTable === $matchingTableName || $fkTable === $matchingFullTableName) |
|
| 125 | 125 | { |
| 126 | 126 | $hasFkField = !$loose && $this->getContext()->hasFkField(); |
| 127 | 127 | $key = $hasFkField ? $this->getFkFields($fkeys['keys']) : $fkeys['keys']; |
| 128 | - if(!empty($key)) |
|
| 128 | + if (!empty($key)) |
|
| 129 | 129 | return $key; |
| 130 | 130 | } |
| 131 | 131 | } |
@@ -152,9 +152,9 @@ discard block |
||
| 152 | 152 | $matching = array(); |
| 153 | 153 | preg_match_all('/\s*(\S+\.)?([\w-]+)\s*/', $this->getContext()->getFkField(), $matching); |
| 154 | 154 | $fields = array(); |
| 155 | - foreach($fkeys as $fkName => $field) |
|
| 155 | + foreach ($fkeys as $fkName => $field) |
|
| 156 | 156 | { |
| 157 | - if(in_array($fkName, $matching[2])) |
|
| 157 | + if (in_array($fkName, $matching[2])) |
|
| 158 | 158 | $fields[$fkName] = $field; |
| 159 | 159 | } |
| 160 | 160 | return $fields; |
@@ -167,9 +167,9 @@ discard block |
||
| 167 | 167 | */ |
| 168 | 168 | protected function getObjectHash($obj, $properties) |
| 169 | 169 | { |
| 170 | - $ids=array(); |
|
| 171 | - foreach($properties as $property) |
|
| 172 | - $ids[] = is_object($obj) ? (string)$obj->getColumnValue($property) : (string)$obj[$property]; |
|
| 170 | + $ids = array(); |
|
| 171 | + foreach ($properties as $property) |
|
| 172 | + $ids[] = is_object($obj) ? (string) $obj->getColumnValue($property) : (string) $obj[$property]; |
|
| 173 | 173 | return serialize($ids); |
| 174 | 174 | } |
| 175 | 175 | |
@@ -193,13 +193,13 @@ discard block |
||
| 193 | 193 | */ |
| 194 | 194 | protected function getIndexValues($keys, $results) |
| 195 | 195 | { |
| 196 | - if(!is_array($results) && !$results instanceof ArrayAccess) |
|
| 196 | + if (!is_array($results) && !$results instanceof ArrayAccess) |
|
| 197 | 197 | $results = array($results); |
| 198 | - $values=array(); |
|
| 199 | - foreach($results as $result) |
|
| 198 | + $values = array(); |
|
| 199 | + foreach ($results as $result) |
|
| 200 | 200 | { |
| 201 | 201 | $value = array(); |
| 202 | - foreach($keys as $name) |
|
| 202 | + foreach ($keys as $name) |
|
| 203 | 203 | $value[] = $result->getColumnValue($name); |
| 204 | 204 | $values[] = $value; |
| 205 | 205 | } |
@@ -213,11 +213,11 @@ discard block |
||
| 213 | 213 | * @param array foreign objects |
| 214 | 214 | * @param array foreign object field names. |
| 215 | 215 | */ |
| 216 | - protected function populateResult(&$results,$properties,&$fkObjects,$fields) |
|
| 216 | + protected function populateResult(&$results, $properties, &$fkObjects, $fields) |
|
| 217 | 217 | { |
| 218 | - $collections=array(); |
|
| 219 | - foreach($fkObjects as $fkObject) |
|
| 220 | - $collections[$this->getObjectHash($fkObject, $fields)][]=$fkObject; |
|
| 218 | + $collections = array(); |
|
| 219 | + foreach ($fkObjects as $fkObject) |
|
| 220 | + $collections[$this->getObjectHash($fkObject, $fields)][] = $fkObject; |
|
| 221 | 221 | $this->setResultCollection($results, $collections, $properties); |
| 222 | 222 | } |
| 223 | 223 | |
@@ -229,9 +229,9 @@ discard block |
||
| 229 | 229 | */ |
| 230 | 230 | protected function setResultCollection(&$results, &$collections, $properties) |
| 231 | 231 | { |
| 232 | - if(is_array($results) || $results instanceof ArrayAccess) |
|
| 232 | + if (is_array($results) || $results instanceof ArrayAccess) |
|
| 233 | 233 | { |
| 234 | - for($i=0,$k=count($results);$i<$k;$i++) |
|
| 234 | + for ($i = 0, $k = count($results); $i < $k; $i++) |
|
| 235 | 235 | $this->setObjectProperty($results[$i], $properties, $collections); |
| 236 | 236 | } |
| 237 | 237 | else |
@@ -248,7 +248,7 @@ discard block |
||
| 248 | 248 | { |
| 249 | 249 | $hash = $this->getObjectHash($source, $properties); |
| 250 | 250 | $prop = $this->getContext()->getProperty(); |
| 251 | - $source->$prop=isset($collections[$hash]) ? $collections[$hash] : array(); |
|
| 251 | + $source->$prop = isset($collections[$hash]) ? $collections[$hash] : array(); |
|
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
@@ -80,13 +80,16 @@ discard block |
||
| 80 | 80 | if($validArray || $results instanceof ArrayAccess || $results instanceof TActiveRecord) |
| 81 | 81 | { |
| 82 | 82 | $this->collectForeignObjects($results); |
| 83 | - while($obj = array_pop($stack)) |
|
| 84 | - $obj->collectForeignObjects($results); |
|
| 83 | + while($obj = array_pop($stack)) { |
|
| 84 | + $obj->collectForeignObjects($results); |
|
| 85 | + } |
|
| 86 | + } else if($results instanceof TActiveRecordRelation) { |
|
| 87 | + $stack[] = $this; |
|
| 88 | + } |
|
| 89 | + //call it later |
|
| 90 | + else if($results === null || !$validArray) { |
|
| 91 | + $stack = array(); |
|
| 85 | 92 | } |
| 86 | - else if($results instanceof TActiveRecordRelation) |
|
| 87 | - $stack[] = $this; //call it later |
|
| 88 | - else if($results === null || !$validArray) |
|
| 89 | - $stack = array(); |
|
| 90 | 93 | return $results; |
| 91 | 94 | } |
| 92 | 95 | |
@@ -115,8 +118,9 @@ discard block |
||
| 115 | 118 | $matchingTableName = strtolower($recordTableInfo->getTableName()); |
| 116 | 119 | $matchingFullTableName = strtolower($recordTableInfo->getTableFullName()); |
| 117 | 120 | $tableInfo=$from; |
| 118 | - if($from instanceof TActiveRecord) |
|
| 119 | - $tableInfo = $gateway->getRecordTableInfo($from); |
|
| 121 | + if($from instanceof TActiveRecord) { |
|
| 122 | + $tableInfo = $gateway->getRecordTableInfo($from); |
|
| 123 | + } |
|
| 120 | 124 | //find first non-empty FK |
| 121 | 125 | foreach($tableInfo->getForeignKeys() as $fkeys) |
| 122 | 126 | { |
@@ -125,8 +129,9 @@ discard block |
||
| 125 | 129 | { |
| 126 | 130 | $hasFkField = !$loose && $this->getContext()->hasFkField(); |
| 127 | 131 | $key = $hasFkField ? $this->getFkFields($fkeys['keys']) : $fkeys['keys']; |
| 128 | - if(!empty($key)) |
|
| 129 | - return $key; |
|
| 132 | + if(!empty($key)) { |
|
| 133 | + return $key; |
|
| 134 | + } |
|
| 130 | 135 | } |
| 131 | 136 | } |
| 132 | 137 | |
@@ -154,8 +159,9 @@ discard block |
||
| 154 | 159 | $fields = array(); |
| 155 | 160 | foreach($fkeys as $fkName => $field) |
| 156 | 161 | { |
| 157 | - if(in_array($fkName, $matching[2])) |
|
| 158 | - $fields[$fkName] = $field; |
|
| 162 | + if(in_array($fkName, $matching[2])) { |
|
| 163 | + $fields[$fkName] = $field; |
|
| 164 | + } |
|
| 159 | 165 | } |
| 160 | 166 | return $fields; |
| 161 | 167 | } |
@@ -168,8 +174,9 @@ discard block |
||
| 168 | 174 | protected function getObjectHash($obj, $properties) |
| 169 | 175 | { |
| 170 | 176 | $ids=array(); |
| 171 | - foreach($properties as $property) |
|
| 172 | - $ids[] = is_object($obj) ? (string)$obj->getColumnValue($property) : (string)$obj[$property]; |
|
| 177 | + foreach($properties as $property) { |
|
| 178 | + $ids[] = is_object($obj) ? (string)$obj->getColumnValue($property) : (string)$obj[$property]; |
|
| 179 | + } |
|
| 173 | 180 | return serialize($ids); |
| 174 | 181 | } |
| 175 | 182 | |
@@ -193,14 +200,16 @@ discard block |
||
| 193 | 200 | */ |
| 194 | 201 | protected function getIndexValues($keys, $results) |
| 195 | 202 | { |
| 196 | - if(!is_array($results) && !$results instanceof ArrayAccess) |
|
| 197 | - $results = array($results); |
|
| 203 | + if(!is_array($results) && !$results instanceof ArrayAccess) { |
|
| 204 | + $results = array($results); |
|
| 205 | + } |
|
| 198 | 206 | $values=array(); |
| 199 | 207 | foreach($results as $result) |
| 200 | 208 | { |
| 201 | 209 | $value = array(); |
| 202 | - foreach($keys as $name) |
|
| 203 | - $value[] = $result->getColumnValue($name); |
|
| 210 | + foreach($keys as $name) { |
|
| 211 | + $value[] = $result->getColumnValue($name); |
|
| 212 | + } |
|
| 204 | 213 | $values[] = $value; |
| 205 | 214 | } |
| 206 | 215 | return $values; |
@@ -216,8 +225,9 @@ discard block |
||
| 216 | 225 | protected function populateResult(&$results,$properties,&$fkObjects,$fields) |
| 217 | 226 | { |
| 218 | 227 | $collections=array(); |
| 219 | - foreach($fkObjects as $fkObject) |
|
| 220 | - $collections[$this->getObjectHash($fkObject, $fields)][]=$fkObject; |
|
| 228 | + foreach($fkObjects as $fkObject) { |
|
| 229 | + $collections[$this->getObjectHash($fkObject, $fields)][]=$fkObject; |
|
| 230 | + } |
|
| 221 | 231 | $this->setResultCollection($results, $collections, $properties); |
| 222 | 232 | } |
| 223 | 233 | |
@@ -231,11 +241,12 @@ discard block |
||
| 231 | 241 | { |
| 232 | 242 | if(is_array($results) || $results instanceof ArrayAccess) |
| 233 | 243 | { |
| 234 | - for($i=0,$k=count($results);$i<$k;$i++) |
|
| 235 | - $this->setObjectProperty($results[$i], $properties, $collections); |
|
| 244 | + for($i=0,$k=count($results);$i<$k;$i++) { |
|
| 245 | + $this->setObjectProperty($results[$i], $properties, $collections); |
|
| 246 | + } |
|
| 247 | + } else { |
|
| 248 | + $this->setObjectProperty($results, $properties, $collections); |
|
| 236 | 249 | } |
| 237 | - else |
|
| 238 | - $this->setObjectProperty($results, $properties, $collections); |
|
| 239 | 250 | } |
| 240 | 251 | |
| 241 | 252 | /** |
@@ -1,14 +1,14 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * TActiveRecordRelation class file. |
|
| 4 | - * |
|
| 5 | - * @author Wei Zhuo <weizhuo[at]gmail[dot]com> |
|
| 6 | - * @link https://github.com/pradosoft/prado |
|
| 7 | - * @copyright Copyright © 2005-2015 The PRADO Group |
|
| 8 | - * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | - * @version $Id$ |
|
| 10 | - * @package System.Data.ActiveRecord.Relations |
|
| 11 | - */ |
|
| 3 | + * TActiveRecordRelation class file. |
|
| 4 | + * |
|
| 5 | + * @author Wei Zhuo <weizhuo[at]gmail[dot]com> |
|
| 6 | + * @link https://github.com/pradosoft/prado |
|
| 7 | + * @copyright Copyright © 2005-2015 The PRADO Group |
|
| 8 | + * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT |
|
| 9 | + * @version $Id$ |
|
| 10 | + * @package System.Data.ActiveRecord.Relations |
|
| 11 | + */ |
|
| 12 | 12 | |
| 13 | 13 | /** |
| 14 | 14 | * Load active record relationship context. |
@@ -30,6 +30,9 @@ discard block |
||
| 30 | 30 | private $_relation; //data from an entry of TActiveRecord::$RELATION |
| 31 | 31 | private $_fkeys; |
| 32 | 32 | |
| 33 | + /** |
|
| 34 | + * @param TActiveRecord $record |
|
| 35 | + */ |
|
| 33 | 36 | public function __construct($record, $property=null, $relation=null) |
| 34 | 37 | { |
| 35 | 38 | $this->_record=$record; |
@@ -163,6 +166,7 @@ discard block |
||
| 163 | 166 | * An instance of TActiveRecordHasOne, TActiveRecordBelongsTo, TActiveRecordHasMany, |
| 164 | 167 | * or TActiveRecordHasManyAssocation will be returned. |
| 165 | 168 | * @param TActiveRecordCriteria search criteria |
| 169 | + * @param TSqlCriteria $criteria |
|
| 166 | 170 | * @return TActiveRecordRelation record relationship handler instnace. |
| 167 | 171 | * @throws TActiveRecordException if property is not defined or missing. |
| 168 | 172 | */ |
@@ -195,7 +199,7 @@ discard block |
||
| 195 | 199 | } |
| 196 | 200 | |
| 197 | 201 | /** |
| 198 | - * @return TActiveRecordRelationCommand |
|
| 202 | + * @return boolean |
|
| 199 | 203 | */ |
| 200 | 204 | public function updateAssociatedRecords($updateBelongsTo=false) |
| 201 | 205 | { |
@@ -30,11 +30,11 @@ discard block |
||
| 30 | 30 | private $_relation; //data from an entry of TActiveRecord::$RELATION |
| 31 | 31 | private $_fkeys; |
| 32 | 32 | |
| 33 | - public function __construct($record, $property=null, $relation=null) |
|
| 33 | + public function __construct($record, $property = null, $relation = null) |
|
| 34 | 34 | { |
| 35 | - $this->_record=$record; |
|
| 36 | - $this->_property=$property; |
|
| 37 | - $this->_relation=$relation; |
|
| 35 | + $this->_record = $record; |
|
| 36 | + $this->_property = $property; |
|
| 37 | + $this->_relation = $relation; |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
@@ -43,7 +43,7 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function hasRecordRelation() |
| 45 | 45 | { |
| 46 | - return $this->_relation!==null; |
|
| 46 | + return $this->_relation !== null; |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | public function getPropertyValue() |
@@ -75,8 +75,8 @@ discard block |
||
| 75 | 75 | */ |
| 76 | 76 | public function getRelationForeignKeys() |
| 77 | 77 | { |
| 78 | - if($this->_fkeys===null) |
|
| 79 | - $this->_fkeys=$this->getRelationHandler()->getRelationForeignKeys(); |
|
| 78 | + if ($this->_fkeys === null) |
|
| 79 | + $this->_fkeys = $this->getRelationHandler()->getRelationForeignKeys(); |
|
| 80 | 80 | return $this->_fkeys; |
| 81 | 81 | } |
| 82 | 82 | |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | */ |
| 112 | 112 | public function getCondition() |
| 113 | 113 | { |
| 114 | - return isset($this->_relation[3])?$this->_relation[3]:null; |
|
| 114 | + return isset($this->_relation[3]) ? $this->_relation[3] : null; |
|
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | /** |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | */ |
| 121 | 121 | public function getParameters() |
| 122 | 122 | { |
| 123 | - return isset($this->_relation[4])?$this->_relation[4]:array(); |
|
| 123 | + return isset($this->_relation[4]) ? $this->_relation[4] : array(); |
|
| 124 | 124 | } |
| 125 | 125 | |
| 126 | 126 | /** |
@@ -166,16 +166,16 @@ discard block |
||
| 166 | 166 | * @return TActiveRecordRelation record relationship handler instnace. |
| 167 | 167 | * @throws TActiveRecordException if property is not defined or missing. |
| 168 | 168 | */ |
| 169 | - public function getRelationHandler($criteria=null) |
|
| 169 | + public function getRelationHandler($criteria = null) |
|
| 170 | 170 | { |
| 171 | - if(!$this->hasRecordRelation()) |
|
| 171 | + if (!$this->hasRecordRelation()) |
|
| 172 | 172 | { |
| 173 | 173 | throw new TActiveRecordException('ar_undefined_relation_prop', |
| 174 | 174 | $this->_property, get_class($this->_record), 'RELATIONS'); |
| 175 | 175 | } |
| 176 | - if($criteria===null) |
|
| 176 | + if ($criteria === null) |
|
| 177 | 177 | $criteria = new TActiveRecordCriteria($this->getCondition(), $this->getParameters()); |
| 178 | - switch($this->getRelationType()) |
|
| 178 | + switch ($this->getRelationType()) |
|
| 179 | 179 | { |
| 180 | 180 | case TActiveRecord::HAS_MANY: |
| 181 | 181 | Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordHasMany'); |
@@ -197,19 +197,19 @@ discard block |
||
| 197 | 197 | /** |
| 198 | 198 | * @return TActiveRecordRelationCommand |
| 199 | 199 | */ |
| 200 | - public function updateAssociatedRecords($updateBelongsTo=false) |
|
| 200 | + public function updateAssociatedRecords($updateBelongsTo = false) |
|
| 201 | 201 | { |
| 202 | - $success=true; |
|
| 203 | - foreach($this->_record->getRecordRelations() as $data) |
|
| 202 | + $success = true; |
|
| 203 | + foreach ($this->_record->getRecordRelations() as $data) |
|
| 204 | 204 | { |
| 205 | 205 | list($property, $relation) = $data; |
| 206 | - $belongsTo = $relation[0]==TActiveRecord::BELONGS_TO; |
|
| 207 | - if(($updateBelongsTo && $belongsTo) || (!$updateBelongsTo && !$belongsTo)) |
|
| 206 | + $belongsTo = $relation[0] == TActiveRecord::BELONGS_TO; |
|
| 207 | + if (($updateBelongsTo && $belongsTo) || (!$updateBelongsTo && !$belongsTo)) |
|
| 208 | 208 | { |
| 209 | 209 | $obj = $this->getSourceRecord(); |
| 210 | - if(!$this->isEmptyFkObject($obj->getColumnValue($property))) |
|
| 210 | + if (!$this->isEmptyFkObject($obj->getColumnValue($property))) |
|
| 211 | 211 | { |
| 212 | - $context = new TActiveRecordRelationContext($this->getSourceRecord(),$property,$relation); |
|
| 212 | + $context = new TActiveRecordRelationContext($this->getSourceRecord(), $property, $relation); |
|
| 213 | 213 | $success = $context->getRelationHandler()->updateAssociatedRecords() && $success; |
| 214 | 214 | } |
| 215 | 215 | } |
@@ -219,10 +219,10 @@ discard block |
||
| 219 | 219 | |
| 220 | 220 | protected function isEmptyFkObject($obj) |
| 221 | 221 | { |
| 222 | - if(is_object($obj)) |
|
| 222 | + if (is_object($obj)) |
|
| 223 | 223 | return $obj instanceof TList ? $obj->count() === 0 : false; |
| 224 | - else if(is_array($obj)) |
|
| 225 | - return count($obj)===0; |
|
| 224 | + else if (is_array($obj)) |
|
| 225 | + return count($obj) === 0; |
|
| 226 | 226 | else |
| 227 | 227 | return empty($obj); |
| 228 | 228 | } |
@@ -75,8 +75,9 @@ discard block |
||
| 75 | 75 | */ |
| 76 | 76 | public function getRelationForeignKeys() |
| 77 | 77 | { |
| 78 | - if($this->_fkeys===null) |
|
| 79 | - $this->_fkeys=$this->getRelationHandler()->getRelationForeignKeys(); |
|
| 78 | + if($this->_fkeys===null) { |
|
| 79 | + $this->_fkeys=$this->getRelationHandler()->getRelationForeignKeys(); |
|
| 80 | + } |
|
| 80 | 81 | return $this->_fkeys; |
| 81 | 82 | } |
| 82 | 83 | |
@@ -173,8 +174,9 @@ discard block |
||
| 173 | 174 | throw new TActiveRecordException('ar_undefined_relation_prop', |
| 174 | 175 | $this->_property, get_class($this->_record), 'RELATIONS'); |
| 175 | 176 | } |
| 176 | - if($criteria===null) |
|
| 177 | - $criteria = new TActiveRecordCriteria($this->getCondition(), $this->getParameters()); |
|
| 177 | + if($criteria===null) { |
|
| 178 | + $criteria = new TActiveRecordCriteria($this->getCondition(), $this->getParameters()); |
|
| 179 | + } |
|
| 178 | 180 | switch($this->getRelationType()) |
| 179 | 181 | { |
| 180 | 182 | case TActiveRecord::HAS_MANY: |
@@ -219,12 +221,13 @@ discard block |
||
| 219 | 221 | |
| 220 | 222 | protected function isEmptyFkObject($obj) |
| 221 | 223 | { |
| 222 | - if(is_object($obj)) |
|
| 223 | - return $obj instanceof TList ? $obj->count() === 0 : false; |
|
| 224 | - else if(is_array($obj)) |
|
| 225 | - return count($obj)===0; |
|
| 226 | - else |
|
| 227 | - return empty($obj); |
|
| 224 | + if(is_object($obj)) { |
|
| 225 | + return $obj instanceof TList ? $obj->count() === 0 : false; |
|
| 226 | + } else if(is_array($obj)) { |
|
| 227 | + return count($obj)===0; |
|
| 228 | + } else { |
|
| 229 | + return empty($obj); |
|
| 230 | + } |
|
| 228 | 231 | } |
| 229 | 232 | } |
| 230 | 233 | |
@@ -46,6 +46,10 @@ discard block |
||
| 46 | 46 | } |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | + /** |
|
| 50 | + * @param TScaffoldEditView $parent |
|
| 51 | + * @param TActiveRecord $record |
|
| 52 | + */ |
|
| 49 | 53 | public function createScaffoldInput($parent, $item, $column, $record) |
| 50 | 54 | { |
| 51 | 55 | $this->_parent=$parent; |
@@ -62,6 +66,11 @@ discard block |
||
| 62 | 66 | $label->setForControl(self::DEFAULT_ID); |
| 63 | 67 | } |
| 64 | 68 | |
| 69 | + /** |
|
| 70 | + * @param TScaffoldEditView $parent |
|
| 71 | + * @param TDbTableColumn $column |
|
| 72 | + * @param TActiveRecord $record |
|
| 73 | + */ |
|
| 65 | 74 | public function loadScaffoldInput($parent, $item, $column, $record) |
| 66 | 75 | { |
| 67 | 76 | $this->_parent=$parent; |
@@ -21,51 +21,51 @@ discard block |
||
| 21 | 21 | { |
| 22 | 22 | $record->getDbConnection()->setActive(true); //must be connected before retrieving driver name! |
| 23 | 23 | $driver = $record->getDbConnection()->getDriverName(); |
| 24 | - switch(strtolower($driver)) |
|
| 24 | + switch (strtolower($driver)) |
|
| 25 | 25 | { |
| 26 | 26 | case 'sqlite': //sqlite 3 |
| 27 | 27 | case 'sqlite2': //sqlite 2 |
| 28 | - require_once(dirname(__FILE__).'/TSqliteScaffoldInput.php'); |
|
| 28 | + require_once(dirname(__FILE__) . '/TSqliteScaffoldInput.php'); |
|
| 29 | 29 | return new TSqliteScaffoldInput($conn); |
| 30 | 30 | case 'mysqli': |
| 31 | 31 | case 'mysql': |
| 32 | - require_once(dirname(__FILE__).'/TMysqlScaffoldInput.php'); |
|
| 32 | + require_once(dirname(__FILE__) . '/TMysqlScaffoldInput.php'); |
|
| 33 | 33 | return new TMysqlScaffoldInput($conn); |
| 34 | 34 | case 'pgsql': |
| 35 | - require_once(dirname(__FILE__).'/TPgsqlScaffoldInput.php'); |
|
| 35 | + require_once(dirname(__FILE__) . '/TPgsqlScaffoldInput.php'); |
|
| 36 | 36 | return new TPgsqlScaffoldInput($conn); |
| 37 | 37 | case 'mssql': |
| 38 | - require_once(dirname(__FILE__).'/TMssqlScaffoldInput.php'); |
|
| 38 | + require_once(dirname(__FILE__) . '/TMssqlScaffoldInput.php'); |
|
| 39 | 39 | return new TMssqlScaffoldInput($conn); |
| 40 | 40 | case 'ibm': |
| 41 | - require_once(dirname(__FILE__).'/TIbmScaffoldInput.php'); |
|
| 41 | + require_once(dirname(__FILE__) . '/TIbmScaffoldInput.php'); |
|
| 42 | 42 | return new TIbmScaffoldInput($conn); |
| 43 | 43 | default: |
| 44 | 44 | throw new TConfigurationException( |
| 45 | - 'scaffold_invalid_database_driver',$driver); |
|
| 45 | + 'scaffold_invalid_database_driver', $driver); |
|
| 46 | 46 | } |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | public function createScaffoldInput($parent, $item, $column, $record) |
| 50 | 50 | { |
| 51 | - $this->_parent=$parent; |
|
| 51 | + $this->_parent = $parent; |
|
| 52 | 52 | $item->setCustomData($column->getColumnId()); |
| 53 | 53 | $this->createControl($item->_input, $column, $record); |
| 54 | - if($item->_input->findControl(self::DEFAULT_ID)) |
|
| 54 | + if ($item->_input->findControl(self::DEFAULT_ID)) |
|
| 55 | 55 | $this->createControlLabel($item->_label, $column, $record); |
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | protected function createControlLabel($label, $column, $record) |
| 59 | 59 | { |
| 60 | - $fieldname = ucwords(str_replace('_', ' ', $column->getColumnId())).':'; |
|
| 60 | + $fieldname = ucwords(str_replace('_', ' ', $column->getColumnId())) . ':'; |
|
| 61 | 61 | $label->setText($fieldname); |
| 62 | 62 | $label->setForControl(self::DEFAULT_ID); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | 65 | public function loadScaffoldInput($parent, $item, $column, $record) |
| 66 | 66 | { |
| 67 | - $this->_parent=$parent; |
|
| 68 | - if($this->getIsEnabled($column, $record)) |
|
| 67 | + $this->_parent = $parent; |
|
| 68 | + if ($this->getIsEnabled($column, $record)) |
|
| 69 | 69 | { |
| 70 | 70 | $prop = $column->getColumnId(); |
| 71 | 71 | $record->setColumnValue($prop, $this->getControlValue($item->_input, $column, $record)); |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | protected function getRecordPropertyValue($column, $record) |
| 82 | 82 | { |
| 83 | 83 | $value = $record->getColumnValue($column->getColumnId()); |
| 84 | - if($column->getDefaultValue()!==TDbTableColumn::UNDEFINED_VALUE && $value===null) |
|
| 84 | + if ($column->getDefaultValue() !== TDbTableColumn::UNDEFINED_VALUE && $value === null) |
|
| 85 | 85 | return $column->getDefaultValue(); |
| 86 | 86 | else |
| 87 | 87 | return $value; |
@@ -51,8 +51,9 @@ discard block |
||
| 51 | 51 | $this->_parent=$parent; |
| 52 | 52 | $item->setCustomData($column->getColumnId()); |
| 53 | 53 | $this->createControl($item->_input, $column, $record); |
| 54 | - if($item->_input->findControl(self::DEFAULT_ID)) |
|
| 55 | - $this->createControlLabel($item->_label, $column, $record); |
|
| 54 | + if($item->_input->findControl(self::DEFAULT_ID)) { |
|
| 55 | + $this->createControlLabel($item->_label, $column, $record); |
|
| 56 | + } |
|
| 56 | 57 | } |
| 57 | 58 | |
| 58 | 59 | protected function createControlLabel($label, $column, $record) |
@@ -81,10 +82,11 @@ discard block |
||
| 81 | 82 | protected function getRecordPropertyValue($column, $record) |
| 82 | 83 | { |
| 83 | 84 | $value = $record->getColumnValue($column->getColumnId()); |
| 84 | - if($column->getDefaultValue()!==TDbTableColumn::UNDEFINED_VALUE && $value===null) |
|
| 85 | - return $column->getDefaultValue(); |
|
| 86 | - else |
|
| 87 | - return $value; |
|
| 85 | + if($column->getDefaultValue()!==TDbTableColumn::UNDEFINED_VALUE && $value===null) { |
|
| 86 | + return $column->getDefaultValue(); |
|
| 87 | + } else { |
|
| 88 | + return $value; |
|
| 89 | + } |
|
| 88 | 90 | } |
| 89 | 91 | |
| 90 | 92 | protected function setRecordPropertyValue($item, $record, $input) |