Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bba10f7

Browse filesBrowse files
committed
Merge branch '2.7' into 2.8
* 2.7: [#6925] Removing more instances of the deprecated getMock() Minor language tweaks removing cookbook entries updating links Moving files into the new structure [#6649] Changing a note to a caution - this *is* a gotcha fix typo Typo in the class name. Fixed indentation issues in alias_private article [Serializer] Document the encoders
2 parents db1bd1e + c52839a commit bba10f7
Copy full SHA for bba10f7

File tree

Expand file treeCollapse file tree

11 files changed

+189
-24
lines changed
Filter options
Expand file treeCollapse file tree

11 files changed

+189
-24
lines changed

‎components/serializer.rst

Copy file name to clipboardExpand all lines: components/serializer.rst
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Usage
4444

4545
Using the Serializer component is really simple. You just need to set up
4646
the :class:`Symfony\\Component\\Serializer\\Serializer` specifying
47-
which Encoders and Normalizer are going to be available::
47+
which encoders and normalizer are going to be available::
4848

4949
use Symfony\Component\Serializer\Serializer;
5050
use Symfony\Component\Serializer\Encoder\XmlEncoder;
@@ -57,10 +57,9 @@ which Encoders and Normalizer are going to be available::
5757
$serializer = new Serializer($normalizers, $encoders);
5858

5959
The preferred normalizer is the
60-
:class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`, but other
61-
normalizers are available.
62-
To read more about them, refer to the `Normalizers`_ section of this page. All
63-
the examples shown below use the ``ObjectNormalizer``.
60+
:class:`Symfony\\Component\\Serializer\\Normalizer\\ObjectNormalizer`,
61+
but other normalizers are available. All the examples shown below use
62+
the ``ObjectNormalizer``.
6463

6564
Serializing an Object
6665
---------------------

‎components/validator/resources.rst

Copy file name to clipboardExpand all lines: components/validator/resources.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ In this example, the validation metadata is retrieved executing the
4242
{
4343
protected $name;
4444

45-
public static function loadValidatorMatadata(ClassMetadata $metadata)
45+
public static function loadValidatorMetadata(ClassMetadata $metadata)
4646
{
4747
$metadata->addPropertyConstraint('name', new Assert\NotBlank());
4848
$metadata->addPropertyConstraint('name', new Asert\Length(array(

‎create_framework/unit_testing.rst

Copy file name to clipboardExpand all lines: create_framework/unit_testing.rst
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ We are now ready to write our first test::
8989

9090
private function getFrameworkForException($exception)
9191
{
92-
$matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
92+
$matcher = $this->createMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
9393
$matcher
9494
->expects($this->once())
9595
->method('match')
@@ -98,9 +98,9 @@ We are now ready to write our first test::
9898
$matcher
9999
->expects($this->once())
100100
->method('getContext')
101-
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
101+
->will($this->returnValue($this->createMock('Symfony\Component\Routing\RequestContext')))
102102
;
103-
$resolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
103+
$resolver = $this->createMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
104104

105105
return new Framework($matcher, $resolver);
106106
}
@@ -144,7 +144,7 @@ Response::
144144

145145
public function testControllerResponse()
146146
{
147-
$matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
147+
$matcher = $this->createMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
148148
$matcher
149149
->expects($this->once())
150150
->method('match')
@@ -159,7 +159,7 @@ Response::
159159
$matcher
160160
->expects($this->once())
161161
->method('getContext')
162-
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
162+
->will($this->returnValue($this->createMock('Symfony\Component\Routing\RequestContext')))
163163
;
164164
$resolver = new ControllerResolver();
165165

‎form/unit_testing.rst

Copy file name to clipboardExpand all lines: form/unit_testing.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ allows you to return a list of extensions to register::
183183

184184
protected function getExtensions()
185185
{
186-
$this->validator = $this->getMock(
186+
$this->validator = $this->createMock(
187187
'Symfony\Component\Validator\Validator\ValidatorInterface'
188188
);
189189
$this->validator

‎forms.rst

Copy file name to clipboardExpand all lines: forms.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ the correct values of a number of field options.
586586
guessed from the validation constraints (if ``Length`` or ``Range`` is used) or
587587
from the Doctrine metadata (via the field's length).
588588

589-
.. note::
589+
.. caution::
590590

591591
These field options are *only* guessed if you're using Symfony to guess
592592
the field type (i.e. omit or pass ``null`` as the second argument to ``add()``).

‎reference/constraints/UserPassword.rst

Copy file name to clipboardExpand all lines: reference/constraints/UserPassword.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ password, but needs to enter their old password for security.
2424
Basic Usage
2525
-----------
2626

27-
Suppose you have a ``PasswordChange`` class, that's used in a form where
27+
Suppose you have a ``ChangePassword`` class, that's used in a form where
2828
the user can change their password by entering their old password and a
2929
new password. This constraint will validate that the old password matches
3030
the user's current password:

‎serializer.rst

Copy file name to clipboardExpand all lines: serializer.rst
+8-2Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ A service leveraging `APCu`_ (and APC for PHP < 5.5) is built-in.
226226
),
227227
));
228228
229-
Going Further with the Serializer Component
230-
-------------------------------------------
229+
Going Further with the Serializer
230+
---------------------------------
231231

232232
`ApiPlatform`_ provides an API system supporting `JSON-LD`_ and `Hydra Core Vocabulary`_
233233
hypermedia formats. It is built on top of the Symfony Framework and its Serializer
@@ -237,6 +237,12 @@ and a caching system.
237237
If you want to leverage the full power of the Symfony Serializer component,
238238
take a look at how this bundle works.
239239

240+
.. toctree::
241+
:maxdepth: 1
242+
:glob:
243+
244+
serializer/*
245+
240246
.. _`APCu`: https://github.com/krakjoe/apcu
241247
.. _`ApiPlatform`: https://github.com/api-platform/core
242248
.. _`JSON-LD`: http://json-ld.org

‎serializer/custom_encoders.rst

Copy file name to clipboard
+97Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
.. index::
2+
single: Serializer; Custom encoders
3+
4+
How to Create your Custom Encoder
5+
=================================
6+
7+
The :doc:`Serializer Component </components/serializer>` uses Normalizers
8+
to transform any data to an array. Then, by leveraging *Encoders*, that data can
9+
be convereted into any data-structure (e.g. JSON).
10+
11+
The Component provides several built-in encoders that are described
12+
:doc:`in their own section </serializer/encoders>` but you may want
13+
to use another structure that's not supported.
14+
15+
Creating a new encoder
16+
----------------------
17+
18+
Imagine you want to serialize and deserialize Yaml. For that you'll have to
19+
create your own encoder that uses the
20+
:doc:`Yaml Component </components/yaml>`::
21+
22+
namespace AppBundle\Encoder;
23+
24+
use Symfony\Component\Serializer\Encoder\DecoderInterface;
25+
use Symfony\Component\Serializer\Encoder\EncoderInterface;
26+
use Symfony\Component\Yaml\Yaml;
27+
28+
class YamlEncoder implements EncoderInterface, DecoderInterface
29+
{
30+
public function encode($data, $format, array $context = array())
31+
{
32+
return Yaml::dump($data);
33+
}
34+
35+
public function supportsEncoding($format)
36+
{
37+
return 'yaml' === $format;
38+
}
39+
40+
public function decode($data, $format, array $context = array())
41+
{
42+
return Yaml::parse($data);
43+
}
44+
45+
public function supportsDecoding($format)
46+
{
47+
return 'yaml' === $format;
48+
}
49+
}
50+
51+
Registering it in your app
52+
--------------------------
53+
54+
If you use the Symfony Framework. then you probably want to register this encoder
55+
as a service in your app. Then, you only need to tag it with ``serializer.encoder``
56+
to inject your custom encoder into the Serializer.
57+
58+
.. configuration-block::
59+
60+
.. code-block:: yaml
61+
62+
# app/config/services.yml
63+
services:
64+
app.encoder.yaml:
65+
class: AppBundle\Encoder\YamlEncoder
66+
tags:
67+
- { name: serializer.encoder }
68+
69+
.. code-block:: xml
70+
71+
<!-- app/config/services.xml -->
72+
<?xml version="1.0" encoding="UTF-8" ?>
73+
<container xmlns="http://symfony.com/schema/dic/services"
74+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
75+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
76+
77+
<services>
78+
<service id="app.encoder.yaml" class="AppBundle\Encoder\YamlEncoder">
79+
<tag name="serializer.encoder" />
80+
</service>
81+
</services>
82+
</container>
83+
84+
.. code-block:: php
85+
86+
// app/config/services.php
87+
$container
88+
->register(
89+
'app.encoder.yaml',
90+
'AppBundle\Encoder\YamlEncoder'
91+
)
92+
->addTag('serializer.encoder')
93+
;
94+
95+
Now you'll be able to serialize and deserialize Yaml!
96+
97+
.. _tracker: https://github.com/symfony/symfony/issues

‎serializer/encoders.rst

Copy file name to clipboard
+63Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.. index::
2+
single: Serializer, Encoders
3+
4+
Encoders
5+
========
6+
7+
Encoders basically turn **arrays** into **formats** and vice versa.
8+
They implement
9+
:class:`Symfony\\Component\\Serializer\\Encoder\\EncoderInterface` for
10+
encoding (array to format) and
11+
:class:`Symfony\\Component\\Serializer\\Encoder\\DecoderInterface` for
12+
decoding (format to array).
13+
14+
You can add new encoders to a Serializer instance by using its second constructor argument::
15+
16+
use Symfony\Component\Serializer\Serializer;
17+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
18+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
19+
20+
$encoders = array(new XmlEncoder(), new JsonEncoder());
21+
$serializer = new Serializer(array(), $encoders);
22+
23+
Built-in Encoders
24+
-----------------
25+
26+
Two encoders are used in the example above:
27+
28+
* :class:`Symfony\\Component\\Serializer\\Encoder\\XmlEncoder` to encode/decode XML
29+
* :class:`Symfony\\Component\\Serializer\\Encoder\\JsonEncoder` to encode/decode JSON
30+
31+
The ``XmlEncoder``
32+
~~~~~~~~~~~~~~~~~~
33+
34+
This encoder transforms arrays into XML and vice versa.
35+
36+
For example, take an object normalized as following::
37+
38+
array('foo' => array(1, 2), 'bar' => true);
39+
40+
The ``XmlEncoder`` will encode this object like that::
41+
42+
<?xml version="1.0"?>
43+
<response>
44+
<foo>1</foo>
45+
<foo>2</foo>
46+
<bar>1</bar>
47+
</response>
48+
49+
Be aware that this encoder will consider keys beginning with ``@`` as attributes::
50+
51+
$encoder = new XmlEncoder();
52+
$encoder->encode(array('foo' => array('@bar' => 'value')));
53+
// will return:
54+
// <?xml version="1.0"?>
55+
// <response>
56+
// <foo bar="value" />
57+
// </response>
58+
59+
The ``JsonEncoder``
60+
~~~~~~~~~~~~~~~~~~~
61+
62+
The ``JsonEncoder`` is much simpler and is based on the PHP
63+
:phpfunction:`json_encode` and :phpfunction:`json_decode` functions.

‎service_container/alias_private.rst

Copy file name to clipboardExpand all lines: service_container/alias_private.rst
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ to be *not* public (i.e. private):
3030
.. code-block:: yaml
3131
3232
services:
33-
foo:
34-
class: Example\Foo
35-
public: false
33+
foo:
34+
class: Example\Foo
35+
public: false
3636
3737
.. code-block:: xml
3838
@@ -87,11 +87,11 @@ services.
8787
.. code-block:: yaml
8888
8989
services:
90-
app.phpmailer:
91-
class: AppBundle\Mail\PhpMailer
90+
app.phpmailer:
91+
class: AppBundle\Mail\PhpMailer
9292
93-
app.mailer:
94-
alias: app.phpmailer
93+
app.mailer:
94+
alias: app.phpmailer
9595
9696
.. code-block:: xml
9797

‎testing/database.rst

Copy file name to clipboardExpand all lines: testing/database.rst
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ it's easy to pass a mock object within a test::
6666
public function testCalculateTotalSalary()
6767
{
6868
// First, mock the object to be used in the test
69-
$employee = $this->getMock('\AppBundle\Entity\Employee');
69+
$employee = $this->createMock('\AppBundle\Entity\Employee');
7070
$employee->expects($this->once())
7171
->method('getSalary')
7272
->will($this->returnValue(1000));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.