1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JamesMoss\Flywheel; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Document |
7
|
|
|
* |
8
|
|
|
* Represents a document in Flywheel. Essentially this is a Plain Old PHP |
9
|
|
|
* Object (POPO). |
10
|
|
|
* |
11
|
|
|
* The only important property on this object is $__flywheelDocId. It's used |
12
|
|
|
* to ensure uniqueness when storing a document. You can set this yourself using |
13
|
|
|
* the `setId()` method or if omitted it will be autogenerated for you. |
14
|
|
|
*/ |
15
|
|
|
class Document implements DocumentInterface |
16
|
|
|
{ |
17
|
|
|
protected $__flywheelDocId; |
18
|
|
|
protected $__flywheelInitialId; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Constructor |
22
|
|
|
* |
23
|
|
|
* @param array $data An associative array, each key/value pair will be |
24
|
|
|
* turned into properties on this object. |
25
|
|
|
*/ |
26
|
32 |
|
public function __construct(array $data = array()) |
27
|
|
|
{ |
28
|
32 |
|
foreach ($data as $key => $value) { |
29
|
32 |
|
$this->{$key} = $value; |
30
|
32 |
|
} |
31
|
32 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the document ID. |
35
|
|
|
* |
36
|
|
|
* @param string $id |
37
|
|
|
*/ |
38
|
20 |
|
public function setId($id) |
39
|
|
|
{ |
40
|
20 |
|
$id = (string)$id; |
41
|
|
|
|
42
|
20 |
|
if(!isset($this->__flywheelInitialId)) { |
43
|
20 |
|
$this->__flywheelInitialId = $id; |
44
|
20 |
|
} |
45
|
|
|
|
46
|
20 |
|
$this->__flywheelDocId = $id; |
47
|
|
|
|
48
|
20 |
|
return $id; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the document ID. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
8 |
|
public function getId() |
57
|
|
|
{ |
58
|
8 |
|
return $this->__flywheelDocId; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the initial document ID. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
2 |
|
public function getInitialId() |
67
|
|
|
{ |
68
|
2 |
|
return $this->__flywheelInitialId; |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
public function getNestedProperty($field, &$found = false) |
72
|
|
|
{ |
73
|
5 |
|
$found = false; |
74
|
5 |
|
$parts = explode('.', $field); |
75
|
5 |
|
$context = $this; |
76
|
5 |
|
foreach ($parts as $part) { |
77
|
5 |
|
if (trim($part) == '') { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
5 |
|
if (is_object($context)) { |
82
|
5 |
|
if(!property_exists($context, $part)) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
5 |
|
$context = $context->{$part}; |
87
|
5 |
|
} else if (is_array($context)) { |
88
|
4 |
|
if(!array_key_exists($part, $context)) { |
89
|
2 |
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
$context = $context[$part]; |
93
|
4 |
|
} |
94
|
5 |
|
} |
95
|
|
|
|
96
|
5 |
|
$found = true; |
97
|
|
|
|
98
|
5 |
|
return $context; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|