1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FondBot\Conversation; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use FondBot\Conversation\Activators\Exact; |
9
|
|
|
use FondBot\Conversation\Activators\Regex; |
10
|
|
|
use FondBot\Conversation\Activators\InArray; |
11
|
|
|
use FondBot\Conversation\Activators\Payload; |
12
|
|
|
use FondBot\Contracts\Conversation\Activator; |
|
|
|
|
13
|
|
|
use FondBot\Conversation\Activators\Contains; |
14
|
|
|
use FondBot\Conversation\Activators\Attachment; |
15
|
|
|
|
16
|
|
|
class ActivatorParser |
17
|
|
|
{ |
18
|
|
|
private static $activators = [ |
19
|
|
|
'contains' => Contains::class, |
20
|
|
|
'exact' => Exact::class, |
21
|
|
|
'in_array' => InArray::class, |
22
|
|
|
'regex' => Regex::class, |
23
|
|
|
'attachment' => Attachment::class, |
24
|
|
|
'payload' => Payload::class, |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
private static $arrayActivators = [ |
28
|
|
|
'in_array', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $data |
33
|
|
|
* |
34
|
|
|
* @return Activator[] |
35
|
|
|
*/ |
36
|
2 |
|
public static function parse(array $data): array |
37
|
|
|
{ |
38
|
2 |
|
$result = []; |
39
|
|
|
|
40
|
2 |
|
foreach ($data as $key => $activator) { |
41
|
2 |
|
if ($activator instanceof Activator) { |
42
|
1 |
|
$result[] = $activator; |
43
|
|
|
|
44
|
1 |
|
continue; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
[$name, $parameters] = explode(':', $activator, 2); |
|
|
|
|
48
|
|
|
|
49
|
2 |
|
$parameters = collect(str_getcsv($parameters)); |
50
|
|
|
|
51
|
2 |
|
if (in_array($name, static::$arrayActivators, true)) { |
|
|
|
|
52
|
1 |
|
$value = $parameters; |
53
|
|
|
} else { |
54
|
2 |
|
$value = $parameters->first(); |
55
|
2 |
|
$parameters = $parameters->slice(1)->values()->transform(function ($item) { |
56
|
1 |
|
if ($item === 'true') { |
57
|
1 |
|
return true; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($item === 'false') { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $item; |
65
|
2 |
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (isset(static::$activators[$name])) { |
|
|
|
|
69
|
|
|
/** @var Activator $activator */ |
70
|
|
|
$activator = new static::$activators[$name]($value, ...$parameters); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$result[] = $activator; |
73
|
|
|
|
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
throw new InvalidArgumentException('Activator `'.$name.'` does not exist.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $result; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: