Skip to content

Commit

Permalink
PublicObject can now be treated as an array
Browse files Browse the repository at this point in the history
  • Loading branch information
minond committed Dec 9, 2013
1 parent be9a6a9 commit 85a87e3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
37 changes: 36 additions & 1 deletion src/Efficio/Utilitatis/PublicObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Efficio\Utilitatis;

use ArrayAccess;

/**
* easy to work with objects
*/
class PublicObject
class PublicObject implements ArrayAccess
{
/**
* internal storage
Expand Down Expand Up @@ -56,5 +58,38 @@ public function __unset($var)
unset($this->data[ $var ]);
return true;
}

/**
* @param string $var
* @param mixed $val
*/
public function offsetSet($var, $val)
{
return $this->__set($var, $val);
}

/**
* @param string $var
*/
public function offsetGet($var)
{
return $this->__get($var);
}

/**
* @param string $var
*/
public function offsetExists($var)
{
return $this->__isset($var);
}

/**
* @param string $var
*/
public function offsetUnset($var)
{
return $this->__unset($var);
}
}

33 changes: 29 additions & 4 deletions tests/Utilitatis/PublicObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,60 @@ public function setUp()
$this->obj = new PublicObject;
}

public function testPropertiesSetAreFlaggedAsSet()
public function testPropertiesSetAreFlaggedAsSetUsingObjectNotation()
{
$this->obj->test = 1;
$this->assertTrue(isset($this->obj->test));
}

public function testPropertiesNotSetAreFlaggedAsNotSet()
public function testPropertiesSetAreFlaggedAsSetUsingArrayNotation()
{
$this->obj['test'] = 1;
$this->assertTrue(isset($this->obj['test']));
}

public function testPropertiesNotSetAreFlaggedAsNotSetUsingObjectNotation()
{
$this->assertFalse(isset($this->obj->test));
}

public function testPropertiesCanBeRetrived()
public function testPropertiesNotSetAreFlaggedAsNotSetUsingArrayNotation()
{
$this->assertFalse(isset($this->obj['test']));
}

public function testPropertiesCanBeRetrivedUsingObjectNotation()
{
$this->obj->test = 1;
$this->assertEquals(1, $this->obj->test);
}

public function testPropertiesCanBeRetrivedUsingArrayNotation()
{
$this->obj['test'] = 1;
$this->assertEquals(1, $this->obj['test']);
}

public function testDataCanBePassedToConstructor()
{
$this->obj = new PublicObject([ 'test' => 0 ]);
$this->assertTrue(isset($this->obj->test));
}

public function testPropertiesCanBeUnset()
public function testPropertiesCanBeUnsetUsingObjectNotation()
{
$this->obj->test = 1;
$this->assertEquals(1, $this->obj->test);
unset($this->obj->test);
$this->assertFalse(isset($this->obj->test));
}

public function testPropertiesCanBeUnsetUsingArrayNotation()
{
$this->obj['test'] = 1;
$this->assertEquals(1, $this->obj['test']);
unset($this->obj['test']);
$this->assertFalse(isset($this->obj['test']));
}
}

0 comments on commit 85a87e3

Please sign in to comment.