-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli-math-ml-table-cell.class.php
762 lines (639 loc) · 18 KB
/
cli-math-ml-table-cell.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
<?php
/** cli-math-ml-table-cell.class.php
*
* Class of individual cell
*
* @author Rafael Martin Soto
* @author {@link https://www.inatica.com/ Inatica}
* @blog {@link https://rafamartin10.blogspot.com/ Blog Rafael Martin Soto}
* @since September 2021
* @version 1.0.2
* @license GNU General Public License v3.0
*/
class cli_math_ml_table_cell {
/**
* arr_alignments
*
* @var array
* @access private
*
**/
private $arr_alignments = [ 'left'=>STR_PAD_RIGHT, 'right'=>STR_PAD_LEFT, 'center'=>STR_PAD_BOTH ];
/**
* Cell Value
*
* @var object
* @access private
*
**/
private $value = null;
/**
* Cell Raw Value
*
* @var object
* @access private
*
**/
private $raw_value = null;
/**
* Cell Formatted Value
*
* @var string
* @access private
*
**/
private $formatted_value = '';
/**
* Cell Width
*
* @var int
* @access private
*
**/
private $width = null;
/**
* Text Color
*
* @var string
* @access private
*
**/
private $text_color = null;
/**
* Background Color
*
* @var string
* @access private
*
**/
private $background_color = null;
/**
* TextDecoration
*
* @var string
* @access private
*
**/
private $text_decoration = null;
/**
* Cell Padding Left
*
* @var int
* @access private
*
**/
private $padding_left = 1;
/**
* Cell Padding Right
*
* @var int
* @access private
*
**/
private $padding_right = 1;
/**
* Alignment of cell
*
* @var string
* @access private
*
**/
private $align = 'left'; // ['left'|'right'|'center']
/**
* Value is OK_KO
*
* @var object
* @access private
*
**/
private $is_ok_ko = null;
/**
* Value is BLANK ('')
*
* @var object
* @access private
*
**/
private $is_blank = null;
/**
* Value is DASH ('-')
*
* @var object
* @access private
*
**/
private $is_dash = null;
/**
* Value is YES_NO
*
* @var object
* @access private
*
**/
private $is_yes_no = null;
/**
* Value is TRUE_FALSE
*
* @var object
* @access private
*
**/
private $is_true_false = null;
/**
* Format type for TRUE_FALSE
*
* Possible values: ['boolean'|'short'|'long'|'raw'|null]
* - 'boolean': 1|0
* - 'short': 't'|'f'
* - 'long': 'true'|'false'
* - 'raw'|null: original value
*
* @var object
* @access private
*
**/
private $format_true_false = null;
/**
* Format type for YES_NO
*
* Possible values: ['boolean'|'short'|'long'|'raw'|null]
* - 'boolean': 1|0
* - 'short': 'y'|'n'
* - 'long': 'yes'|'no'
* - 'raw'|null: original value
*
* @var object
* @access private
*
**/
private $format_yes_no = null;
/**
* Value is NUMBER
*
* @var object
* @access private
*
**/
private $is_numeric = null;
/**
* Format Color
*
* @var string
* @access private
*
**/
private $color = null;
/**
* Constructor
*
* @access public
* @param object $value
*/
public function __construct( $value ) {
$this->set_value($value);
} // /__construct()
/**
* Get Cell width
*
* @access public
* @return int $width_plus_padding
*/
public function width( ) {
return $this->width + $this->padding_left + $this->padding_right;
} // /width()
/**
* Set padding_left
*
* @access public
* @param int $padding_left
*/
public function set_padding_left( $padding_left ) {
$this->padding_left = $padding_left;
} // /set_padding_left()
/**
* Set padding_right
*
* @access public
* @paramn int $padding_right
*/
public function set_padding_right( $padding_right ) {
$this->padding_right = $padding_right;
} // /set_padding_right()
/**
* Set TYPE of Value
*
* @access public
*/
public function set_type( ) {
$this->is_blank( true );
$this->is_dash( true );
$this->is_yes_no( true );
$this->is_true_false( true );
$this->is_ok_ko( true );
$this->is_numeric( true );
} // /set_type()
/**
* Format raw value to desire format
* yes_no:
* - raw: original value
* - boolean: ['1'|'0']
* - short: ['y'|'n']
* - long: ['yes'|'no']
* true_false:
* - raw: original value
* - boolean: ['1'|'0']
* - short: ['t'|'f']
* - long: ['true'|'false']
*
* @access public
* @param object $value
* @return string $transform_value
*/
public function formatted_raw_value_output( ) {
$transform_value = $this->raw_value;
if( $this->is_true_false() && !is_null($this->format_true_false) && $this->format_true_false != 'raw' ||
$this->is_yes_no() && !is_null($this->format_yes_no) && $this->format_yes_no != 'raw' ){
$type = ((!is_null($this->format_true_false))?$this->format_true_false:$this->format_yes_no);
switch( $type ){
case 'boolean': if( $this->is_yes() || $this->is_true() ){
$transform_value = '1';
} else if( $this->is_no() || $this->is_false() ){
$transform_value = '0';
}
break;
case 'short': if( $this->is_yes() ){
$transform_value = 'y';
} else if( $this->is_no() ){
$transform_value = 'n';
}if( $this->is_true() ){
$transform_value = 't';
} else if( $this->is_false() ){
$transform_value = 'f';
}
break;
case 'long': if( $this->is_yes() ){
$transform_value = 'yes';
} else if( $this->is_no() ){
$transform_value = 'no';
}if( $this->is_true() ){
$transform_value = 'true';
} else if( $this->is_false() ){
$transform_value = 'false';
}
break;
} // /switch $type
} // if need to transform
// Freemem()
unset( $type );
return $transform_value;
} // formatted_raw_value_output()
/**
* Set Value of Cell
*
* @access public
* @param object $value
*/
public function set_value( $value = null ) {
if( !is_null($value) ){
$this->raw_value = $value;
}
// Transform
$this->value = $this->formatted_raw_value_output( );
if( !is_null($value) ){
$this->set_type( );
if( $this->is_ok_ko_blank_dash() || $this->is_yes_no() || $this->is_true_false() ){
$this->align = 'center'; // Default alignment for ok_ko, yes_no, true_false
} else{
$cleaned_value = $this->cleaned_value( );
if( is_numeric( $cleaned_value ) || in_array($cleaned_value, array('na', 'nan')) ){
$this->align = 'right'; // Default alignment for numbers
}
}
}
if( is_null($value) ){
$this->width = strlen($this->cleaned_value());
}
$this->set_formatted_value( );
// Freemem()
unset( $cleaned_value );
} // /set_value()
/**
* Set Format text color
*
* @access public
* @param string $text_color
*/
public function set_color( $color = null ) {
$this->color = $color;
} // /set_color()
/**
* Set Format format yes no
*
* @access public
* @param string $format_yes_no
*/
public function set_format_yes_no( $format_yes_no = null ) {
$this->format_yes_no = $format_yes_no;
$this->set_value( );
} // /set_format_yes_no()
/**
* Set Format format true false
*
* @access public
* @param string $format_true_false
*/
public function set_format_true_false( $format_true_false = null ) {
$this->format_true_false = $format_true_false;
$this->set_value( );
} // /set_format_true_false()
/**
* Set Format background color
*
* @access public
* @param string $background_color
*/
public function set_background_color( $background_color = null ) {
$this->background_color = $background_color;
} // /set_background_color(9)
/**
* Set Format text decoration
*
* @access public
* @param string $text_decoration
*/
public function set_text_decoration( $text_decoration = null ) {
$this->text_decoration = $text_decoration;
} // /set_text_decoration()
/**
* Set Formatted Value of Cell
*
* @access private
*/
private function set_formatted_value( ) {
$this->formatted_value = $this->formatted_value( );
} // /set_formatted_value()
/**
* Set alignment of Cell
*
* @access public
* @param string $align
*/
public function align( $align='left' ) {
$this->align = $align;
$this->formatted_value = $this->set_formatted_value( );
} // /align()
/**
* Format Color Value of Cell
*
* @access public
* @return string $formatted_value
*/
public function formatted_color_value( ) {
$str_value = $this->formatted_value( );
$StrColor = null;
if( !is_null($this->color) || !is_null($this->text_decoration) || !is_null($this->background_color) ){
//$StrColor = chr(27);
}
if( !is_null($this->color) ){
$StrColor .= chr(27).$this->color;
}
if( !is_null($this->text_decoration) ){
$StrColor .= chr(27).$this->text_decoration;
}
if( !is_null($this->background_color) ){
$StrColor .= chr(27).$this->background_color;
}
if( !is_null( $StrColor) ){
$str_value = $StrColor.$str_value.chr(27).'[0m';
}
// Freemem()
unset( $StrColor );
return $str_value;
} // /formatted_color_value()
/**
* Format Value of Cell
*
* @access public
* @return string $formatted_value
*/
public function formatted_value( ) {
$str_value = $this->cleaned_value( );
if( is_null($this->width) ){
$this->set_width( strlen( $str_value )+$this->padding_left+$this->padding_right );
}
$str_value = str_pad($str_value, $this->width, ' ', $this->arr_alignments[$this->align]);
$str_pad_left = str_pad('', $this->padding_left);
$str_pad_right = str_pad('', $this->padding_right);
$str_value = $str_pad_left.$str_value.$str_pad_right;
// Freemem()
unset( $str_pad_left );
unset( $str_pad_right );
return $str_value;
} // /formatted_value
/**
* Set Width of Cell
*
* @access public
* @param int $width
*/
public function set_width( $width=null ) {
if( is_null($this->width) && is_null($width) ){
$this->set_width( strlen( $this->cleaned_value( ) )+$this->padding_left+$this->padding_right );
}
$this->width = $width-$this->padding_left-$this->padding_right;
$this->formatted_value = $this->formatted_value($this->value);
} // /set_width()
/**
* Get if Value is Number
*
* @access public
* @param boolean $set_variable
* @return boolean $is_numeric
*/
public function is_numeric( $set_variable=false ) {
if( $set_variable ){
$cleaned_value = $this->cleaned_value( );
$this->is_numeric = ( is_numeric( $cleaned_value ) || $cleaned_value == 'na' || $cleaned_value == 'nan' ) ||
( isset($cleaned_value[0]) && ($cleaned_value[0] == '<' || $cleaned_value[0] == '>') && is_numeric( substr($cleaned_value, 1 )) );
// Freemem()
unset( $cleaned_value );
}
return $this->is_numeric;
} // /is_numeric()
/**
* Get if Value is blank
*
* @access public
* @param boolean $set_variable
* @return boolean $is_blank
*/
public function is_blank( $set_variable=false ) {
if( $set_variable ){
$cleaned_value = $this->cleaned_value( );
$this->is_blank = ( $cleaned_value == '' || $cleaned_value == 'null' );
// Freemem()
unset( $cleaned_value );
}
return $this->is_blank;
} // /is_blank()
/**
* Get if Value is dash
*
* @access public
* @param boolean $set_variable
* @return boolean $is_dash
*/
public function is_dash( $set_variable=false ) {
if( $set_variable ){
$this->is_dash = ( $this->cleaned_value( ) == '-' );
}
return $this->is_dash;
} // /is_dash()
/**
* Get if Value is is_numeric()|''
*
* @access public
* @return boolean $is_numeric_blank
*/
public function is_numeric_blank_dash( ) {
return ( $this->is_numeric() || $this->is_blank() || $this->is_dash() );
} // /is_numeric_blank_dash()
/**
* Get if Value is OK|KO|''
*
* @access public
* @param boolean $set_variable
* @return boolean $is_ok_ko
*/
public function is_ok_ko_blank_dash( $set_variable=false ) {
return ( $this->is_ok_ko() || $this->is_blank() || $this->is_dash() );
} // /is_ok_ko_blank_dash()
/**
* Get if Value is OK_KO
*
* @access public
* @param boolean $set_variable
* @return boolean $is_ok_ko
*/
public function is_ok_ko( $set_variable=false ) {
if( $set_variable ){
$this->is_ok_ko = ( $this->is_ok( ) || $this->is_ko( ) );
}
return $this->is_ok_ko;
} // /is_ok_ko()
/**
* Get if Value is OK
*
* @access public
* @return boolean $is_ok
*/
public function is_ok( ) {
return ( $this->cleaned_value( ) == 'ok' );
} // /is_ok()
/**
* Get if Value is KO
*
* @access public
* @return boolean $is_ko
*/
public function is_ko( ) {
return ( $this->cleaned_value( ) == 'ko' );
} // /is_ko()
/**
* Get if Value is YES|NO|''
*
* @access public
* @return boolean $is_yes_no
*/
public function is_yes_no_blank_dash( ) {
return ( $this->is_yes_no() || $this->is_blank() || $this->is_dash() );
} // /is_yes_no_blank_dash()
/**
* Get if Value is YES_NO
*
* @access public
* @param boolean $set_variable
* @return boolean $is_yes_no
*/
public function is_yes_no( $set_variable=false ) {
if( $set_variable ){
$this->is_yes_no = ( $this->is_yes( ) || $this->is_no( ) );
}
return $this->is_yes_no;
} // /is_yes_no()
/**
* Get if Value is YES
*
* @access public
* @return boolean $is_yes
*/
public function is_yes( ) {
$cleaned_value =$this->cleaned_value( );
return ( $cleaned_value == 'yes' || $cleaned_value == 'y' );
} // /is_yes()
/**
* Get if Value is NO
*
* @access public
* @return boolean $is_no
*/
public function is_no( ) {
$cleaned_value =$this->cleaned_value( );
return ( $cleaned_value == 'no' || $cleaned_value == 'n' );
} // /is_no()
/**
* Get if Value is TRUE|FALSE|''
*
* @access public
* @return boolean $is_true_false
*/
public function is_true_false_blank_dash( ) {
return ( $this->is_true_false() || $this->is_blank() || $this->is_blank() );
} // /is_true_false_blank_dash()
/**
* Get if Value is TRUE_FALSE
*
* @access public
* @param boolean $set_variable
* @return boolean $is_true_false
*/
public function is_true_false( $set_variable=false ) {
if( $set_variable ){
$this->is_true_false = ( $this->is_true( ) || $this->is_false( ) );
}
return $this->is_true_false;
} // /is_true_false()
/**
* Get if Value is TRUE
*
* @access public
* @return boolean $is_true
*/
public function is_true( ) {
$tr_true_false =$this->cleaned_value( );
return ( $tr_true_false == 'true' || $tr_true_false == 't' || $tr_true_false == '1' );
} // /is_true()
/**
* Get if Value is FALSE
*
* @access public
* @param boolean $set_variable
* @return boolean $is_false
*/
public function is_false( ) {
$tr_true_false =$this->cleaned_value( );
return ( $tr_true_false == 'false' || $tr_true_false == 'f' || $tr_true_false == '0' );
} // /is_false()
/**
* Clean Value
*
* @access public
* @param mixed $value
* @return boolean $cleaned_value
*/
public function cleaned_value( $value = null ) {
$ValueToClean = ( is_null($value)?$this->value:$value );
return trim( strtolower( (string)$ValueToClean ) );
} // /cleaned_value()
} // /cli_math_ml_table_cell class
?>