Ferreteria/v0.6/clade/Sys/FileSys/Mode/@removed

From WoozleCodes
Jump to navigation Jump to search
Removed Code

More decluttering from stuff removed in late December.

  • 2025-12-23 Actually, let's use From*() pseudoconstructors.
#
     /**
     * INPUT:
     *  if string: enum flags defined above
     *  if array: array of enums defined above
     */
    /* 
    public function __construct(string|array $vRequest) {
        if (is_string($vRequest)) {
            $this->ParseReqString($vRequest);
        } else {
            $this->HandleReqArray($vRequest);
        }
    }
#
    public function __construct(string|array $vRequest) {
        if (is_array($vRequest)) {
            $sRequest = '';
            foreach ($vRequest as $eReq) {
                $sRequest .= $eReq->value;
            }
        } else {
            $sRequest = $vRequest;
        }
        $this->Request($sRequest);
    }
  • 2025-12-22 These were PUBLIC "in case we need to do other things (e.g. create parent-folder) depending on mode", but I don't even know what I meant by that now.
#
    protected function IsReader(eTriState $e=NULL)      : eTriState { return $this->Modes(eFileMode::READER,$e); }
    protected function IsWriter(eTriState $e=NULL)      : eTriState { return $this->Modes(eFileMode::WRITER,$e); }
    public    function CanCreate(eTriState $e=NULL)     : eTriState { return $this->Modes(eFileMode::CREATE,$e); }
    protected function ExistsAction(eIfFound $do=NULL)  : eIfFound  { return $this->Modes(eFileMode::FOUND,$do); }
  • 2025-12-22 I can't see any need to preserve the original request.
    • It turns out (now) that I need it for error-reporting -- but I'm going to implement it less complicatedly.
#
    private $sReq;
    protected function Request(string $sReq=NULL) : string {
        if (is_string($sReq)) {
            $this->sReq = $sReq;
            $this->ParseRequest();
        } else {
            $sReq = $this->sReq;
        }
        return $sReq;
    }
    // TODO: cache NativeString(), maybe?
    public function IsValid() : bool { return $this->Request() != '' && is_string($this->NativeString()); }
  • 2025-12-22 previous code, not really working:
#
    // ++ VALUES ++ //

    public function NativeString(string $s=NULL) : ?string {

      // ORIGINAL CODE

        $sMode = NULL;

        $ecRd = $this->IsReader();
        $ecWr = $this->IsWriter();
        $ecCr = $this->CanCreate();
        $ecEx = $this->ExistsAction();

        switch ($ecRd) {
          case eTriState::SHRUG:  // read OPTIONAL
            $sMode = $this->NativeString_forReadShrug();
            $this->AmHereShort("SHRUG=[$sMode]");
            break;
          case eTriState::OFF:  // read OFF
            $sMode = $this->NativeString_forReadForbid();
            $this->AmHereShort("FORBID=[$sMode]");
            break;
          case eTriState::ON:   // read ON
            $sMode = $this->NativeString_forReadDemand();
            $this->AmHereShort("DEMAND=[$sMode]");
            break;
        }

        return $sMode;
    }
    // CONDITION: readability optional/ok
    protected function NativeString_forReadShrug() : string {
        $ecWr = $this->IsWriter();
        #self::GotToHere('READ: optional');
        // OPTIONAL readability
        switch ($ecWr) {
          case eTriState::OFF:  // read OPTIONAL, write OFF
            echo $this->CodingPrompt("Must request R and/or W mode.");
            break;
          case eTriState::ON: // read OPTIONAL, write ON
            #self::GotToHere('WRITE: on');
            $ecCr = $this->CanCreate();
            switch ($ecCr) {
              case eTriState::OFF:    // read OPTIONAL, write ON, NO CREATE
              case eTriState::SHRUG:  // we'll let SHRUG default to NO CREATE
                $sMode = 'r+';  // read-write, BOF
                break;
              case eTriState::ON:  // read OPTIONAL, write ON, CREATE if n/f
                // We're writing, creating new file, zeroing existing file -- can assume reading not needed
                $sMode = 'w+';  // (if we want to assume reading, use r+
                break;
              default:
                self::GotToHere('CREATE switch is broken, somehow.');
            }
            break;
              case eTriState::SHRUG: // read OPTIONAL, write OPTIONAL
                $this->RequestPrompt("Both read and write are optional - what are we actually trying to do?");
                break;
              default:
                self::GotToHere('WRITE switch is broken, somehow.');
        }
        return $sMode;
    }
    // CONDITION: must NOT be readable
    protected function NativeString_forReadForbid() : string {
        $ecWr = $this->IsWriter();
        #self::GotToHere('READ: no');
        // NOT readable
        switch ($ecWr) {
          case eTriState::OFF:  // read OFF, write OFF
            echo $this->CodingPrompt("Must request R and/or W mode.");
            break;
          case eTriState::ON: // read OFF, write ON
            // WRITE-ONLY
            self::HardAssert($ecCr->value->IsOptional(),"Can't force do-not-create on a write-only file.");
            switch($ecEx) {
              case eIfFound::SHRUG: $sMode = 'c'; break;
              case eIfFound::EOF:   $sMode = 'a'; break;
              case eIfFound::FAIL:  $sMode = 'x'; break;
              case eIfFound::ZERO:  $sMode = 'w'; break;
              default: self::GotToHere("IfFound switch [$ecEx] is broken somehow");
            }
            break;
        }
    }
    // CONDITION: must be readable
    protected function NativeString_forReadDemand() : string {
        $ecWr = $this->IsWriter();
        #self::GotToHere('READ: yes');
        switch ($ecWr) {
          case eTriState::OFF:  // read ON, write OFF
            // READ-ONLY
            // There's only one read-only mode:
            $sMode = 'r';
            self::SoftAssert($ecCr->value != eTriState::ON,"Can't force CREATE on a read-only file.");
            break;

          case eTriState::ON:
            // READ+WRITE
            $ecEx = $this->ExistsAction();
            switch($ecEx) {
              case eIfFound::SHRUG: $sMode = 'c+'; break;
              case eIfFound::EOF:   $sMode = 'a+'; break;
              case eIfFound::FAIL:  $sMode = 'x+'; break;
              case eIfFound::ZERO:  $sMode = 'w+'; break;
              default: self::GotToHere("IfFound switch [$ecEx] is broken somehow");
            }
            break;

          case eTriState::SHRUG:  $sMode = 'r'; break;  // grant as little permission as needed
        }
        return $sMode;
    }

    protected function RequestPrompt(string $sDetails) {
        $sReq = $this->Request();
        echo $this->CodingPrompt("'$sReq' is a rather ambiguous request. $sDetails");
    }

    // ACTION: for the given $eMode, gets or sets its value in the $arData array
    private $arData = [];
    protected function Modes(eFileMode $eMode, $eVal=NULL) {
        $sv = $eMode->value;  // index for array lookup
        $this->AmHereShort("sv=[$sv] eVal=".(is_null($eVal) ? '(NULL)' : $eVal->value));
        if (is_null($eVal)) {
            $ar = $this->arData;
            if (array_key_exists($sv,$ar)) {
                $eVal = $ar[$sv];
                echo "sv=[$sv] FOUND eVal=".(is_null($eVal) ? '(NULL)' : $eVal->value).CRLF;
            } else {
                $eVal = $eMode->Default();
                echo "sv=[$sv] DEFAULT eVal=".(is_null($eVal) ? '(NULL)' : ($eVal->value.' ('.get_class($eVal).')').CRLF;
            }
        } else {
            $this->arData[$sv] = $eVal;
            echo "sv=[$sv] SET eVal=".(is_null($eVal) ? '(NULL)' : $eVal->value).CRLF;
        }
        return $eVal;
    }

    // -- VALUES -- //

Removed from ParseRequest() on 2025-12-14:

#
        //* 2025-12-14 let's try a different approach
        $nIdx = 0;
        $nLen = strlen($sReq);
        $this->AmHere("REQUEST LENGTH: [$nLen]");
        do {
            $chReq = $sReq[$nIdx++];
            $chVal = $sReq[$nIdx++];
            $eMode = eFileMode::tryFrom($chReq);
            $eVal = $eMode->EnumForValue($chVal);
            if (is_null($eVal)) {
                $sReq = $this->Request();
                echo $this->CodingPrompt("The value '$chVal' (for '$chReq' in [$sReq]) returned NULL.");
            } else {
                $arData[$chReq] = $eVal;
            }

        } while ($nIdx < $nLen);