Ferreteria/v0.6/clade/IO/O/Data/Element/Branch/grid/Row/@code

From WoozleCodes
Jump to navigation Jump to search
Code Snapshots & Removals

Just before removing the Table-dependency:

interface iRow extends BaseIface, StyledIface {
    // SETUP
    static function FromCells(TableIface $oTbl, array $ar) : self;
    static function FromData(TableIface $oTbl, array $ar) : self;
    static function FromKeys(TableIface $oTbl, array $ar) : self;
    // SETUP: dynamic
    function WithCells(array $ar);
    function WithData(array $ar);
    function WithKeys(array $ar);
    // SETTINGS
    function IsHeader(bool $b=NULL) : bool;
    // ACCESS
    function NewCell_frVal(string $sValue) : CellIface;
    #function NewCell_wAttr(string $sValue, array $arAttr=[]) : CellIface;
    function NewNamedCell(string $sName,string $sValue) : CellIface;
    function GetCells() : array;
    // OBJECTS
    function OStyle() : StyleIface;
}

class cRow extends BaseClass implements RowIface {
    #use AttrsTrait;
    use StyledTrait;

    // ++ CONFIG ++ //

    protected function CellClass() : string { return CellClass::class; }

    // -- CONFIG -- //
    // ++ SETUP ++ //

    static public function FromCells(TableIface $oTbl, array $ar) : iRow { ($oThis = new static($oTbl))->WithCells($ar); return $oThis; }
    static public function FromData(TableIface $oTbl, array $ar) : iRow { ($oThis = new static($oTbl))->WithData($ar); return $oThis; }
    static public function FromKeys(TableIface $oTbl, array $ar) : iRow { ($oThis = new static($oTbl))->WithKeys($ar); return $oThis; }

    // ++ SETUP: dynamic ++ //

    /**
     * INPUT: $ar = list of Cell objects
     */
    public function WithCells(array $ar) {
        foreach ($ar as $nKey => $oCell) {
            $this->AddCell($oCell);
        }
    }
    /**
     * INPUT: $ar = list of [<column name> => <cell value>]
     *  The column names are ignored here.
     */
    public function WithData(array $ar) {
        foreach ($ar as $sKey => $sVal) {
            if (is_array($sVal)) {
                $this->AmHere("KEY=[$sKey] COUNT=".count($sVal));
            }
            $this->NewNamedCell($sKey,$sVal);
        }
    }
    // Cards and Decks use this, but I don't remember/understand how.
    public function WithKeys(array $ar) {
        foreach ($ar as $sKey => $sVal) {
            $this->NewCell_frVal($sKey);
        }
    }

    // -- SETUP -- //
    // ++ SETTINGS ++ //

    private bool $isHdr = FALSE;
    public function IsHeader(bool $b=NULL) : bool { return is_null($b) ? $this->isHdr : ($this->isHdr = $b); }

    // TEMPLATE
    private $oStyle = NULL;
    public function OStyle() : StyleIface {
        $o = $this->oStyle;
        if (is_null($o)) {
            $o = $this->oStyle = new (static::StyleClass());
        }
        $o->OData = $this;
        return $o;
    }

    // -- SETTINGS -- //
    // ++ ACCESS ++ //

    private array $arCells = [];
    protected function AddCell(CellIface $o) { $this->arCells[] = $o; }
    public function NewCell_frVal(string $sValue) : CellIface {
        $oCell = ($this->CellClass())::FromRowVal($this,$sValue);
        $this->AddCell($oCell);
        return  $oCell;
    }
    /*
    public function NewCell_wAttr(string $sValue) : CellIface {
        $oCell = ($this->CellClass())::FromRowVal($this,$sValue);
        $this->AddCell($oCell);
        return  $oCell;
    }*/
    function NewNamedCell(string $sName,string $sValue) : CellIface {
        $oCell = ($this->CellClass())::FromRowVal($this,$sValue);
        $oCell->QName()->SetIt($sName);
        $this->AddCell($oCell);
        return  $oCell;
    }
    public function GetCells() : array { return $this->arCells; }


    // -- ACCESS -- //
    // ++ OUTPUT ++ //

    public function Render() : string {
        $oStyle = $this->OStyle();
        return $oStyle->Render();
    }

    // -- OUTPUT -- //

}