/
Builder.php
149 lines (129 loc) · 3.97 KB
/
Builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace DatastoreHelper\Key;
use DatastoreHelper\Exception\MissingFieldsException;
use DatastoreHelper\Exception\ParameterException;
use DatastoreHelper\Interfaces\IBuilder;
class Builder extends Path\Builder implements IBuilder
{
/**
* @var string $datasetId
*/
protected $datasetId = null;
/**
* @var string $namespace
*/
protected $namespace = null;
/**
* @var \Google_Service_Datastore_KeyPathElement[] $parent
*/
protected $parent = [];
/**
* Builder constructor.
*
* @param string $datasetId
* @param string $namespace
* @param \Google_Service_Datastore_KeyPathElement|\Google_Service_Datastore_KeyPathElement[] $path_or_paths
*/
public function __construct($datasetId = null, $namespace = null, $path_or_paths = null)
{
if ($datasetId !== null) $this->withDatasetId($datasetId);
if ($namespace !== null) $this->withNamespace($namespace);
if ($path_or_paths !== null) $this->withPath($path_or_paths);
}
/**
* @return string
*/
public function getDatasetId()
{
return $this->datasetId;
}
/**
* @param string $datasetId
* @return $this
*/
public function withDatasetId($datasetId)
{
$this->datasetId = $datasetId;
return $this;
}
/**
* @return string
*/
public function getNamespace()
{
return $this->namespace;
}
/**
* @param string $namespace
* @return $this
*/
public function withNamespace($namespace)
{
$this->namespace = $namespace;
return $this;
}
/**
* @return \Google_Service_Datastore_KeyPathElement[]
*/
public function getParentPath()
{
return $this->parent;
}
/**
* @return \Google_Service_Datastore_KeyPathElement[]
*/
public function getPath()
{
$path = $this->parent;
$path[] = parent::build();
return $path;
}
/**
* @param \Google_Service_Datastore_Key|\Google_Service_Datastore_KeyPathElement|Path\Builder|Builder $parentKey_or_parentPath
* @return $this
* @throws ParameterException
*/
public function withParent($parentKey_or_parentPath)
{
if ($parentKey_or_parentPath instanceof Builder)
$this->parent = $parentKey_or_parentPath->getPath();
else if ($parentKey_or_parentPath instanceof Path\Builder)
$this->parent = $parentKey_or_parentPath->build();
else if ($parentKey_or_parentPath instanceof \Google_Service_Datastore_Key)
$this->parent = $parentKey_or_parentPath->getPath();
else if ($parentKey_or_parentPath instanceof \Google_Service_Datastore_KeyPathElement)
$this->parent = $parentKey_or_parentPath;
else
throw new ParameterException(__METHOD__, [\Google_Service_Datastore_Key::class, \Google_Service_Datastore_KeyPathElement::class, Path\Builder::class, Builder::class ]);
return $this;
}
/**
* @return \Google_Service_Datastore_Key
* @throws MissingFieldsException
*/
public function build()
{
$partitionId = new \Google_Service_Datastore_PartitionId;
$partitionId->setDatasetId($this->datasetId);
$partitionId->setNamespace($this->namespace);
$key = new \Google_Service_Datastore_Key;
$key->setPartitionId($partitionId);
$key->setPath($this->getPath());
return $key;
}
/**
* @param \Google_Service_Datastore_Key $key
* @return Builder
*/
public static function fromKey($key)
{
$keyBuilder = new self;
$keyBuilder->parent = $key->getPath();
/** @var \Google_Service_Datastore_KeyPathElement $latestPath */
$latestPath = array_pop($keyBuilder->parent);
$keyBuilder->kind = $latestPath->getKind();
$keyBuilder->id = $latestPath->getId();
$keyBuilder->name = $latestPath->getName();
return $keyBuilder;
}
}