Skip to content

Commit db1a4d8

Browse files
committed
Added data object
1 parent 6290613 commit db1a4d8

2 files changed

Lines changed: 177 additions & 0 deletions

File tree

src/Kore/DataObject/Struct.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Kore\DataObject;
4+
5+
class Struct
6+
{
7+
/**
8+
* Generic constructor
9+
*
10+
* @param array $values
11+
* @return void
12+
*/
13+
public function __construct(array $values = array())
14+
{
15+
foreach ($values as $name => $value) {
16+
$this->$name = $value;
17+
}
18+
}
19+
20+
/**
21+
* Throw exception on get on unknown property
22+
*
23+
* @param string $name
24+
* @return void
25+
*/
26+
public function __get($name)
27+
{
28+
throw new \OutOfRangeException("Unknown property \${$name} in " . get_class($this) . ".");
29+
}
30+
31+
/**
32+
* Throw exception on set on unknown property
33+
*
34+
* @param string $name
35+
* @param mixed $value
36+
* @return void
37+
*/
38+
public function __set($name, $value)
39+
{
40+
throw new \OutOfRangeException("Unknown property \${$name} in " . get_class($this) . ".");
41+
}
42+
43+
/**
44+
* Throw exception on unset on unknown property
45+
*
46+
* @param string $name
47+
* @return void
48+
*/
49+
public function __unset($name)
50+
{
51+
throw new \OutOfRangeException("Unknown property \${$name} in " . get_class($this) . ".");
52+
}
53+
54+
/**
55+
* Deep clone for structs
56+
*
57+
* @return void
58+
*/
59+
public function __clone()
60+
{
61+
foreach ($this as $property => $value) {
62+
if (is_object($value)) {
63+
$this->$property = clone $value;
64+
}
65+
66+
if (is_array($value)) {
67+
$this->cloneArray($this->$property);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* Clone array
74+
*
75+
* @param array $array
76+
*/
77+
private function cloneArray(array &$array)
78+
{
79+
foreach ($array as $key => $value) {
80+
if (is_object($value)) {
81+
$array[$key] = clone $value;
82+
}
83+
84+
if (is_array($value)) {
85+
$this->cloneArray($array[$key]);
86+
}
87+
}
88+
}
89+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace Kore\DataObject;
4+
5+
class TestStruct extends Struct {
6+
public $property;
7+
}
8+
9+
/**
10+
* @covers \Kagency\CouchdbEndpoint\Struct
11+
*/
12+
class StructTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testGetValue()
15+
{
16+
$struct = new TestStruct();
17+
18+
$this->assertNull($struct->property);
19+
}
20+
21+
public function testConstructor()
22+
{
23+
$struct = new TestStruct(
24+
array(
25+
'property' => 42,
26+
)
27+
);
28+
29+
$this->assertSame(42, $struct->property);
30+
}
31+
32+
public function testSetValue()
33+
{
34+
$struct = new TestStruct();
35+
$struct->property = 42;
36+
37+
$this->assertSame(42, $struct->property);
38+
}
39+
40+
public function testUnsetValue()
41+
{
42+
$struct = new TestStruct();
43+
$struct->property = 42;
44+
unset($struct->property);
45+
46+
$this->assertFalse(isset($struct->property));
47+
}
48+
49+
/**
50+
* @expectedException \OutOfRangeException
51+
*/
52+
public function testGetUnknownValue()
53+
{
54+
$struct = new TestStruct();
55+
56+
$this->assertNull($struct->unknown);
57+
}
58+
59+
/**
60+
* @expectedException \OutOfRangeException
61+
*/
62+
public function testConstructorUnknwonValue()
63+
{
64+
$struct = new TestStruct(
65+
array(
66+
'unknown' => 42,
67+
)
68+
);
69+
}
70+
71+
/**
72+
* @expectedException \OutOfRangeException
73+
*/
74+
public function testSetUnknownValue()
75+
{
76+
$struct = new TestStruct();
77+
$struct->unknown = 42;
78+
}
79+
80+
/**
81+
* @expectedException \OutOfRangeException
82+
*/
83+
public function testUnsetUnknownValue()
84+
{
85+
$struct = new TestStruct();
86+
unset($struct->unknown);
87+
}
88+
}

0 commit comments

Comments
 (0)