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