/** * @return Forms */ public function forms() : Forms { if (!isset($this->forms)) { $this->forms = new Forms($this->sessionBag->getBag("Forms")); } return $this->forms; }
/** * @param \string[] ...$keys * @return Obfuscator */ public function setFields(string ...$keys) : self { $keysCount = count($keys); $bitsCount = $keysCount * (self::OBFUSCATE_KEY_SIZE * 4); // Get cryptographically secure random bytes $bytes = Security::randomKey($bitsCount); $bytes = str_split($bytes, self::OBFUSCATE_KEY_SIZE); // Make sure there are no duplicates some how if (count($bytes) !== count(array_unique($bytes))) { // Repeating key detected, retry return call_user_func_array([$this, "setFields"], $keys); } // Iterate through keys $count = 0; foreach ($keys as $key) { $this->obfuscated[$key] = $bytes[$count]; $count++; } // Save hash $this->hash = hash("sha1", implode(":", array_keys($this->obfuscated))); // Save to session? if (isset($this->sessionBag)) { $this->sessionBag->getBag($this->name)->set("hash", $this->hash)->set("fields", $this->obfuscated); } // Chain return $this; }
/** * @param string $name * @return Retriever */ public function retrieve(string $name) : Retriever { $obfuscated = []; // Check if form was saved in session if ($this->sessionBag->hasBag($name)) { // Retrieve from session $obfuscated = $this->sessionBag->getBag($name)->get("fields"); // Make sure $obfuscated is an Array if (!is_array($obfuscated)) { $obfuscated = []; $this->sessionBag->removeBag($name); } } $retriever = new Retriever($name, $obfuscated); return $retriever; }