1: <?php
2:
3: namespace PHPixie\ORM\Data\Types\Document;
4:
5: abstract class Node
6: {
7: protected $documentBuilder;
8:
9: public function __construct($documentBuilder)
10: {
11: $this->documentBuilder = $documentBuilder;
12: }
13:
14: protected function convertValue($value)
15: {
16: $isArray = is_array($value);
17: $isAssociativeArray = $isArray && !empty($value) && !is_numeric(key($value));
18:
19: if ($value instanceof \stdClass || $isAssociativeArray) {
20: $value = $this->documentBuilder->document($value);
21: } elseif ($isArray) {
22: $value = $this->documentBuilder->arrayNode($value);
23: }
24:
25: return $value;
26: }
27:
28: protected function convertNode($type)
29: {
30: if ($type instanceof Node) {
31: $type = $type->data();
32: }elseif (is_object($type) && !($type instanceof \stdClass)){
33: $class = get_class($type);
34: throw new \PHPixie\ORM\Exception\Model("Only \stdClass instances are allowed, an instance of $class passed.");
35: }
36:
37: return $type;
38: }
39:
40: abstract public function data();
41: }