<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->

<refentry xml:id="mongodb-bson-serializable.bsonserialize" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>MongoDB\BSON\Serializable::bsonSerialize</refname>
  <refpurpose>Provides an array or document to serialize as BSON</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <modifier>abstract</modifier> <modifier>public</modifier> <type class="union"><type>array</type><type>stdClass</type><type>MongoDB\BSON\Document</type><type>MongoDB\BSON\PackedArray</type></type><methodname>MongoDB\BSON\Serializable::bsonSerialize</methodname>
   <void />
  </methodsynopsis>
  <para>
   Called during serialization of the object to BSON. The method must return an
   <type>array</type>, <classname>stdClass</classname>, <classname>MongoDB\BSON\Document</classname>,
   or <classname>MongoDB\BSON\PackedArray</classname>.
  </para>
  <para>
   Root documents (e.g. a
   <interfacename>MongoDB\BSON\Serializable</interfacename> passed to
   <function>MongoDB\BSON\fromPHP</function>) will always be serialized as a
   BSON document. For field values, associative arrays and
   <classname>stdClass</classname> instances will be serialized as a BSON
   document and sequential arrays (i.e. sequential, numeric indexes starting at
   <literal>0</literal>) will be serialized as a BSON array.
  </para>
  <para>
   Users are encouraged to include an <property>_id</property> property (e.g. a
   <classname>MongoDB\BSON\ObjectId</classname> initialized in your constructor)
   when returning data for a BSON root document; otherwise, the driver or
   database will need to generate a
   <classname>MongoDB\BSON\ObjectId</classname> when inserting or upserting the
   document, respectively.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  &no.function.parameters;
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   An <type>array</type>, <classname>stdClass</classname>, <classname>MongoDB\BSON\Document</classname>,
   or <classname>MongoDB\BSON\PackedArray</classname> to be serialized as a BSON
   array or document.
  </para>
 </refsect1>

 <refsect1 role="changelog">
  &reftitle.changelog;
  <para>
   <informaltable>
    <tgroup cols="2">
     <thead>
      <row>
       <entry>&Version;</entry>
       <entry>&Description;</entry>
      </row>
     </thead>
     <tbody>
      <row>
       <entry>PECL mongodb 1.17.0</entry>
       <entry>
        <para>
         The return type was changed from <type class="union"><type>array</type><type>object</type></type>.
         Instead of <type>object</type>, the return type now specifies <classname>stdClass</classname>.
         Classes that implement this interface must be changed to no longer
         declare an <type>object</type> return type. As the return type is
         tentative, a deprecation warning is emitted on PHP 8.1 and newer.
        </para>
        <para>
         In addition to the changes above, the driver now also supports
         returning instances of <classname>MongoDB\BSON\Document</classname>
         and <classname>MongoDB\BSON\PackedArray</classname>. Please note that
         any <classname>MongoDB\BSON\PackedArray</classname> instances returned
         are silently converted to objects when stored as root documents. They
         are stored as arrays when used as an embedded field value.
        </para>
       </entry>
      </row>
     </tbody>
    </tgroup>
   </informaltable>
  </para>
 </refsect1>


 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title><function>MongoDB\BSON\Serializable::bsonSerialize</function> returning an associative array for root document</title>
   <programlisting role="php">
<![CDATA[
<?php

class MyDocument implements MongoDB\BSON\Serializable
{
    private $id;

    function __construct()
    {
        $this->id = new MongoDB\BSON\ObjectId;
    }

    function bsonSerialize(): array
    {
        return ['_id' => $this->id, 'foo' => 'bar'];
    }
}

$bson = MongoDB\BSON\fromPHP(new MyDocument);
echo MongoDB\BSON\toJSON($bson), "\n";

?>
]]>
   </programlisting>
   &example.outputs.similar;
   <screen>
<![CDATA[
{ "_id" : { "$oid" : "56cccdcada14d8755a58c591" }, "foo" : "bar" }
]]>
   </screen>
  </example>

  <example>
   <title><function>MongoDB\BSON\Serializable::bsonSerialize</function> returning a sequential array for root document</title>
   <programlisting role="php">
<![CDATA[
<?php

class MyArray implements MongoDB\BSON\Serializable
{
    function bsonSerialize(): array
    {
        return [1, 2, 3];
    }
}

$bson = MongoDB\BSON\fromPHP(new MyArray);
echo MongoDB\BSON\toJSON($bson), "\n";

?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
{ "0" : 1, "1" : 2, "2" : 3 }
]]>
   </screen>
  </example>

  <example>
   <title><function>MongoDB\BSON\Serializable::bsonSerialize</function> returning an associative array for document field</title>
   <programlisting role="php">
<![CDATA[
<?php

class MyDocument implements MongoDB\BSON\Serializable
{
    function bsonSerialize(): array
    {
        return ['foo' => 'bar'];
    }
}

$value = ['document' => new MyDocument];
$bson = MongoDB\BSON\fromPHP($value);
echo MongoDB\BSON\toJSON($bson), "\n";

?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
{ "document" : { "foo" : "bar" } }
]]>
   </screen>
  </example>

  <example>
   <title><function>MongoDB\BSON\Serializable::bsonSerialize</function> returning a sequential array for document field</title>
   <programlisting role="php">
<![CDATA[
<?php

class MyArray implements MongoDB\BSON\Serializable
{
    function bsonSerialize(): array
    {
        return [1, 2, 3];
    }
}

$value = ['array' => new MyArray];
$bson = MongoDB\BSON\fromPHP($value);
echo MongoDB\BSON\toJSON($bson), "\n";

?>
]]>
   </programlisting>
   &example.outputs;
   <screen>
<![CDATA[
{ "array" : [ 1, 2, 3 ] }
]]>
   </screen>
  </example>
 </refsect1>


 <refsect1 role="seealso">
  &reftitle.seealso;
  <simplelist>
   <member><function>MongoDB\BSON\Unserializable::bsonUnserialize</function></member>
   <member><interfacename>MongoDB\BSON\Persistable</interfacename></member>
   <member><xref linkend="mongodb.persistence"/></member>
  </simplelist>
 </refsect1>

</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
