Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ClassicArray.php
Go to the documentation of this file.
1<?php
2
4
5use Ubiquity\utils\base\UArray;
7
8class ClassicArray extends BaseArray{
9 private $fields;
10 private $iFields;
11 private $datas;
12
13 public function __construct($datas,$part=null){
14 if(!is_array($datas)){
15 $datas=[$part=>$datas];
16 }
17 if(sizeof($datas)==1 && is_array(current($datas))){
18 $datas=current($datas);
19 }
20 $this->datas=$datas;
21 }
22
23 public function parse($reverse=true){
24 if($reverse){
25 return $this->parseReverse_();
26 }
27 return $this->parse_();
28 }
29
30 private function getFields(){
31 if(is_array($this->fields)){
33 }else{
34 $fields=array_keys($this->datas);
35 }
36 return $fields;
37 }
38
39 private function parseReverse_(){
40 if(sizeof($this->datas)==0){
41 return [['Nothing to display']];
42 }
43 if(UArray::isAssociative($this->datas)){
44 $array=[['field','value']];
45 $fields=$this->getFields();
46 foreach ($fields as $field){
47 if(isset($this->datas[$field]))
48 $array[]=[$field,$this->parseValue($this->datas[$field])];
49 }
50 }else{
51 $array=[];
52
53 foreach ($this->datas as $data){
54 $array[]=[$this->parseValue($data)];
55 }
56
57 }
58 return $array;
59 }
60
61 private function parse_(){
62 $fields=$this->getFields();
63 $array=[$fields];
64 $result=array_intersect_key($this->datas, array_flip($fields));
65 array_walk($result, function(&$item){$item=$this->parseValue($item);});
66 $array[]=$result;
67 return $array;
68 }
69
70 protected function parseValue($value) {
71 if(is_array($value)){
72 return $this->parseArray($value,$this->iFields);
73 }elseif(UString::isValid($value)){
74 return var_export($value,true);
75 }
76 return '{.}';
77 }
78
82 public function getDatas() {
83 return $this->datas;
84 }
85
89 public function setFields($fields) {
90 $this->fields = $fields;
91 }
92
96 public function setDatas($datas) {
97 $this->datas = $datas;
98 }
102 public function setIFields($iFields) {
103 $this->iFields = $iFields;
104 }
105
106
107}
108
String utilities.
Definition UString.php:15