From 287c6c4d25f8deb05b562063d3df47e36fd62245 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <> Date: Mon, 18 Jul 2022 09:50:54 +0200 Subject: [PATCH] stability in Table --- phpxpress/phpxpress/table.php | 82 ++++++++++++++++++++++++++++------- 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/phpxpress/phpxpress/table.php b/phpxpress/phpxpress/table.php index fb9a3da..e297ea3 100644 --- a/phpxpress/phpxpress/table.php +++ b/phpxpress/phpxpress/table.php @@ -1,8 +1,7 @@ dataSource)){ throw new BadFunctionCallException('Error: dataSource not set.'); } @@ -32,7 +34,7 @@ if($this->small) $tableClass.= " table-sm"; echo ""; - + // Print head echo ''; 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 "WarningUse of pedantic type check after datasource is set. Instruction ignored."; + return; + } + $this->pedantic_type_check = $bool; + } } ?>