forked from yiisoft/yii2-httpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonFormatter.php
37 lines (32 loc) · 885 Bytes
/
JsonFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\httpclient;
use yii\base\Object;
use yii\helpers\Json;
/**
* JsonFormatter formats HTTP message as JSON.
*
* @author Paul Klimov <[email protected]>
* @since 2.0
*/
class JsonFormatter extends Object implements FormatterInterface
{
/**
* @var int the encoding options.For more details please refer to
* <http://www.php.net/manual/en/function.json-encode.php>.
*/
public $encodeOptions = 0;
/**
* @inheritdoc
*/
public function format(Request $request)
{
$request->getHeaders()->set('Content-Type', 'application/json; charset=UTF-8');
$request->setContent(Json::encode($request->getData(), $this->encodeOptions));
return $request;
}
}