PHP8 MongoDB序列化為BSON系列化

2024-04-08 10:15 更新

陣 列

如果數(shù)組是打包數(shù)組,即空數(shù)組或 鍵從 0 開始,是連續(xù)的,沒有間隙:BSON 數(shù)組。

如果數(shù)組未打包(即具有關(guān)聯(lián)(字符串)鍵,則 鍵不從 0 開始,或者當(dāng)有間隙時(shí):: BSON 對(duì)象

頂級(jí)(根)文檔,始終序列化為 BSON文件。

例子

它們序列化為 BSON 數(shù)組:

[ 8, 5, 2, 3 ] => [ 8, 5, 2, 3 ]
[ 0 => 4, 1 => 9 ] => [ 4, 9 ]

這些序列化為 BSON 文檔:

[ 0 => 1, 2 => 8, 3 => 12 ] => { "0" : 1, "2" : 8, "3" : 12 }
[ "foo" => 42 ] => { "foo" : 42 }
[ 1 => 9, 0 => 10 ] => { "1" : 9, "0" : 10 }

請(qǐng)注意,這五個(gè)示例是完整 文檔,并且僅表示 公文。

對(duì)象

如果對(duì)象屬于 stdClass 類,則序列化 作為 BSON 文檔。

如果對(duì)象是實(shí)現(xiàn) MongoDB\BSON\Type 的受支持類,則使用 BSON 該特定類型的序列化邏輯。MongoDB\BSON\Type 實(shí)例(不包括 MongoDB\BSON\Serializable)可能只有 序列化為文檔字段值。嘗試序列化這樣的 對(duì)象作為根文檔將拋出 MongoDB\Driver\Exception\UnexpectedValueException

如果對(duì)象屬于實(shí)現(xiàn) MongoDB\BSON\Type 接口的未知類,則拋出 MongoDB\Driver\Exception\UnexpectedValueException

如果對(duì)象屬于任何其他類,則不實(shí)現(xiàn)任何特殊 接口,序列化為BSON文檔。僅保留公共屬性,忽略受保護(hù)的私有屬性。

如果對(duì)象屬于實(shí)現(xiàn) MongoDB\BSON\Serializable 接口的類,請(qǐng)調(diào)用 MongoDB\BSON\Serializable::bsonSerialize() 并使用 返回的數(shù)組或 stdClass 序列化為 BSON 文檔或數(shù)組。BSON 類型將由以下因素決定:

  1. 根文檔必須序列化為 BSON 公文。
  2. MongoDB\BSON\Persistable 對(duì)象必須是 序列化為 BSON 文檔。
  3. 如果 MongoDB\BSON\Serializable::bsonSerialize() 返回打包數(shù)組,則序列化為 BSON 數(shù)組。
  4. 如果 MongoDB\BSON\Serializable::bsonSerialize() 返回非打包數(shù)組或 stdClass, 序列化為 BSON 文檔。
  5. 如果 MongoDB\BSON\Serializable::bsonSerialize() 未返回?cái)?shù)組或 stdClass,則引發(fā) MongoDB\Driver\Exception\UnexpectedValueException 異常。

如果對(duì)象屬于實(shí)現(xiàn) MongoDB\BSON\Persistable 接口的類( 表示 MongoDB\BSON\Serializable),獲取 屬性以與前面段落類似的方式,但也添加了一個(gè)附加屬性__pclass作為二進(jìn)制值,其中子類型和數(shù)據(jù)帶有完全限定的類名 正在序列化的對(duì)象。0x80

將 __pclass 屬性添加到數(shù)組中,或者 MongoDB\BSON\Serializable::bsonSerialize() 返回的對(duì)象,其中 表示它將覆蓋任何__pclass鍵/屬性 MongoDB\BSON\Serializable::bsonSerialize() 返回值。如果要避免此行為并設(shè)置自己的 __pclass 值,則不能實(shí)現(xiàn) MongoDB\BSON\Persistable 和 應(yīng)該直接實(shí)現(xiàn) MongoDB\BSON\Serializable。

例子

<?php

class stdClass {
  public $foo = 42;
} // => { "foo" : 42 }

class MyClass {
  public $foo = 42;
  protected $prot = "wine";
  private $fpr = "cheese";
} // => { "foo" : 42 }

class AnotherClass1 implements MongoDB\BSON\Serializable {
  public $foo = 42;
  protected $prot = "wine";
  private $fpr = "cheese";
  function bsonSerialize(): array {
      return [ 'foo' => $this->foo, 'prot' => $this->prot ];
  }
} // => { "foo" : 42, "prot" : "wine" }

class AnotherClass2 implements MongoDB\BSON\Serializable {
  public $foo = 42;
  function bsonSerialize(): self {
      return $this;
  }
} // => MongoDB\Driver\Exception\UnexpectedValueException("bsonSerialize() did not return an array or stdClass")

class AnotherClass3 implements MongoDB\BSON\Serializable {
  private $elements = [ 'foo', 'bar' ];
  function bsonSerialize(): array {
      return $this->elements;
  }
} // => { "0" : "foo", "1" : "bar" }

class ContainerClass implements MongoDB\BSON\Serializable {
  public $things = AnotherClass4 implements MongoDB\BSON\Serializable {
    private $elements = [ 0 => 'foo', 2 => 'bar' ];
    function bsonSerialize(): array {
      return $this->elements;
    }
  }
  function bsonSerialize(): array {
      return [ 'things' => $this->things ];
  }
} // => { "things" : { "0" : "foo", "2" : "bar" } }

class ContainerClass implements MongoDB\BSON\Serializable {
  public $things = AnotherClass5 implements MongoDB\BSON\Serializable {
    private $elements = [ 0 => 'foo', 2 => 'bar' ];
    function bsonSerialize(): array {
      return array_values($this->elements);
    }
  }
  function bsonSerialize(): array {
      return [ 'things' => $this->things ];
  }
} // => { "things" : [ "foo", "bar" ] }

class ContainerClass implements MongoDB\BSON\Serializable {
  public $things = AnotherClass6 implements MongoDB\BSON\Serializable {
    private $elements = [ 'foo', 'bar' ];
    function bsonSerialize(): array {
      return (object) $this->elements;
    }
  }
  function bsonSerialize(): array {
      return [ 'things' => $this->things ];
  }
} // => { "things" : { "0" : "foo", "1" : "bar" } }

class UpperClass implements MongoDB\BSON\Persistable {
  public $foo = 42;
  protected $prot = "wine";
  private $fpr = "cheese";
  function bsonSerialize(): array {
      return [ 'foo' => $this->foo, 'prot' => $this->prot ];
  }
} // => { "foo" : 42, "prot" : "wine", "__pclass" : { "$type" : "80", "$binary" : "VXBwZXJDbGFzcw==" } }
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)