1: <?php
2:
3: namespace PHPixie\ORM\Planners\Planner;
4:
5: class Document extends \PHPixie\ORM\Planners\Planner
6: {
7: protected function explodePath($path)
8: {
9: return explode('.', $path);
10: }
11:
12: public function getDocument($document, $path, $createMissing = false)
13: {
14: $explodedPath = $this->explodePath($path);
15: return $this->getDocumentByExplodedPath($document, $explodedPath, $createMissing);
16: }
17:
18: public function getArrayNode($document, $path, $createMissing = false)
19: {
20: list($parent, $key) = $this->getParentDocumentAndKey($document, $path);
21: if($document === null)
22: return null;
23:
24: $property = $parent->get($key);
25: if($property !== null) {
26: if(!($property instanceof \PHPixie\ORM\Data\Types\Document\Node\ArrayNode))
27: throw new \PHPixie\ORM\Exception\Data("$path is not an array node");
28: }elseif($createMissing) {
29: $parent->addArray($key);
30: }else{
31: return null;
32: }
33: return $parent->get($key);
34: }
35:
36: public function getParentDocumentAndKey($document, $path, $createMissing = false)
37: {
38: $explodedPath = $this->explodePath($path);
39: $key = array_pop($explodedPath);
40: $parent = $this->getDocumentByExplodedPath($document, $explodedPath, $createMissing);
41: return array($parent, $key);
42: }
43:
44: protected function getDocumentByExplodedPath($document, $explodedPath, $createMissing)
45: {
46: $last = count($explodedPath) - 1;
47: foreach($explodedPath as $i => $key) {
48: $property = $document->get($key);
49: if($property !== null) {
50: if($i === $last && !($property instanceof \PHPixie\ORM\Data\Types\Document\Node\Document)) {
51: $path = implode('.', array_slice($explodedPath, 0, $i+1));
52: throw new \PHPixie\ORM\Exception\Data("$path is not a document node.");
53: }
54: }elseif($createMissing) {
55: $document->addDocument($key);
56: }else{
57: return null;
58: }
59: $document = $document->get($key);
60: }
61: return $document;
62: }
63: }