Ferreteria/v0.6/clade/Sys/Data/aux/MetaPair

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | Sys‎ | Data‎ | aux
Jump to navigation Jump to search
clade: Sys\Data\aux\MetaPair
Clade Family
StandardBase MetaPair (none)
Clade Aliases
Alias Clade
ActionIface Sys\Events\ItWent
Base* [c,i] aux\StandardBase
DataFmtIface Sys\Data\aux\metaPair\info\Array
DataNode* [c,i] Sys\Data\aux\metaPair\Data
DbConner* [i,t] Sys\Data\Engine\need\UseEngine
FIdentIface Sys\FileSys\Aspect\Ident
InfoNode* [c,i] Sys\Data\aux\metaPair\Info
QObjIface Data\Mem\QVar\Obj
QStrIface Data\Mem\QVar\Str
SchemaIface Sys\Data\Engine\aux\Schema
SelfIface Sys\Data\aux\MetaPair
Subpages

About

  • Purpose: holder for a data-file and its corresponding metadata file
    • Can create both from the name of either one (though the meta's data must already contain the filename).

Auxiliary clades are in metaPair.

History

  • 2025-11-02 cobbled together from bits of Node.php and maybe Data.php

Code

Code

as of 2026-03-11:

interface iMetaPair extends BaseIface, DbConnerIface {
    // CONFIG
    const sDateFmt = 'Y.m.d H:i:s';   // "/" chars get escaped in JSON, so "." makes the text a little more readable
    // SETUP
    static function FromDataSpec(string $fs) : SelfIface;
    static function FromInfoSpec(string $fs) : SelfIface;
    static function FromDataSpec_wSchema(SchemaIface $o,string $fs) : self;
    // OBJECTS
    function DataFile() : DataNodeIface;
    function InfoFile() : InfoNodeIface;
}
class cMetaPair extends BaseClass implements SelfIface {
    use DbConnerTrait;

    // ++ CONFIG ++ //

    protected function DataNodeClass()  : string { return DataNodeClass::class; }
    protected function InfoNodeClass()  : string { return InfoNodeClass::class; }

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

    public static function FromDataIdent(FIdentIface $ofData) : SelfIface {
        $oThis = new static;
        $oThis->oFiData = $ofData->Node();
        $oThis->WithDataIdent();
        return $oThis;
    }
    public static function FromDataSpec(string $fs) : SelfIface {
        $oThis = new static;
        $oThis->WithDataSpec($fs);
        return $oThis;
    }
    public static function FromInfoIdent(FIdentIface $ofMeta) : SelfIface {
        $oThis = new static;
        $oThis->oFiMeta = $ofMeta->Node();
        $oThis->WithInfoIdent();
        return $oThis;
    }
    public static function FromInfoSpec(string $fs) : SelfIface {
        $oThis = new static;
        $oThis->WithInfoSpec($fs);
        return $oThis;
    }
    public static function FromDataSpec_wSchema(SchemaIface $o,string $fsData) : SelfIface {
        $oThis = static::FromDataSpec($fsData);
        #echo $oThis->ReflectThis()->Report();
        #echo $oThis->InfoFile()->InOut()->OAData()->ReflectThis()->Report();
        #$oThis->InfoFile()->InOut()->OAData()->QOSchema()->SetIt($o);
        $oThis->QOSchema()->SetIt($o);
        return $oThis;
    }

    // ++ SETUP: dynamic ++ //

    protected function WithDataSpec(string $fsData) : void {
        $ofData = $this->DataNodeClass()::FromSpec($fsData);
        $this->oFiData = $ofData;
        $this->WithDataIdent();
    }
    protected function WithDataIdent() : void {
        $oDataIdent = $this->DataFile()->Ident();
        $fsData = $oDataIdent->SpecFull();

        // $fsData is the data file we're documenting -- add an extension for the metadata
        $fsMeta = $fsData.'.json';

        $ofMeta = $this->InfoNodeClass()::FromSpec($fsMeta);
        $this->oFiMeta = $ofMeta;


        // remember/store just the filename -- the meta-file should be portable; this is not a log file
        $fnData = $oDataIdent->SpecNameExt();
        // in the metadata, save the name of the datafile:
        $ofMeta->InOut()->OAData()->QSFileName()->SetIt($fnData);
    }
    protected function WithInfoSpec(string $fsMeta) : void {
        $ofMeta = $this->InfoNodeClass()::FromSpec($fsMeta);
        $this->oFiMeta = $ofMeta;
        $this->WithInfoIdent();
    }
    protected function WithInfoIdent() : void {
        $ofInfo = $this->InfoFile();
        $fsInfo = $ofInfo->Ident()->SpecFull();
        $oaInfo = $ofInfo->InOut()->OAData();
        $oaInfo->Fetch(); // load the data from the file
        $fnData = $oaInfo->QSFileName()->GetIt();
        #$sSchema = $oaInfo->QSSchema()->GetIt();
        #$this->QSSchema()->SetIt($sSchema);

        // Create the DataFile object so we can use its filespec stuff to change its .ext (there should be a better way...):
        $ofData = $this->DataNodeClass()::FromSpec($fsInfo);
        $ofData->Ident()->OSpec()->QOLeaf()->GetIt()->SetIt($fnData);
        $this->oFiMeta = $ofInfo;
        $this->oFiData = $ofData;
        #$this->oSchema = $this->OSchema();
    }

    // -- SETUP -- //
    // ++ OBJECTS ++ //

    private $oFiData;
    public function DataFile() : DataNodeIface { return $this->oFiData; }

    // TODO 2025-11-14: Rename to InfoFile()
    private $oFiMeta;
    public function InfoFile() : InfoNodeIface { return $this->oFiMeta; }

    // SHORTCUT
    protected function QOSchema() : QObjIface { return $this->InfoFile()->InOut()->OAData()->QOSchema(); }

    // -- OBJECTS -- //
    // ++ VALUES ++ //

    // SHORTCUT
    protected function QSDbase() : QStrIface  { return $this->InfoFile()->InOut()->OAData()->QSDbase(); }

    // -- VALUES -- //
    // ++ FIGURING ++ //

    protected function TimeString() : string { return date(self::sDateFmt); }

    // -- FIGURING -- //
    // ++ I/O: disk ++ //

    public function SaveStart() {
        // TODO: include program name/version
        $oSch = $this->QOSchema()->GetIt();
        #$oDInOut = $this->DataFile()->InOut();
        $oMInOut = $this->InfoFile()->InOut();
        $oaMeta = $oMInOut->OAData();
        #echo $oSch->ReflectThis()->Report();  // how to get Client object
        $arMetaNew = [
          DataFmtIface::sfDbaseSlug => $oSch->OEngine()->QSName()->GetIt(),
          DataFmtIface::sfDbaseSchema => $oSch->SchemaName(),
          #DataFmtIface::sfFileName => $oaMeta->QSFileName()->GetIt(),
          #DataFmtIface::sfDataType => $oaMeta->DataFormat(),
          DataFmtIface::sfTimeStInt => hrtime(),
          DataFmtIface::sfTimeStStr => $this->TimeString(),
          ];
        #echo $this->ReflectThis()->Report();
        $oaMeta->SetVals($arMetaNew);
        $oaMeta->Store();
    }
    public function SaveFinish(ActionIface $oAct) {
        $arMetaNew = [
          DataFmtIface::sfTimeFiInt => hrtime(),
          DataFmtIface::sfTimeFiStr => $this->TimeString(),
          DataFmtIface::sfCmdStatus => $oAct->RenderStatusInline(),
          ];
        $oMInOut = $this->InfoFile()->InOut();
        $oaMeta = $oMInOut->OAData();
        $oaMeta->SetVals($arMetaNew);
        $oaMeta->Store();
    }

    // ++ I/O: UI ++ //

    public function HeadersArray() : array {
        return ['file','fmt','dbase','schema','started','finished','status'];  // 2025-11-11 can't remember...
    }
    // CEMENT VRowIface
    public function ValuesArray() : array {  // what to display for this row, as array of strings
        return $this->InfoFile()->InOut()->OAData()->GetPairs([
          "file.name",
          "data.format",
          "db.slug",
          "db.schema",
          "time.start.str",
          "time.finish.str",
          "status.cmd"
        ]);
    }
    public function RenderInline() : string { return self::PromptForMethod(); }
    public function RenderBlock() : string { return self::PromptForMethod(); }

    // -- I/O -- //
    // ++ DIAGS ++ //

    public function VIEW_Inline() : string {
        $arVals = $this->ValuesArray();
        #$fn = $arVals['file.name'];
        #$sConn = $arVals['db.slug'];
        #$sSchm = $arVals['db.schema'];
        #return "$fn: $sConn: $sSchm";
        $nSize = $this->DataFile()->Ident()->NBytes();
        $sSize = number_format($nSize);
        $sWhen = $arVals['time.finish.str'];
        return "$sWhen: $sSize bytes";
    }

    // -- DIAGS -- //
}