|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FondBot\Conversation; |
|
6
|
|
|
|
|
7
|
|
|
use FondBot\Helpers\Str; |
|
8
|
|
|
use FondBot\Contracts\Filesystem\Filesystem; |
|
9
|
|
|
|
|
10
|
|
|
class ConversationCreator |
|
11
|
|
|
{ |
|
12
|
|
|
private $filesystem; |
|
13
|
|
|
|
|
14
|
|
|
public function __construct(Filesystem $filesystem) |
|
15
|
|
|
{ |
|
16
|
|
|
$this->filesystem = $filesystem; |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create new intent. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $directory |
|
23
|
|
|
* @param string $namespace |
|
24
|
|
|
* @param string $name |
|
25
|
|
|
*/ |
|
26
|
|
|
public function createIntent(string $directory, string $namespace, string $name): void |
|
27
|
|
|
{ |
|
28
|
|
|
$contents = $this->filesystem->read(__DIR__.'/../../resources/stubs/Intent.stub'); |
|
29
|
|
|
|
|
30
|
|
|
$className = $this->className($name, 'Intent'); |
|
31
|
|
|
|
|
32
|
|
|
// Replace stub placeholders |
|
33
|
|
|
$this->replacePlaceholder($contents, 'namespace', $this->namespace($namespace)); |
|
34
|
|
|
$this->replacePlaceholder($contents, 'className', $className); |
|
35
|
|
|
$this->replacePlaceholder($contents, 'name', $this->formatName($name)); |
|
36
|
|
|
|
|
37
|
|
|
$path = $directory.'/'.$this->filename($className); |
|
38
|
|
|
|
|
39
|
|
|
$this->filesystem->write($path, $contents); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Create new interaction. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $directory |
|
46
|
|
|
* @param string $namespace |
|
47
|
|
|
* @param string $name |
|
48
|
|
|
*/ |
|
49
|
|
|
public function createInteraction(string $directory, string $namespace, string $name): void |
|
50
|
|
|
{ |
|
51
|
|
|
$contents = $this->filesystem->read(__DIR__.'/../../resources/stubs/Interaction.stub'); |
|
52
|
|
|
|
|
53
|
|
|
$className = $this->className($name, 'Interaction'); |
|
54
|
|
|
|
|
55
|
|
|
// Replace stub placeholders |
|
56
|
|
|
$this->replacePlaceholder($contents, 'namespace', $this->namespace($namespace, 'Interactions')); |
|
57
|
|
|
$this->replacePlaceholder($contents, 'className', $className); |
|
58
|
|
|
|
|
59
|
|
|
$path = $directory.'/Interactions/'.$this->filename($className); |
|
60
|
|
|
|
|
61
|
|
|
$this->filesystem->write($path, $contents); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Replace placeholder. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $input |
|
68
|
|
|
* @param string $key |
|
69
|
|
|
* @param string $value |
|
70
|
|
|
*/ |
|
71
|
|
|
private function replacePlaceholder(string &$input, string $key, string $value): void |
|
72
|
|
|
{ |
|
73
|
|
|
$key = mb_strtoupper($key); |
|
74
|
|
|
$input = str_replace('___'.$key.'___', $value, $input); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get filename. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
private function filename(string $name): string |
|
85
|
|
|
{ |
|
86
|
|
|
return $name.'.php'; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Get formatted name. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $name |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
private function formatName(string $name): string |
|
97
|
|
|
{ |
|
98
|
|
|
return mb_strtolower(trim($name)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get formatted namespace. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $value |
|
105
|
|
|
* |
|
106
|
|
|
* @param string|null $postfix |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
private function namespace(string $value, string $postfix = null): string |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
if (Str::endsWith($value, '\\')) { |
|
113
|
|
|
$value = mb_substr($value, 0, -1); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
if ($postfix !== null) { |
|
|
|
|
|
|
117
|
|
|
$value .= '\\'.$postfix; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $value; |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Get name of class. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $name |
|
127
|
|
|
* @param string $postfix |
|
128
|
|
|
* |
|
129
|
|
|
* @return string |
|
130
|
|
|
*/ |
|
131
|
|
|
private function className(string $name, string $postfix): string |
|
132
|
|
|
{ |
|
133
|
|
|
$name = trim($name); |
|
134
|
|
|
if (!Str::endsWith($name, $postfix)) { |
|
135
|
|
|
$name .= $postfix; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $name; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|