forked from angyvolin/mongodb-php-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
66 lines (57 loc) · 1.59 KB
/
functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Tequila\MongoDB;
use MongoDB\BSON\Serializable;
use Tequila\MongoDB\Exception\InvalidArgumentException;
/**
* @param $document
* @param array $typeMap
*
* @return object
*/
function applyTypeMap($document, array $typeMap)
{
return \MongoDB\BSON\toPHP(\MongoDB\BSON\fromPHP($document), $typeMap);
}
function ensureValidDocument($document)
{
if ($document instanceof Serializable) {
// do not validate Serializable instances since call to Serializable::bsonSerialize()
// will increase a memory usage by creating array|object copy of the document
return;
}
$arrayDocument = (array) $document;
$firstFieldName = key($arrayDocument);
if (!preg_match('/^[^$][^\.]*$/', $firstFieldName)) {
throw new InvalidArgumentException(
sprintf(
'Invalid field name "%s": field names cannot start with a dollar sign ("$") and cannot contain dots.',
$firstFieldName
)
);
}
}
function ensureValidUpdate(array $update)
{
$firstOperator = key($update);
if ('$' !== substr($firstOperator, 0, 1)) {
throw new InvalidArgumentException(
sprintf(
'Invalid $update document: first key "%s" is not an update operator.',
$firstOperator
)
);
}
}
function getType($value)
{
return is_object($value) ? get_class($value) : \gettype($value);
}
/**
* @param string $value - a value to check
*
* @return bool
*/
function isValidObjectId($value)
{
return 24 === strspn((string) $value, '0123456789ABCDEFabcdef');
}