ArrayAccess in extension

Author Message

A Costello

Sunday 06 July 2008 3:14:20 pm

I have an extension that makes use of the ArrayAccess interface. The template system does not correctly handle the array access. It always returns null. Is there anyway to work around this?

André R.

Monday 07 July 2008 2:19:57 am

ArrayAccess??

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

A Costello

Monday 07 July 2008 9:25:28 pm

ArrayAccess is an interface for php classes. It is quite handy. I am sending you a demo.

class Demo implements ArrayAccess, Iterator 
{
	 private $demo = 'default';
	 private $test = 'default';
	 private $_offsets = array('Demo', 'Test');       
	 
	 public function setDemo($value)
	 {
	 	 $this->demo = $value;	 	 
	 }
	 
	 public function getDemo()
	 {
	 	 return $this->demo;
	 }
	 
	 public function setTest($value)
	 {
	 	 $this->test = $value;
	 }
	 
	 public function getTest()
	 {
	 	 return $this->test;
	 }
	 
	//Foreach Access
    public function rewind()
    {
        $this->cur = 0;
    }
    
    public function key()
    {
        return $this->_offsets[$this->cur];
    }
    
    public function current()
    {   
        $function = 'get' . $this->_offsets[$this->cur];
        return $this->$function();   
    }
    
    public function next()
    {
        ++$this->cur;
    }
    
    public function valid()
    {
        return $this->cur < count($this->_offsets);
    }
    
    //Array Accessor
    function offsetExists($name) 
    {
        return (in_array($name, $this->_offsets));
    }
    
    function offsetGet($name) 
    {
        if(in_array($name, $this->_offsets))
        {
            $function = 'get' . $name;  
            return $this->$function();
        }
        return null;                         
    }
    
    function offsetSet($name, $id) 
    {
        if(in_array($name, $this->_offsets))
        {
            $function = 'set' . $name;
            return $this->$function($id);
        }                                     
    }
    
    function offsetUnset($name) 
    {
        if(in_array($name, $this->_offsets))
        {
            $function = 'set' . $name;
            return $this->$function(null);
        }
    }
}     

$example = new Demo();
echo '<h4>Before Set</h4>';
echo 'demo = ' . $example['Demo'] . '<br>';
echo 'test = ' . $example['Test'] . '<br>';   


$example['Demo'] = 'Demo';
$example['Test'] = 'Test';
echo '<h4>After Set</h4>';
echo 'demo = ' . $example['Demo'] . '<br>';
echo 'test = ' . $example['Test'] . '<br>';   

echo '<h4>Foreach</h4>';
foreach($example as $key => $value)
{
	echo "$key = $value <br>";
}

echo '<h4>Work Around</h4>';
foreach($example as $key => $value)
{
	$$key = $value;
	//Set to template variable
}
echo  'demo = ' . $Demo . '<br>';
echo 'test = ' . $Test . '<br>';  

André R.

Tuesday 08 July 2008 3:49:37 am

Ok, good to know what we're talking about :)
You need to implement hasAttribute, attribute and attributes functions when you want to use a object in the template language. Look at for instance kernel/classes/ezpersistentobject.php

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.