Slim-Flash icon indicating copy to clipboard operation
Slim-Flash copied to clipboard

Problems with ArrayAccess storage

Open sverlan opened this issue 7 years ago • 1 comments

When using an ArrayAccess storage (I'm using adbario/slim-secure-session-middleware) the addMessage fails to write the message to the storage. The problem is that with ArrayAccess double indexing cannot be used:

$this->storage[$this->storageKey][$key] = array();

has no effect. Instead the following should be used:

$temp = $this->storage[$this->storageKey];
$temp[$key] = array();
$this->storage[$this->storageKey] =$temp;

I made a small modification to addMessage which seems to work:

public function addMessage($key, $message)
    {
       // Workaround for ArrayAccess storage
        $temp = $this->storage[$this->storageKey];
        // Create Array for this key
        if (!isset($temp[$key])) {
            $temp[$key] = [];
        }
        // Push onto the array
        $temp[$key][] = $message;
        $this->storage[$this->storageKey] = $temp;
    }

sverlan avatar Mar 10 '18 15:03 sverlan

If you have control of the ArrayAccess implementation, you can add a & to the offsetGet implementation to solve this.

public function &offsetGet($offset)
{

ntzm avatar Jun 19 '20 09:06 ntzm