Ferreteria/v0.6/clade/Data/Mem/QVar

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | Data‎ | Mem
Revision as of 13:54, 26 November 2025 by Woozle (talk | contribs) (Created page with "{{page/clade/v2 |fam= {{!}} align=right {{!}} <code>{{l/ver/clade|Aux|StandardBase}}</code> {{!}} align=center {{!}} ⇒ <code>{{l/ver/clade|Data\Mem|QVar}}</code> ⇒ {{!}} align=left {{!}} <poem> <code>/Arr/</code> <code>/Int/</code> <code>/Mix/</code> <code>/Obj/</code> <code>/Res/</code> <code>/Str/</code> <code>/Wild/</code> </poem> |alia= {{!-!}} '''Base'''* [c,i] {{!!}} <code>{{l/ver/clade/full|Aux|StandardBase}}</code> {{!-...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
clade: Data\Mem\QVar
Clade Family
StandardBase QVar
Clade Aliases
Alias Clade
Base* [c,i] Aux\StandardBase
StaticTypes Data\Mem\Val\Types
ArrayOptsEnum Data\Mem\QVar\opts\Array
ArrayIface Sys\Data\Things\Array
Subpages

About

  • Purpose: queryable var-wrapper objects

History

  • {{fmt/date|2024-07-18** starting this from scratch to replace the awkward double-wrapper Wrap and Core class-family.
  • {{fmt/date|2025-01-14** Decided `SetIt_from[O]Array()` should specifically *not* save a reference, because that can cause problems if the value is set elsewhere, or if we're trying to let go of the array to save memory.
  • {{fmt/date|2025-04-20** Renamed `RefFromOArrElem()` -> `WithOAElement()`, for consistency with `FromOAElement()`
  • {{fmt/date|2025-04-24** Restored `FromValue()` from code removed on 04/14.
  • {{fmt/date|2025-05-11** Changed some terminology:
    • [From/With]AElement() -> [From/With]AValue()
    • [From/With]OAElement() -> [From/With]OAValue()
    ...and added [From/With]AIndex()

Code

interface iQVar extends BaseIface {

    // SETUP
    static function AsNew() : self;
    static function FromVarRef(&$v) : self;
    static function FromAValue(array $ar, int|string $vKey, ArrayOptsEnum $eOpt=ArrayOptsEnum::Nothing) : self;
    static function FromOAValue(ArrayIface $oa, int|string $vKey, ArrayOptsEnum $eOpt=ArrayOptsEnum::Nothing) : self;

    // ACCESS: typeless
    function HasIt() : bool;
    function ZapIt();
    function TypeOk(mixed $v) : bool;

    function WithOAValue(ArrayIface $oa, int|string $key, ArrayOptsEnum $eOpt=ArrayOptsEnum::Nothing) : void;

    // ++ ACCESS: basic types ++ //

    //// - int

    function HasInt() : bool;
    function SetInt(int $n);
    function SetIntNz(?int $n);
    function &GetInt() : int;
    function GetIntNz(int $nDefault=0) : int;
    function RefInt(int &$n) : void;

    //// - string

    function HasStr() : bool;
    function SetStr(string $s);
    function SetStrNz(?string $s);
    function &GetStr() : string;
    function GetStrNz(string $sDefault='', string $sPfx='', string $sSfx='') : string;
    function RefStr(string &$s) : void;

    //// - object

    function HasObj() : bool;
    function SetObj(object $o);
    function SetObjNz(?object $o);
    function GetObj() : object;
    function GetObjNz(object $oDefault=NULL) : ?object;

    /*
    // ACCESS: undeclarable
    // These can't be declared because their types change in each podling. "mixed" is replaced by the corresponding type.

    function SetIt(mixed $n);
    function SetItNz(?mixed $v);
    function &GetIt() : mixed;
    function GetItNz(mixed $vDefault=0) : mixed;
    function RefIt(mixed &$v) : void;

    function SetDefault(mixed $n);
    */
}

/**
 * NOTE:
 *  2024-07-19 There is an implied SetIt(variant) method, but we can't declare it here
 *    because that would prevent $v from having a required type.
 */
trait tQVar {

    // ++ SETUP ++ //

    public static function AsNew() : iQVar { return new static; }
    public static function FromVarRef(&$v) : iQVar {
        $oThis = new static;
        $oThis->RefIt($v);
        return $oThis;
    }
    public static function FromValue(mixed $v) : iQVar {
        $oThis = new static;
        $oThis->SetIt($v);
        return $oThis;
    }
    public static function FromAIndex(array $ar, int|string $vKey, ArrayOptsEnum $eOpt=ArrayOptsEnum::Nothing) : iQVar {
        $oThis = new static;
        $oThis->WithAIndex($ar,$key,$eOpt);
        return $oThis;
    }
    public static function FromAValue(array $ar, int|string $vKey, ArrayOptsEnum $eOpt=ArrayOptsEnum::Nothing) : iQVar {
        $oThis = new static;
        $oThis->WithAValue($ar,$key,$eOpt);
        return $oThis;
    }
    public static function FromOAValue(ArrayIface $oa, int|string $key, ArrayOptsEnum $eOpt=ArrayOptsEnum::Nothing) : iQVar {
        $oThis = new static;
        $oThis->WithOAValue($oa,$key,$eOpt);
        return $oThis;
    }

    // -- SETUP -- //
    // ++ ACCESS: typeless ++ //

    protected $v = NULL;
    public function ZapIt()          { $this->v = NULL; }

    protected function WithAIndex(array $ar, int|string $snKey, ArrayOptsEnum $eOpt=ArrayOptsEnum::Nothing) : void {
        $isFnd = FALSE;
        if (array_key_exists($snKey,$ar)) {
            $this->SetIt($snKey);
        }
    }

    public function WithAValue(array $ar, int|string $snKey, ArrayOptsEnum $eOpt=ArrayOptsEnum::Nothing) : void {
        $isFnd = FALSE;
        if (array_key_exists($snKey,$ar)) {
            $vDup =& $ar[$snKey];
            $isFnd = $this->TypeOk($vDup);
        }
        if (!$isFnd) {
            $isFnd = $this->HandleAbsent($snKey,$eOpt);
        }
        if ($isFnd) {
            $this->RefIt($vDup);
        }
    }
    public function WithOAValue(ArrayIface $oa, int