Ferreteria/v0.6/clade/Sys/Data/Things/Array/View/List
Jump to navigation
Jump to search
| ||||||||||||||||||||
About
- Purpose: generic Viewer for a list of
name:valuepairs
History
- 2025-02-21 started, if only to make it easier to figure out what functionality is needed for more complex data
Code
interface iList extends BaseIface {
static function FromOArray(ArrayIface $oa) : self;
}
class cList extends BaseClass implements iList {
// ++ SETUP ++ //
public static function FromOArray(ArrayIface $oa) : iList {
$oThis = new static($oa);
#$oThis->WithOArray($oa);
return $oThis;
}
// ++ SETUP: dynamic ++ //
// TYPE-SPECIFIC ALIASES
protected function WithOArray(ArrayIface $oa) : void { $this->WithSubject($oa); }
protected function OArray() : ArrayIface { return $this->Subject(); }
// -- SETUP -- //
// ++ OUTPUT ++ //
// CEMENT
public function Render() : string {
$oa = $this->OArray();
$n = $oa->Count();
#echo "LIST COUNT=[$n]".CRLF;
if ($oa->Count() === 0) {
$sOut = '{empty array}'.CRLF;
} else {
$sOut = $this->RenderPopulated();
}
#echo $oa->ReflectThis()->Report(); die();
return $sOut;
}
// ++ OUTPUT: internal ++ //
protected function RenderPopulated() : string {
$oTable = static::Screen()->NewTable();
$this->AddTableHeader($oTable);
$ar = $this->OArray()->GetVals();
foreach ($ar as $sKey => $vVal) {
$this->TableAnElement($oTable,$sKey,$vVal);
}
return $oTable->Render();
}
// DEFAULT
protected function AddTableHeader(TableIface $oTbl) : void {
$oRow = $oTbl->NewRow();
$oRow->IsHeader(TRUE);
$oRow->WithData(['name','value']); // header
}
// DEFAULT/FALLBACK
protected function TableAnElement(TableIface $oTbl, string $sKey, mixed $v) : void {
$oRow = $oTbl->NewRow();
if (is_object($v)) {
$sVal = $this->RenderObject($v);
} else {
$sVal = static::DiagnoseValue($v);
}
$oRow->WithData([$sKey,$sVal]);
}
// DEFAULT/FALLBACK
private $didWarn = FALSE;
protected function RenderObject(object $oSubj) : string {
if (!$this->didWarn) {
$scView = get_called_class();
$scSubj = get_class($oSubj);
$sDet = CRLF.' - Viewer class: '.$scView.CRLF.' - Subject class: '.$scSubj.CRLF;
echo self::CodingPrompt('A more-specific Viewer would be better.'.$sDet,1); // 1 = go back extra step in stack
#debug_print_backtrace(); // 2025-05-18 maybe this isn't needed now
$this->didWarn = TRUE;
}
return $oSubj->ObjectInfo();
}
// -- OUTPUT -- //
}