MongoDBRef.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. class MongoDBRef
  16. {
  17. /**
  18. * @static
  19. * @var $refKey
  20. */
  21. protected static $refKey = '$ref';
  22. /**
  23. * @static
  24. * @var $idKey
  25. */
  26. protected static $idKey = '$id';
  27. /**
  28. * If no database is given, the current database is used.
  29. *
  30. * @link http://php.net/manual/en/mongodbref.create.php
  31. * @static
  32. * @param string $collection Collection name (without the database name)
  33. * @param mixed $id The _id field of the object to which to link
  34. * @param string $database Database name
  35. * @return array Returns the reference
  36. */
  37. public static function create($collection, $id, $database = null)
  38. {
  39. $ref = [
  40. static::$refKey => $collection,
  41. static::$idKey => $id
  42. ];
  43. if ($database !== null) {
  44. $ref['$db'] = $database;
  45. }
  46. return $ref;
  47. }
  48. /**
  49. * This not actually follow the reference, so it does not determine if it is broken or not.
  50. * It merely checks that $ref is in valid database reference format (in that it is an object or array with $ref and $id fields).
  51. *
  52. * @link http://php.net/manual/en/mongodbref.isref.php
  53. * @static
  54. * @param mixed $ref Array or object to check
  55. * @return boolean Returns true if $ref is a reference
  56. */
  57. public static function isRef($ref)
  58. {
  59. $check = (array) $ref;
  60. return array_key_exists(static::$refKey, $check) && array_key_exists(static::$idKey, $check);
  61. }
  62. /**
  63. * Fetches the object pointed to by a reference
  64. * @link http://php.net/manual/en/mongodbref.get.php
  65. * @static
  66. * @param MongoDB $db Database to use
  67. * @param array $ref Reference to fetch
  68. * @return array|null Returns the document to which the reference refers or null if the document does not exist (the reference is broken)
  69. */
  70. public static function get($db, $ref)
  71. {
  72. if (! static::isRef($ref)) {
  73. return null;
  74. }
  75. return $db->selectCollection($ref[static::$refKey])->findOne(['_id' => $ref[static::$idKey]]);
  76. }
  77. }