Ferreteria/v0.6/clade/Sys/Data/Things/Array/ifaces/Keyed

From Woozle Writes Code
< Ferreteria‎ | v0.6‎ | clade‎ | Sys‎ | Data‎ | Things‎ | Array
Jump to navigation Jump to search
clade: Sys\Data\Things\Array\ifaces\Keyed
Clade Family
(root) Keyed (i,t)

D/Keyed (i,t)
Q/Keyed (i,t)

Code

as of 2025-10-03

interface iKeyed extends ArrayAccess {
    // MAIN
    function QryIt(int|string $key) : QVarIface;
    function GetIt(int|string $key) : mixed;
    function GetItNz(int|string $key,mixed $vDefault) : mixed;
    function HasIt(int|string $key) : bool;
    function LetIt(int|string $key,mixed $val) : mixed;
    function SetIt(int|string $key,mixed $val) : void;
    function ZapIt(int|string $key) : void;
    // FIGURING
    function SortByKey() : void;
}
trait tKeyed {  // IMPLEMENTS iKeyed

    // ++ CONFIG ++ //

    protected function QVarClass(int|string $snKey) : string { return QVarClass::class; }

    // -- CONFIG -- //
    // ++ ACCESS ++ //

    // Non-writing version of GetItQ() (changing the Q object does not affect the source array)
    // 2025-09-30 Maybe this should be renamed to ReadItQ(), for clarity?
    public function QryIt(int|string $snKey) : QVarIface {
        $sc = $this->QVarClass($snKey);
        $scThis = get_called_class();
        #echo "QVAR CLASS for [$snKey]: [$sc] (in $scThis)".CRLF;
        $os = ($sc)::AsNew();
        if (array_key_exists($snKey,$this->ar)) {
            #echo "QArr class: ".get_called_class().CRLF;
            #echo "key=[$snKey] QVAR class: [$sc]".CRLF;
            #echo $this->ReflectThis()->Report();
            #echo $os->ReflectThis()->Report();
            #$this->AmHere("Retrieving: [$snKey] in ".get_called_class()." and returning in ".get_class($os));
            $os->SetIt($this->ar[$snKey]);
        }
        return $os;
    }

    public function HasIt(int|string $key) : bool { return array_key_exists($key,$this->ar); }
    public function GetIt(int|string $key) : mixed { return $this->ar[$key]; }

    public function GetItNz(int|string $key, mixed $vDefault) : mixed { return $this->HasIt($key) ? $this->GetIt($key) : $vDefault; }
    public function LetIt(int|string $key,mixed $vUpdate) : mixed { // updates value if not NULL; always returns latest value
        if (!is_null($vUpdate)) {
            $this->SetIt($key,$vUpdate);
        }
        return $this->GetIt($key);
    }
    public function ZapIt(int|string $key) : void { unset($this->ar[$key]); }

    // ++ ACCESS: sequence ++ //

    public function SortByKey() : void { ksort($this->ar); }

    // -- ACCESS -- //
    // ++ IFACE: ArrayAccess ++ //

    public function offsetExists(mixed $offset) : bool { return $this->HasIt($offset); }
    public function offsetGet(mixed $offset) : mixed { return $this->GetIt($offset); }
    public function offsetSet(mixed $offset, mixed $value) : void { $this->SetIt($offset,$value); }
    public function offsetUnset(mixed $offset) : void { $this->ZapIt($offset); }

    // -- IFACE -- //
}