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