|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\UnifiedSpecTests\Constraint; |
| 4 | + |
| 5 | +use LogicException; |
| 6 | +use MongoDB\BSON\BinaryInterface; |
| 7 | +use MongoDB\BSON\DBPointer; |
| 8 | +use MongoDB\BSON\Decimal128Interface; |
| 9 | +use MongoDB\BSON\Int64; |
| 10 | +use MongoDB\BSON\JavascriptInterface; |
| 11 | +use MongoDB\BSON\MaxKeyInterface; |
| 12 | +use MongoDB\BSON\MinKeyInterface; |
| 13 | +use MongoDB\BSON\ObjectIdInterface; |
| 14 | +use MongoDB\BSON\RegexInterface; |
| 15 | +use MongoDB\BSON\Serializable; |
| 16 | +use MongoDB\BSON\Symbol; |
| 17 | +use MongoDB\BSON\TimestampInterface; |
| 18 | +use MongoDB\BSON\Type; |
| 19 | +use MongoDB\BSON\Undefined; |
| 20 | +use MongoDB\BSON\UTCDateTimeInterface; |
| 21 | +use MongoDB\Model\BSONArray; |
| 22 | +use MongoDB\Model\BSONDocument; |
| 23 | +use PHPUnit\Framework\Constraint\Constraint; |
| 24 | +use PHPUnit\Framework\Constraint\LogicalOr; |
| 25 | +use RuntimeException; |
| 26 | +use Symfony\Bridge\PhpUnit\ConstraintTrait; |
| 27 | +use function array_keys; |
| 28 | +use function array_map; |
| 29 | +use function count; |
| 30 | +use function in_array; |
| 31 | +use function is_array; |
| 32 | +use function is_bool; |
| 33 | +use function is_float; |
| 34 | +use function is_int; |
| 35 | +use function is_object; |
| 36 | +use function is_string; |
| 37 | +use function range; |
| 38 | +use function sprintf; |
| 39 | +use const PHP_INT_SIZE; |
| 40 | + |
| 41 | +final class IsBsonType extends Constraint |
| 42 | +{ |
| 43 | + use ConstraintTrait; |
| 44 | + |
| 45 | + /** @var array */ |
| 46 | + private static $types = [ |
| 47 | + 'double', |
| 48 | + 'string', |
| 49 | + 'object', |
| 50 | + 'array', |
| 51 | + 'binData', |
| 52 | + 'undefined', |
| 53 | + 'objectId', |
| 54 | + 'bool', |
| 55 | + 'date', |
| 56 | + 'null', |
| 57 | + 'regex', |
| 58 | + 'dbPointer', |
| 59 | + 'javascript', |
| 60 | + 'symbol', |
| 61 | + 'javascriptWithScope', |
| 62 | + 'int', |
| 63 | + 'timestamp', |
| 64 | + 'long', |
| 65 | + 'decimal', |
| 66 | + 'minKey', |
| 67 | + 'maxKey', |
| 68 | + ]; |
| 69 | + |
| 70 | + /** @var string */ |
| 71 | + private $type; |
| 72 | + |
| 73 | + public function __construct(string $type) |
| 74 | + { |
| 75 | + if (! in_array($type, self::$types)) { |
| 76 | + throw new RuntimeException(sprintf('Type specified for %s <%s> is not a valid type', self::class, $type)); |
| 77 | + } |
| 78 | + |
| 79 | + $this->type = $type; |
| 80 | + } |
| 81 | + |
| 82 | + public static function any() : LogicalOr |
| 83 | + { |
| 84 | + return self::anyOf(...self::$types); |
| 85 | + } |
| 86 | + |
| 87 | + public static function anyOf(string ...$types) : Constraint |
| 88 | + { |
| 89 | + if (count($types) === 1) { |
| 90 | + return new self(...$types); |
| 91 | + } |
| 92 | + |
| 93 | + return LogicalOr::fromConstraints(...array_map(function ($type) { |
| 94 | + return new self($type); |
| 95 | + }, $types)); |
| 96 | + } |
| 97 | + |
| 98 | + private function doMatches($other) : bool |
| 99 | + { |
| 100 | + switch ($this->type) { |
| 101 | + case 'double': |
| 102 | + return is_float($other); |
| 103 | + case 'string': |
| 104 | + return is_string($other); |
| 105 | + case 'object': |
| 106 | + return self::isObject($other); |
| 107 | + case 'array': |
| 108 | + return self::isArray($other); |
| 109 | + case 'binData': |
| 110 | + return $other instanceof BinaryInterface; |
| 111 | + case 'undefined': |
| 112 | + return $other instanceof Undefined; |
| 113 | + case 'objectId': |
| 114 | + return $other instanceof ObjectIdInterface; |
| 115 | + case 'bool': |
| 116 | + return is_bool($other); |
| 117 | + case 'date': |
| 118 | + return $other instanceof UTCDateTimeInterface; |
| 119 | + case 'null': |
| 120 | + return $other === null; |
| 121 | + case 'regex': |
| 122 | + return $other instanceof RegexInterface; |
| 123 | + case 'dbPointer': |
| 124 | + return $other instanceof DBPointer; |
| 125 | + case 'javascript': |
| 126 | + return $other instanceof JavascriptInterface && $other->getScope() === null; |
| 127 | + case 'symbol': |
| 128 | + return $other instanceof Symbol; |
| 129 | + case 'javascriptWithScope': |
| 130 | + return $other instanceof JavascriptInterface && $other->getScope() !== null; |
| 131 | + case 'int': |
| 132 | + return is_int($other); |
| 133 | + case 'timestamp': |
| 134 | + return $other instanceof TimestampInterface; |
| 135 | + case 'long': |
| 136 | + if (PHP_INT_SIZE == 4) { |
| 137 | + return $other instanceof Int64; |
| 138 | + } |
| 139 | + |
| 140 | + return is_int($other); |
| 141 | + case 'decimal': |
| 142 | + return $other instanceof Decimal128Interface; |
| 143 | + case 'minKey': |
| 144 | + return $other instanceof MinKeyInterface; |
| 145 | + case 'maxKey': |
| 146 | + return $other instanceof MaxKeyInterface; |
| 147 | + default: |
| 148 | + // This should already have been caught in the constructor |
| 149 | + throw new LogicException('Unsupported type: ' . $this->type); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private function doToString() : string |
| 154 | + { |
| 155 | + return sprintf('is of BSON type "%s"', $this->type); |
| 156 | + } |
| 157 | + |
| 158 | + private static function isArray($other) : bool |
| 159 | + { |
| 160 | + if ($other instanceof BSONArray) { |
| 161 | + return true; |
| 162 | + } |
| 163 | + |
| 164 | + // Serializable can produce an array or object, so recurse on its output |
| 165 | + if ($other instanceof Serializable) { |
| 166 | + return self::isArray($other->bsonSerialize()); |
| 167 | + } |
| 168 | + |
| 169 | + if (! is_array($other)) { |
| 170 | + return false; |
| 171 | + } |
| 172 | + |
| 173 | + // Empty and indexed arrays serialize as BSON arrays |
| 174 | + return self::isArrayEmptyOrIndexed($other); |
| 175 | + } |
| 176 | + |
| 177 | + private static function isObject($other) : bool |
| 178 | + { |
| 179 | + if ($other instanceof BSONDocument) { |
| 180 | + return true; |
| 181 | + } |
| 182 | + |
| 183 | + // Serializable can produce an array or object, so recurse on its output |
| 184 | + if ($other instanceof Serializable) { |
| 185 | + return self::isObject($other->bsonSerialize()); |
| 186 | + } |
| 187 | + |
| 188 | + // Non-empty, associative arrays serialize as BSON objects |
| 189 | + if (is_array($other)) { |
| 190 | + return ! self::isArrayEmptyOrIndexed($other); |
| 191 | + } |
| 192 | + |
| 193 | + if (! is_object($other)) { |
| 194 | + return false; |
| 195 | + } |
| 196 | + |
| 197 | + /* Serializable has already been handled, so any remaining instances of |
| 198 | + * Type will not serialize as BSON objects */ |
| 199 | + return ! $other instanceof Type; |
| 200 | + } |
| 201 | + |
| 202 | + private static function isArrayEmptyOrIndexed(array $a) : bool |
| 203 | + { |
| 204 | + if (empty($a)) { |
| 205 | + return true; |
| 206 | + } |
| 207 | + |
| 208 | + return array_keys($a) === range(0, count($a) - 1); |
| 209 | + } |
| 210 | +} |
0 commit comments