HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/ste.komma.pro/app/Session/HasSessionTrait.php
<?php declare(strict_types=1);


namespace App\Session;

trait HasSessionTrait
{
    /**
     * Update the session variable with data from other variables
     */
    public function saveSession()
    {
        $this->validateSetup();
        $dataToSave = [];
        foreach ($this->variablesForSession as $variableForSession) {
            $dataToSave[$variableForSession] = $this->{$variableForSession};
        }

        session()->put(static::class, $dataToSave);
    }

    /**
     * Restores the data using the session.
     *
     * Warning! This will not work when this function is called via __constructors in
     * laravel controllers. Since the session is not initialized yet.
     *
     * @return void
     */
    private function restoreFromSession(): void
    {
        $this->validateSetup();
        if(session()->has(static::class)) {
            $dataToLoad = session()->get(static::class);
            foreach($this->variablesForSession as $variableForSession)
                if(isset($dataToLoad[$variableForSession]))  $this->{$variableForSession} = $dataToLoad[$variableForSession];
        }
    }

    /**
     * Clears session data for this class.
     *
     * Warning! This will not work when this function is called via __constructors in
     * laravel controllers. Since the session is not initialized yet.
     *
     * @return void
     */
    private function clearSession(): void
    {
        $this->validateSetup();
        session()->remove(static::class);
    }

    /**
     * Validates that the session variable is set.
     */
    private function validateSetup() {
        if(!isset($this->variablesForSession)) throw new \RuntimeException('HasSessionTrait: Make sure that a variablesForSession variable is set to an array of strings in "'.static::class.'". These string must refer to properties in "'.static::class.'".');
        foreach($this->variablesForSession as $item)
        {
            if(!property_exists($this, $item))
                throw new \RuntimeException('HasSessionTrait: The class "'.static::class.'" must, but did not have a property called "'.$item.'". Because it was listed in the $variablesForSession variable on "'.static::class.'".');
        }
    }
}