Files
doc-fr/reference/mongodb/bson/serializable/bsonserialize.xml
T
Yannick Torres dcea0054b9 Sync with EN
git-svn-id: https://svn.php.net/repository/phpdoc/fr/trunk@351109 c90b9560-bf6c-de11-be94-00142212c4b1
2020-10-31 17:09:33 +00:00

211 lines
5.4 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 53242ee6628dc1ae6989fe002231fddfd8f005c6 Maintainer: yannick Status: ready -->
<!-- Reviewed: no -->
<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>Fournit un tableau ou un document à sérialiser en tant que BSON</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>abstract</modifier> <modifier>public</modifier> <type class="union"><type>array</type><type>object</type></type><methodname>MongoDB\BSON\Serializable::bsonSerialize</methodname>
<void />
</methodsynopsis>
<para>
Appelé pendant la sérialisation de l'objet à BSON. La méthode doit retourner
un &array; ou <classname>stdClass</classname>.
</para>
<para>
Les documents racine (par exemple, un
<interfacename>MongoDB\BSON\Serializable</interfacename> passé à
<function>MongoDB\BSON\fromPHP</function>) seront toujours sérialisés en tant
que document BSON. Pour les valeurs de champ, les tableaux associatifs et les
instances <classname>stdClass</classname> seront sérialisés en tant que
document BSON et les tableaux séquentiels (c'est-à-dire les index numériques
séquentiels commençant à <literal>0</literal>) seront sérialisés en tant que
tableau BSON.
</para>
<para>
Les utilisateurs sont encouragés à inclure une propriété <property>_id</property>
(par exemple un <classname>MongoDB\BSON\ObjectId</classname> initialisé dans
votre constructeur) lors du renvoi de données pour un document racine BSON;
sinon, le pilote ou la base de données devra générer un
<classname>MongoDB\BSON\ObjectId</classname> lors de l'insertion ou de la
upserting du document, respectivement.
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
&no.function.parameters;
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
&array; ou <classname>stdClass</classname> à sérialiser en tant que tableau ou document BSON.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoDB\BSON\Serializable::bsonSerialize</function> renvoyant un tableau associatif pour le document racine</title>
<programlisting role="php">
<![CDATA[
<?php
class MyDocument implements MongoDB\BSON\Serializable
{
private $id;
function __construct()
{
$this->id = new MongoDB\BSON\ObjectId;
}
function bsonSerialize()
{
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> renvoyant un tableau séquentiel pour le document racine</title>
<programlisting role="php">
<![CDATA[
<?php
class MyArray implements MongoDB\BSON\Serializable
{
function bsonSerialize()
{
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> renvoyant un tableau associatif pour le champ de document</title>
<programlisting role="php">
<![CDATA[
<?php
class MyDocument implements MongoDB\BSON\Serializable
{
function bsonSerialize()
{
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> renvoyant un tableau séquentiel pour le champ de document</title>
<programlisting role="php">
<![CDATA[
<?php
class MyArray implements MongoDB\BSON\Serializable
{
function bsonSerialize()
{
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
-->