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,8 +1,7 @@
<?php
include "include.php";
class Table{
// Structure
private $dataSource;
private $columnCaptions;
@ -16,9 +15,12 @@
private $bordered = false;
private $hoverAnimation = false;
private $small = false;
// other
private $pedantic_type_check = false;
function draw(){
if(!isset($this->dataSource)){
throw new BadFunctionCallException('Error: dataSource not set.');
}
@ -32,7 +34,7 @@
if($this->small) $tableClass.= " table-sm";
echo "<table class = '$tableClass'>";
// Print head
echo '<thead><tr>';
foreach($this->columnCaptions as $caption){
@ -65,16 +67,55 @@
function setDataSource(Array $dataSource){
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
//$element->{$captionName} = null; // append the already inserted captions at the end
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
}
}
}
}
@ -82,27 +123,27 @@
$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.');
$provided = count($captions);
$expected = count($this->columnCaptions);
if($provided == $expected)
$this -> columnCaptions = $captions;
else
throw new LengthException('Number of provided captions not matching the datasource ones.
throw new LengthException('Number of provided captions not matching the datasource ones.
Provided: ' . $provided . "; Expected: " . $expected);
}
@ -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;
}
}
?>