1: <?php
2:
3: /*
4: * Copyright (c) 2015 @trashtoy
5: * https://github.com/trashtoy/
6: *
7: * Permission is hereby granted, free of charge, to any person obtaining a copy of
8: * this software and associated documentation files (the "Software"), to deal in
9: * the Software without restriction, including without limitation the rights to use,
10: * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
11: * Software, and to permit persons to whom the Software is furnished to do so,
12: * subject to the following conditions:
13: *
14: * The above copyright notice and this permission notice shall be included in all
15: * copies or substantial portions of the Software.
16: *
17: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23: */
24: /**
25: * PHP class file.
26: * @auhtor trashtoy
27: * @since 2.2.0
28: */
29: namespace Peach\Http\Body;
30:
31: use Peach\DF\Codec;
32: use Peach\Http\BodyRenderer;
33:
34: /**
35: * Codec オブジェクトの Adapter として機能する Renderer クラスです.
36: * このクラスの render メソッドは, インスタンス初期化時に指定した Codec オブジェクトの encode()
37: * メソッドを実行して値を文字列に変換します.
38: */
39: class CodecRenderer implements BodyRenderer
40: {
41: /**
42: * render() 内で値を文字列に変換するための Codec です.
43: *
44: * @var Codec
45: */
46: private $codec;
47:
48: /**
49: * 指定された Codec オブジェクトを使って render を行う CodecRenderer オブジェクトを構築します.
50: *
51: * @param Codec $codec 任意の値を文字列に変換する Codec
52: */
53: public function __construct(Codec $codec)
54: {
55: $this->codec = $codec;
56: }
57:
58: /**
59: * このオブジェクトに登録されている Codec を使用して引数の値を文字列に変換します.
60: *
61: * @param mixed $var 変換対象の値
62: * @return string 変換結果
63: */
64: public function render($var)
65: {
66: return $this->codec->encode($var);
67: }
68: }
69: