1: <?php
2: /*
3: * Copyright (c) 2015 @trashtoy
4: * https://github.com/trashtoy/
5: *
6: * Permission is hereby granted, free of charge, to any person obtaining a copy of
7: * this software and associated documentation files (the "Software"), to deal in
8: * the Software without restriction, including without limitation the rights to use,
9: * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10: * Software, and to permit persons to whom the Software is furnished to do so,
11: * subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included in all
14: * copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19: * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20: * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21: * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22: */
23: /**
24: * PHP class file.
25: * @auhtor trashtoy
26: * @since 2.2.0
27: */
28: namespace Peach\Http\Header;
29:
30: use Peach\Http\MultiHeaderField;
31: use Peach\Util\ArrayMap;
32:
33: /**
34: * Set-Cookie ヘッダーを表現するクラスです.
35: */
36: class SetCookie implements MultiHeaderField
37: {
38: /**
39: * 各 cookie のキーと, そのキーに相当する CookieItem オブジェクトをマッピングする ArrayMap です.
40: *
41: * @var ArrayMap
42: */
43: private $items;
44:
45: /**
46: * 新しい SetCookie オブジェクトを構築します.
47: * 引数に cookie のキー, 値, 属性を指定することで, 初期化と同時に
48: * 1 個目の cookie を追加することができます.
49: * 引数を省略した場合は cookie を何も持たない状態で初期化されます.
50: *
51: * @param string $name cookie のキー
52: * @param string $value cookie の値
53: * @param CookieOptions $options 各種属性
54: */
55: public function __construct($name = null, $value = null, CookieOptions $options = null)
56: {
57: $this->items = new ArrayMap();
58: if ($name !== null) {
59: $this->setItem($name, $value, $options);
60: }
61: }
62:
63: /**
64: * 指定されたキーと値の cookie を持つ新しい Set-Cookie ヘッダーを追加します.
65: *
66: * @param string $name cookie のキー
67: * @param string $value cookie の値
68: * @param CookieOptions $options 各種属性
69: */
70: public function setItem($name, $value, CookieOptions $options = null)
71: {
72: $item = new CookieItem($name, $value, $options);
73: $this->items->put($name, $item);
74: }
75:
76: /**
77: * すべての Set-Cookie ヘッダーの値を配列で返します.
78: *
79: * @return array すべての Set-Cookie ヘッダー値の配列
80: */
81: public function format()
82: {
83: $result = array();
84: foreach ($this->items as $item) {
85: $result[] = $item->format();
86: }
87: return $result;
88: }
89:
90: /**
91: * 文字列 "set-cookie" を返します.
92: *
93: * @return string ヘッダー名 "set-cookie"
94: */
95: public function getName()
96: {
97: return "set-cookie";
98: }
99:
100: /**
101: * このオブジェクトにセットされている Set-Cookie ヘッダーの一覧を
102: * CookieItem オブジェクトの配列として返します.
103: *
104: * @return CookieItem[] CookieItem オブジェクトの配列
105: */
106: public function getValues()
107: {
108: return array_values($this->items->asArray());
109: }
110: }
111: