mirror of https://github.com/xfarrow/phpxpress.git
stability in Table
This commit is contained in:
parent
55ee0d1731
commit
287c6c4d25
|
@ -1,6 +1,5 @@
|
|||
<?php
|
||||
include "include.php";
|
||||
|
||||
class Table{
|
||||
|
||||
// Structure
|
||||
|
@ -17,6 +16,9 @@
|
|||
private $hoverAnimation = false;
|
||||
private $small = false;
|
||||
|
||||
// other
|
||||
private $pedantic_type_check = false;
|
||||
|
||||
function draw(){
|
||||
|
||||
if(!isset($this->dataSource)){
|
||||
|
@ -66,33 +68,72 @@
|
|||
if(empty($dataSource))
|
||||
throw new InvalidArgumentException('Parameter cannot be empty.');
|
||||
|
||||
if(isset($this-> dataSource))
|
||||
throw new BadFunctionCallException("Cannot add datasource to a Table already having a datasource");
|
||||
|
||||
|
||||
$is_array_of_arrays; // if false, the datasource is an array of object(s)
|
||||
if(is_object($dataSource[0])){
|
||||
$is_array_of_arrays = false;
|
||||
}
|
||||
else if(is_array($dataSource[0])){
|
||||
$is_array_of_arrays = true;
|
||||
}
|
||||
else{
|
||||
throw new InvalidArgumentException('Parameter "datasource" must be an' .
|
||||
'array of array(s) or array of object(s)');
|
||||
}
|
||||
|
||||
if($this -> pedantic_type_check){
|
||||
if(count(array_filter($dataSource, function($entry) use($is_array_of_arrays){
|
||||
if(is_array($entry)){
|
||||
return !$is_array_of_arrays;
|
||||
}
|
||||
else if(is_object($entry)){
|
||||
return $is_array_of_arrays;
|
||||
}
|
||||
else
|
||||
throw new InvalidArgumentException('Parameter "datasource" must be an' .
|
||||
'array of array(s) or array of object(s)');
|
||||
})) > 0){
|
||||
throw new InvalidArgumentException('Parameter "datasource" must be an' .
|
||||
'array of array(s) or array of object(s)');
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** If one or more addColumn() were called before setting the datasource,
|
||||
** we should add those columns in the source.
|
||||
*/
|
||||
if(isset($this -> columnCaptions)){
|
||||
foreach($dataSource as &$element){
|
||||
foreach($dataSource as &$row){
|
||||
foreach($this -> columnCaptions as $captionName){
|
||||
$element = (object)(array($captionName => null) + (array)$element); // append the already inserted captions at the beginning
|
||||
if($is_array_of_arrays){
|
||||
$row = array($captionName => null) + (array)$row; // append the already inserted captions at the beginning
|
||||
//$element->{$captionName} = null; // append the already inserted captions at the end
|
||||
}
|
||||
else{
|
||||
$row = (object)(array($captionName => null) + (array)$row); // append the already inserted captions at the beginning
|
||||
//$element->{$captionName} = null; // append the already inserted captions at the end
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this -> dataSource = $dataSource;
|
||||
|
||||
// Set captions when array of objects provided
|
||||
if(is_object($dataSource[0])){
|
||||
if(!$is_array_of_arrays){
|
||||
$this->columnCaptions = array_keys(get_object_vars($this -> dataSource[0]));
|
||||
}
|
||||
|
||||
// Set caption when array of arrays provided
|
||||
else if(is_array($dataSource[0])){
|
||||
else{
|
||||
$this->columnCaptions = array_keys($this -> dataSource[0]);
|
||||
}
|
||||
}
|
||||
|
||||
function setCustomCaptions(Array $captions){
|
||||
|
||||
if(empty($this->dataSource))
|
||||
throw new BadFunctionCallException('Before setting Custom captions, a datasource must be provided first.');
|
||||
|
||||
|
@ -121,7 +162,6 @@
|
|||
$obj->{$captionName} = null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -178,5 +218,17 @@
|
|||
}
|
||||
$this->small = $bool;
|
||||
}
|
||||
|
||||
// to be used before setting the datasource
|
||||
function setPedanticTypeCheck($bool){
|
||||
if(!is_bool($bool)){
|
||||
throw new InvalidArgumentException('Parameter must be a boolean.');
|
||||
}
|
||||
if(isset($this->datasource)){
|
||||
echo "<b>Warning</b>Use of pedantic type check after datasource is set. Instruction ignored.";
|
||||
return;
|
||||
}
|
||||
$this->pedantic_type_check = $bool;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
Loading…
Reference in New Issue