From 7a644b1506b36024026ff874e9774f491234f06d Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 30 Jul 2011 12:16:51 -0400 Subject: [PATCH] Add base\Struct from phalanx --- base/struct.php | 93 +++++++++++++++++++++++ testing/tests/base/struct_test.php | 116 +++++++++++++++++++++++++++++ 2 files changed, 209 insertions(+) create mode 100644 base/struct.php create mode 100644 testing/tests/base/struct_test.php diff --git a/base/struct.php b/base/struct.php new file mode 100644 index 0000000..2ef8382 --- /dev/null +++ b/base/struct.php @@ -0,0 +1,93 @@ +. + +namespace hoplite\base; + +/*! + A struct can be used to maintain a list of properties. Any properties not + strictly defined in the |$fields| array will throw exceptions when accessed. +*/ +class Struct +{ + /*! @var array The fields of this struct. */ + protected $fields = array(); + + /*! @var array The data that corresponds to the |$fields|. */ + protected $data = array(); + + /*! + Creates a new Struct, optionally initializing the data members from an + iterable object. + */ + public function __construct($data = array()) + { + foreach ($data as $key => $value) + if (!in_array($key, $this->fields)) + throw new StructException('Invalid field "' . $key . '"'); + else + $this->data[$key] = $value; + } + + /*! Gets a specified key. This can return NULL. */ + public function __get($key) + { + if (!in_array($key, $this->fields)) + throw new StructException('Invalid field "' . $key . '"'); + + if (!isset($this->data[$key])) + return NULL; + + return $this->data[$key]; + } + public function Get($key) { return $this->__get($key); } + + /*! Sets the specified key-value pair. */ + public function __set($key, $value) + { + if (!in_array($key, $this->fields)) + throw new StructException('Cannot set value for invalid field "' . $key . '"'); + $this->data[$key] = $value; + } + public function Set($key, $value) { $this->__set($key, $value); } + + /*! Takes values from an array or object and sets the relevant values. */ + public function SetFrom($array) + { + foreach ($array as $key => $value) + if (in_array($key, $this->fields)) + $this->data[$key] = $value; + } + + /*! Returns the data members as an array. */ + public function ToArray() + { + return $this->data; + } + + /*! Returns the number of members in the struct. */ + public function Count() + { + return count($this->fields); + } + + /*! Returns the list of fields the Struct has. */ + public function GetFields() + { + return $this->fields; + } +} + +class StructException extends \Exception {} diff --git a/testing/tests/base/struct_test.php b/testing/tests/base/struct_test.php new file mode 100644 index 0000000..8eacbaa --- /dev/null +++ b/testing/tests/base/struct_test.php @@ -0,0 +1,116 @@ +. + +namespace hoplite\test; +use \hoplite\base as base; + +require_once TEST_ROOT . '/tests/base.php'; + +class TestStruct extends base\Struct +{ + protected $fields = array( + 'first', + 'second', + 'third' + ); +} + +class StructTest extends \PHPUnit_Framework_TestCase +{ + public $struct; + + public function setUp() + { + $this->struct = new TestStruct(); + } + + public function testCount() + { + $this->assertEquals(3, $this->struct->Count()); + $this->struct->first = 'foo'; + $this->assertEquals(3, $this->struct->Count()); + } + + public function testSet() + { + $this->struct->first = 1; + $this->struct->second = 2; + $this->struct->third = 3; + } + + public function testSetInvalid() + { + $this->setExpectedException('hoplite\base\StructException'); + $this->struct->fourth = 4; + } + + public function testGetNull() + { + $this->assertNull($this->struct->first); + $this->assertNull($this->struct->second); + $this->assertNull($this->struct->third); + } + + public function testGet() + { + $this->struct->first = 1; + $this->assertEquals(1, $this->struct->first); + } + + public function testGetInvalid() + { + $this->setExpectedException('hoplite\base\StructException'); + $foo = $this->struct->fourth; + } + + public function testSetFromArray() + { + $array = array( + 'first' => 1, + 'second' => 2, + 'fourth' => 4 + ); + $this->struct->SetFrom($array); + $this->assertEquals(1, $this->struct->first); + $this->assertEquals(2, $this->struct->second); + $this->assertNull($this->struct->third); + $this->assertEquals(3, $this->struct->Count()); + } + + public function testSetFromObject() + { + $obj = new \StdClass(); + $obj->first = 1; + $obj->second = 2; + $obj->fourth = 4; + $this->struct->SetFrom($obj); + $this->assertEquals(1, $this->struct->first); + $this->assertEquals(2, $this->struct->second); + $this->assertNull($this->struct->third); + $this->assertEquals(3, $this->struct->Count()); + } + + public function testToArray() + { + $this->struct->first = 'alpha'; + $this->struct->third = 'bravo'; + $array = array( + 'first' => 'alpha', + 'third' => 'bravo' + ); + $this->assertEquals($array, $this->struct->ToArray()); + } +} -- 2.43.5