MongoBinData.php 3.4 KB

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