1: <?php
2:
3: namespace PHPixie\ORM\Data\Types\Document\Node;
4:
5: class Document extends \PHPixie\ORM\Data\Types\Document\Node
6: {
7: protected $properties = array();
8:
9: public function __construct($documentBuilder, $data = null)
10: {
11: parent::__construct($documentBuilder);
12: if($data !== null) {
13: foreach ($data as $key => $value) {
14: $this->set($key, $value);
15: }
16: }
17: }
18:
19: public function data()
20: {
21: $current = new \stdClass;
22: foreach($this->properties as $key => $value)
23: $current->$key = $this->convertNode($value);
24:
25: return $current;
26: }
27:
28: public function set($key, $value = null)
29: {
30: $this->properties[$key] = $this->convertValue($value);
31: return $this;
32: }
33:
34: public function remove($key)
35: {
36: unset($this->properties[$key]);
37: return $this;
38: }
39:
40: public function get($key, $default = null)
41: {
42: if(array_key_exists($key, $this->properties))
43: return $this->properties[$key];
44:
45: return $default;
46: }
47:
48: public function getRequired($key)
49: {
50: if(array_key_exists($key, $this->properties))
51: return $this->properties[$key];
52:
53: throw new \PHPixie\ORM\Exception\Data("Field '$key' is not set");
54: }
55:
56: public function addArray($key, $data = array())
57: {
58: return $this->$key = $this->documentBuilder->arrayNode($data);
59: }
60:
61: public function addDocument($key, $data = null)
62: {
63: return $this->$key = $this->documentBuilder->document($data);
64: }
65:
66: public function __set($key, $value)
67: {
68: $this->set($key, $this->convertValue($value));
69: }
70:
71: public function __get($key)
72: {
73: return $this->properties[$key];
74: }
75:
76: public function __isset($key)
77: {
78: return array_key_exists($key, $this->properties);
79: }
80:
81: public function __unset($key)
82: {
83: $this->remove($key);
84: }
85:
86: }
87: