stability in Table

This commit is contained in:
Alessandro Ferro 2022-07-18 09:50:54 +02:00
parent 55ee0d1731
commit 287c6c4d25
1 changed files with 67 additions and 15 deletions

View File

@ -1,6 +1,5 @@
<?php <?php
include "include.php"; include "include.php";
class Table{ class Table{
// Structure // Structure
@ -17,6 +16,9 @@
private $hoverAnimation = false; private $hoverAnimation = false;
private $small = false; private $small = false;
// other
private $pedantic_type_check = false;
function draw(){ function draw(){
if(!isset($this->dataSource)){ if(!isset($this->dataSource)){
@ -66,15 +68,54 @@
if(empty($dataSource)) if(empty($dataSource))
throw new InvalidArgumentException('Parameter cannot be empty.'); 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, ** If one or more addColumn() were called before setting the datasource,
** we should add those columns in the source. ** we should add those columns in the source.
*/ */
if(isset($this -> columnCaptions)){ if(isset($this -> columnCaptions)){
foreach($dataSource as &$element){ foreach($dataSource as &$row){
foreach($this -> columnCaptions as $captionName){ 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){
//$element->{$captionName} = null; // append the already inserted captions at the end $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
}
} }
} }
} }
@ -82,17 +123,17 @@
$this -> dataSource = $dataSource; $this -> dataSource = $dataSource;
// Set captions when array of objects provided // 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])); $this->columnCaptions = array_keys(get_object_vars($this -> dataSource[0]));
} }
// Set caption when array of arrays provided // Set caption when array of arrays provided
else if(is_array($dataSource[0])){ else{
$this->columnCaptions = array_keys($this -> dataSource[0]); $this->columnCaptions = array_keys($this -> dataSource[0]);
} }
} }
function setCustomCaptions(Array $captions){ function setCustomCaptions(Array $captions){
if(empty($this->dataSource)) if(empty($this->dataSource))
throw new BadFunctionCallException('Before setting Custom captions, a datasource must be provided first.'); throw new BadFunctionCallException('Before setting Custom captions, a datasource must be provided first.');
@ -121,7 +162,6 @@
$obj->{$captionName} = null; $obj->{$captionName} = null;
} }
} }
} }
/* /*
@ -178,5 +218,17 @@
} }
$this->small = $bool; $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;
}
} }
?> ?>