public function toString($field) { $item = $this->{$field} ?? null; if (!array_key_exists($field, $this->_data)) { return null; } elseif (is_null($item)) { return var_export($item, true); } elseif (is_bool($item)) { return var_export($item, true); } elseif (is_string($item)) { return $item; } elseif (is_numeric($item)) { return $item; } elseif (is_array($item)) { return \mongo\format\JSON::encode($item); } elseif ($item instanceof \mongo\type\Timestamp) { return $item->__toString(); } elseif ($item instanceof \mongo\type\DateTime) { return $item->__toString(); } elseif ($item instanceof \MongoDB\BSON\ObjectId) { return $item->__toString(); } elseif ($item instanceof \MongoDB\BSON\Binary) { return pretty_size(strlen($item->getData())); } elseif ($item instanceof \MongoDB\BSON\Javascript) { return \mongo\type\Javascript::bsonExport($item); } elseif ($item instanceof \MongoDB\BSON\Regex) { return \mongo\type\Regex::bsonExport($item); } elseif ($item instanceof \MongoDB\BSON\MaxKey) { return '$MaxKey'; } elseif ($item instanceof \MongoDB\BSON\MinKey) { return '$MinKey'; } else { return \mongo\format\JSON::encode($item); } }
public static function bsonSerialize($item, $tag, $flags) { if ($tag === 'DateTime') { return \mongo\type\DateTime::fromString($item); } elseif ($tag === 'Timestamp') { return \mongo\type\Timestamp::fromString($item); } elseif ($tag === 'ObjectID') { return new \MongoDB\BSON\ObjectID($item); } elseif ($tag === 'Binary') { return \mongo\type\Binary::bsonImport($item); } elseif ($tag === 'Javascript') { return \mongo\type\Javascript::bsonImport($item); } elseif ($tag === 'Regex') { return \mongo\type\Regex::bsonImport($item); } elseif ($tag === 'MaxKey') { return new \MongoDB\BSON\MaxKey(); } elseif ($tag === 'MinKey') { return new \MongoDB\BSON\MinKey(); } }