1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tgallice\Wit; |
4
|
|
|
|
5
|
|
|
use Tgallice\Wit\Exception\InvalidStepException; |
6
|
|
|
use Tgallice\Wit\Model\Step; |
7
|
|
|
use Tgallice\Wit\Model\Step\Action; |
8
|
|
|
use Tgallice\Wit\Model\Step\Merge; |
9
|
|
|
use Tgallice\Wit\Model\Step\Message; |
10
|
|
|
use Tgallice\Wit\Model\Step\Stop; |
11
|
|
|
|
12
|
|
|
class StepFactory |
13
|
|
|
{ |
14
|
9 |
|
public static function create(array $step) |
15
|
|
|
{ |
16
|
9 |
|
$type = isset($step['type']) ? $step['type'] : null; |
17
|
|
|
|
18
|
|
|
switch ($type) { |
19
|
9 |
|
case Step::TYPE_ACTION: |
20
|
2 |
|
return self::createActionStep($step); |
21
|
9 |
|
case Step::TYPE_MESSAGE: |
22
|
2 |
|
return self::createMessageStep($step); |
23
|
9 |
|
case Step::TYPE_MERGE: |
24
|
4 |
|
return self::createMergeStep($step); |
25
|
8 |
|
case Step::TYPE_STOP: |
26
|
7 |
|
return self::createStopStep($step); |
27
|
1 |
|
default: |
28
|
1 |
|
throw new InvalidStepException('Invalid Step', $step); |
29
|
1 |
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $step |
34
|
|
|
* |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
9 |
|
private static function getEntitiesFromStepData(array $step = []) |
38
|
|
|
{ |
39
|
9 |
|
return isset($step['entities']) ? $step['entities'] : []; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $step |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
3 |
|
private static function getQuickRepliesFromStepData(array $step = []) |
48
|
|
|
{ |
49
|
3 |
|
return isset($step['quickreplies']) ? $step['quickreplies'] : []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $step |
54
|
|
|
* |
55
|
|
|
* @return Action |
56
|
|
|
*/ |
57
|
3 |
|
public static function createActionStep(array $step) |
58
|
|
|
{ |
59
|
3 |
|
return new Action($step['action'], $step['confidence'], self::getEntitiesFromStepData($step)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $step |
64
|
|
|
* |
65
|
|
|
* @return Merge |
66
|
|
|
*/ |
67
|
5 |
|
public static function createMergeStep(array $step) |
68
|
|
|
{ |
69
|
5 |
|
return new Merge(self::getEntitiesFromStepData($step), $step['confidence']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $step |
74
|
|
|
* |
75
|
|
|
* @return Message |
76
|
|
|
*/ |
77
|
3 |
|
public static function createMessageStep(array $step) |
78
|
|
|
{ |
79
|
3 |
|
return new Message($step['msg'], $step['confidence'], self::getEntitiesFromStepData($step), self::getQuickRepliesFromStepData($step)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param array $step |
84
|
|
|
* |
85
|
|
|
* @return Stop |
86
|
|
|
*/ |
87
|
8 |
|
public static function createStopStep(array $step) |
88
|
|
|
{ |
89
|
8 |
|
return new Stop($step['confidence']); |
90
|
|
|
} |
91
|
|
|
} |