Response   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 0
dl 0
loc 131
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 13 5
A __toString() 0 10 3
B __get() 0 15 6
A getText() 0 4 1
A getYomi() 0 4 1
A getContext() 0 4 1
A getMode() 0 4 1
A getNumber() 0 4 1
A className() 0 4 1
1
<?php
2
/**
3
 * docomoから返答されたデータ
4
 * @author AIZAWA Hina <[email protected]>
5
 * @copyright 2015 by AIZAWA Hina <[email protected]>
6
 * @license https://github.com/fetus-hina/docomo-dialogue/blob/master/LICENSE MIT
7
 * @since 0.1.0
8
 */
9
10
namespace jp3cki\docomoDialogue;
11
12
use stdClass;
13
14
/**
15
 * docomoから返答されたデータ
16
 *
17
 * @property-read string $utt            ユーザへ返答するテキスト
18
 * @property-read string $yomi           音声合成にシステム返答を読ませるための出力
19
 * @property-read string $context        コンテキストID
20
 * @property-read string $mode           会話モード
21
 * @property-read int    $da             ユーザとシステムの対話に対してサーバが付与した番号
22
 */
23
class Response
24
{
25
    /** @internal */
26
    private $parameters = [
27
        'utt'       => null,
28
        'yomi'      => null,
29
        'context'   => null,
30
        'mode'      => null,
31
        'da'        => null,
32
    ];
33
34
    /**
35
     * コンストラクタ
36
     *
37
     * @param \stdClass $response サーバからの応答をJSONデコードしたもの
38
     */
39
    public function __construct(stdClass $response)
40
    {
41
        foreach (['utt', 'yomi', 'mode', 'context'] as $key) {
42
            if (isset($response->$key)) {
43
                $this->parameters[$key] = (string)$response->$key;
44
            }
45
        }
46
        foreach (['da'] as $key) {
47
            if (isset($response->$key)) {
48
                $this->parameters[$key] = (int)(string)$response->$key;
49
            }
50
        }
51
    }
52
53
    /**
54
     * マジックメソッド __toString
55
     *
56
     * デバッグ等で表示する際に中身がわかるように JSON で返すだけで、
57
     * 表現そのものに意味はないし依存してはならない
58
     *
59
     * @return string
60
     */
61
    public function __toString()
62
    {
63
        $data = [];
64
        foreach ($this->parameters as $k => $v) {
65
            if ($v !== null) {
66
                $data[$k] = $v;
67
            }
68
        }
69
        return json_encode($data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
70
    }
71
72
    /**
73
     * マジックメソッド __get
74
     *
75
     * @param   string  $key    プロパティ取得用のキー
76
     * @return  string          キーに対応する値
77
     */
78
    public function __get($key)
79
    {
80
        switch ($key) {
81
            case 'utt':
82
                return $this->getText();
83
            case 'yomi':
84
                return $this->getYomi();
85
            case 'context':
86
                return $this->getContext();
87
            case 'mode':
88
                return $this->getMode();
89
            case 'da':
90
                return $this->getNumber();
91
        }
92
    }
93
94
    /**
95
     * システムからの返答テキストを取得する
96
     *
97
     * @return string
98
     */
99
    public function getText()
100
    {
101
        return $this->parameters['utt'];
102
    }
103
104
    /**
105
     * 音声合成にシステム返答を読ませるための出力を取得する
106
     *
107
     * @return string
108
     */
109
    public function getYomi()
110
    {
111
        return $this->parameters['yomi'];
112
    }
113
114
    /**
115
     * コンテキストIDを取得する
116
     *
117
     * @return string
118
     */
119
    public function getContext()
120
    {
121
        return $this->parameters['context'];
122
    }
123
124
    /**
125
     * 対話のモードを取得する
126
     *
127
     * @return string
128
     */
129
    public function getMode()
130
    {
131
        return $this->parameters['mode'];
132
    }
133
134
    /**
135
     * ユーザとシステムの対話に対してサーバが付与した番号を取得する
136
     *
137
     * @return int
138
     */
139
    public function getNumber()
140
    {
141
        return $this->parameters['da'];
142
    }
143
144
    /**
145
     * クラス名(FQCN)を取得
146
     *
147
     * return string
148
     */
149
    public static function className()
150
    {
151
        return get_called_class();
152
    }
153
}
154