8000 Merge branch '3.0' into 3.1 · symfony/symfony-docs@7bc40da · GitHub
[go: up one dir, main page]

Skip to content

Commit 7bc40da

Browse files
committed
Merge branch '3.0' into 3.1
* 3.0: [#6925] Removing more instances of the deprecated getMock() Method "$this->getMock()" is depreciated 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. Update tags.rst Fixed indentation issues in alias_private article [Serializer] Document the encoders
2 parents f2e2914 + 49492b7 commit 7bc40da

File tree

12 files changed

+192
-27
lines changed

12 files changed

+192
-27
lines changed

components/serializer.rst

Lines 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

Lines 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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ We are now ready to write our first test::
9292

9393
private function getFrameworkForException($exception)
9494
{
95-
$matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
95+
$matcher = $this->createMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
9696
$matcher
9797
->expects($this->once())
9898
->method('match')
@@ -101,10 +101,10 @@ We are now ready to write our first test::
101101
$matcher
102102
->expects($this->once())
103103
->method('getContext')
104-
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
104+
->will($this->returnValue($this->createMock('Symfony\Component\Routing\RequestContext')))
105105
;
106-
$controllerResolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
107-
$argumentResolver = $this->getMock('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface');
106+
$controllerResolver = $this->createMock('Symfony\Component\HttpKernel\Controller\ControllerResolverInterface');
107+
$argumentResolver = $this->createMock('Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface');
108108

109109
return new Framework($matcher, $controllerResolver, $argumentResolver);
110110
}
@@ -149,7 +149,7 @@ Response::
149149

150150
public function testControllerResponse()
151151
{
152-
$matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
152+
$matcher = $this->createMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
153153
$matcher
154154
->expects($this->once())
155155
->method('match')
@@ -164,7 +164,7 @@ Response::
164164
$matcher
165165
->expects($this->once())
166166
->method('getContext')
167-
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RequestContext')))
167+
->will($this->returnValue($this->createMock('Symfony\Component\Routing\RequestContext')))
168168
;
169169
$controllerResolver = new ControllerResolver();
170170
$argumentResolver = new ArgumentResolver();

form/unit_testing.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ make sure the ``FormRegistry`` uses the created instance::
127127
protected function setUp()
128128
{
129129
// mock any dependencies
130-
$this->entityManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
130+
$this->entityManager = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')->getMock();
131131

132132
parent::setUp();
133133
}
@@ -178,7 +178,7 @@ allows you to return a list of extensions to register::
178178

179179
protected function getExtensions()
180180
{
181-
$this->validator = $this->getMock(
181+
$this->validator = $this->createMock(
182182
'Symfony\Component\Validator\Validator\ValidatorInterface'
183183
);
184184
$this->validator

forms.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ the correct values of a number of field options.
567567
guessed from the validation constraints (if ``Length`` or ``Range`` is used) or
568568
from the Doctrine metadata (via the field's length).
569569

570-
.. note::
570+
.. caution::
571571

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

reference/constraints/UserPassword.rst

Lines 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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ A service leveraging `APCu`_ (and APC for PHP < 5.5) is built-in.
210210
),
211211
));
212212
213-
Going Further with the Serializer Component
214-
-------------------------------------------
213+
Going Further with the Serializer
214+
---------------------------------
215215

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

224+
.. toctree::
225+
:maxdepth: 1
226+
:glob:
227+
228+
serializer/*
229+
224230
.. _`APCu`: https://github.com/krakjoe/apcu
225231
.. _`ApiPlatform`: https://github.com/api-platform/core
226232
.. _`JSON-LD`: http://json-ld.org

serializer/custom_encoders.rst

Lines 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

Lines 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 10000 +
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

Lines 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

service_container/tags.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Create a Compiler Pass
195195
~~~~~~~~~~~~~~~~~~~~~~
196196

197197
You can now use a :ref:`compiler pass <components-di-separate-compiler-passes>` to ask the
198-
container for any services with the ``acme_mailer.transport`` tag::
198+
container for any services with the ``app.mail_transport`` tag::
199199

200200
// src/AppBundle/DependencyInjection/Compiler/MailTransportPass.php
201201
namespace AppBundle\DependencyInjection\Compiler;

testing/database.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ it's easy to pass a mock object within a test::
7373
public function testCalculateTotalSalary()
7474
{
7575
// First, mock the object to be used in the test
76-
$employee = $this->getMock(Employee::class);
76+
$employee = $this->createMock(Employee::class);
7777
$employee->expects($this->once())
7878
->method('getSalary')
7979
->will($this->returnValue(1000));

0 commit comments

Comments
 (0)
0