MongoDBRef.php 2.9 KB

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