MongoBinData.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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('MongoBinData', false)) {
  16. return;
  17. }
  18. use Alcaeus\MongoDbAdapter\TypeInterface;
  19. use MongoDB\BSON\Binary;
  20. use MongoDB\BSON\Type;
  21. class MongoBinData implements TypeInterface
  22. {
  23. /**
  24. * Generic binary data.
  25. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.constants.custom
  26. */
  27. const GENERIC = 0x0;
  28. /**
  29. * Function
  30. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.constants.func
  31. */
  32. const FUNC = 0x1;
  33. /**
  34. * Generic binary data (deprecated in favor of MongoBinData::GENERIC)
  35. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.constants.byte-array
  36. */
  37. const BYTE_ARRAY = 0x2;
  38. /**
  39. * Universally unique identifier (deprecated in favor of MongoBinData::UUID_RFC4122)
  40. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.constants.uuid
  41. */
  42. const UUID = 0x3;
  43. /**
  44. * Universally unique identifier (according to » RFC 4122)
  45. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.constants.custom
  46. */
  47. const UUID_RFC4122 = 0x4;
  48. /**
  49. * MD5
  50. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.constants.md5
  51. */
  52. const MD5 = 0x5;
  53. /**
  54. * User-defined type
  55. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.constants.custom
  56. */
  57. const CUSTOM = 0x80;
  58. /**
  59. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.props.bin
  60. * @var $bin
  61. */
  62. public $bin;
  63. /**
  64. * @link http://php.net/manual/en/class.mongobindata.php#mongobindata.props.type
  65. * @var $type
  66. */
  67. public $type;
  68. /**
  69. * Creates a new binary data object.
  70. *
  71. * @link http://php.net/manual/en/mongobindata.construct.php
  72. * @param string $data Binary data
  73. * @param int $type Data type
  74. */
  75. public function __construct($data, $type = 2)
  76. {
  77. if ($data instanceof Binary) {
  78. $this->bin = $data->getData();
  79. $this->type = $data->getType();
  80. } else {
  81. $this->bin = $data;
  82. $this->type = $type;
  83. }
  84. }
  85. /**
  86. * Returns the string "<Mongo Binary Data>". To access the contents of a MongoBinData, use the bin field.
  87. *
  88. * @return string
  89. */
  90. public function __toString()
  91. {
  92. return '<Mongo Binary Data>';
  93. }
  94. /**
  95. * Converts this MongoBinData to the new BSON Binary type
  96. *
  97. * @return Binary
  98. * @internal This method is not part of the ext-mongo API
  99. */
  100. public function toBSONType()
  101. {
  102. return new Binary($this->bin, $this->type);
  103. }
  104. }