1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
if (class_exists('MongoDBRef', false)) { |
17
|
|
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
class MongoDBRef |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @static |
24
|
|
|
* @var $refKey |
25
|
|
|
*/ |
26
|
|
|
protected static $refKey = '$ref'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @static |
30
|
|
|
* @var $idKey |
31
|
|
|
*/ |
32
|
|
|
protected static $idKey = '$id'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* If no database is given, the current database is used. |
36
|
|
|
* |
37
|
|
|
* @link http://php.net/manual/en/mongodbref.create.php |
38
|
|
|
* @static |
39
|
|
|
* @param string $collection Collection name (without the database name) |
40
|
|
|
* @param mixed $id The _id field of the object to which to link |
41
|
|
|
* @param string $database Database name |
42
|
|
|
* @return array Returns the reference |
43
|
|
|
*/ |
44
|
|
|
public static function create($collection, $id, $database = null) |
45
|
|
|
{ |
46
|
|
|
$ref = [ |
47
|
|
|
static::$refKey => $collection, |
48
|
|
|
static::$idKey => $id |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
if ($database !== null) { |
52
|
|
|
$ref['$db'] = $database; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $ref; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* This not actually follow the reference, so it does not determine if it is broken or not. |
60
|
|
|
* It merely checks that $ref is in valid database reference format (in that it is an object or array with $ref and $id fields). |
61
|
|
|
* |
62
|
|
|
* @link http://php.net/manual/en/mongodbref.isref.php |
63
|
|
|
* @static |
64
|
|
|
* @param mixed $ref Array or object to check |
65
|
|
|
* @return boolean Returns true if $ref is a reference |
66
|
|
|
*/ |
67
|
|
|
public static function isRef($ref) |
68
|
|
|
{ |
69
|
|
|
$check = (array) $ref; |
70
|
|
|
|
71
|
|
|
return array_key_exists(static::$refKey, $check) && array_key_exists(static::$idKey, $check); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Fetches the object pointed to by a reference |
76
|
|
|
* @link http://php.net/manual/en/mongodbref.get.php |
77
|
|
|
* @static |
78
|
|
|
* @param MongoDB $db Database to use |
79
|
|
|
* @param array $ref Reference to fetch |
80
|
|
|
* @return array|null Returns the document to which the reference refers or null if the document does not exist (the reference is broken) |
81
|
|
|
*/ |
82
|
|
|
public static function get($db, $ref) |
83
|
|
|
{ |
84
|
|
|
if (! static::isRef($ref)) { |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $db->selectCollection($ref[static::$refKey])->findOne(['_id' => $ref[static::$idKey]]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|