|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Adventure\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use App\Adventure\Location; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* The Unpacker class is responsible for unpacking location descriptions, |
|
9
|
|
|
* that includes descriptions of visible connections and visible items in the location. |
|
10
|
|
|
*/ |
|
11
|
|
|
class Unpacker |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Unpacks the location by showing the detailed description, |
|
15
|
|
|
* visible connections and visible items in the location. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Location $location The location to unpack. |
|
18
|
|
|
* @return string The unpacked location descriptions. |
|
19
|
|
|
*/ |
|
20
|
3 |
|
public static function unpackLocationDescriptions(Location $location): string |
|
21
|
|
|
{ |
|
22
|
3 |
|
$locationText = $location->getLocationDetails() . "\n"; |
|
23
|
3 |
|
$locationText .= self::unpackVisibleConnectionsToLocation($location); |
|
24
|
3 |
|
$locationText .= self::unpackVisibleItemsInLocation($location); |
|
25
|
|
|
|
|
26
|
3 |
|
return $locationText; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Unpacks the visible connections to the location. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Location $location The location to unpack connections from. |
|
33
|
|
|
* @return string The unpacked connection descriptions. |
|
34
|
|
|
*/ |
|
35
|
3 |
|
public static function unpackVisibleConnectionsToLocation(Location $location): string |
|
36
|
|
|
{ |
|
37
|
3 |
|
$connectedLocations = $location->getConnectedLocations(); |
|
38
|
|
|
|
|
39
|
3 |
|
$connectionDescriptions = ""; |
|
40
|
3 |
|
foreach ($connectedLocations as $direction => $location) { |
|
41
|
|
|
$connectionDescriptions .= "- To the {$direction}, {$location->getDescription()}\n"; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
return $connectionDescriptions; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Unpacks the visible items in the location. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Location $location The location to unpack items from. |
|
51
|
|
|
* @return string The unpacked item descriptions. |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public static function unpackVisibleItemsInLocation(Location $location): string |
|
54
|
|
|
{ |
|
55
|
3 |
|
$itemsInLocation = $location->getItems(); |
|
56
|
|
|
|
|
57
|
3 |
|
$itemDescriptions = ""; |
|
58
|
3 |
|
foreach ($itemsInLocation as $item) { |
|
59
|
|
|
$itemDescription = self::encloseWordInBrackets($item->getDescription(), $item->getName()); |
|
60
|
|
|
$itemDescriptions .= "- {$itemDescription}\n"; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
3 |
|
return $itemDescriptions; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Add brackets around a specific word in a given text. |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $text The input text. |
|
70
|
|
|
* @param string $word The word to be enclosed in brackets. |
|
71
|
|
|
* @return string|null The modified text with the word enclosed by brackets. |
|
72
|
|
|
*/ |
|
73
|
|
|
private static function encloseWordInBrackets(string $text, string $word): ?string |
|
74
|
|
|
{ |
|
75
|
|
|
// Escape any special characters in the word |
|
76
|
|
|
$escapedWord = preg_quote($word); |
|
77
|
|
|
|
|
78
|
|
|
// Create the regex pattern to search for the word |
|
79
|
|
|
$pattern = '/\b' . $escapedWord . '\b/i'; |
|
80
|
|
|
|
|
81
|
|
|
// Add brackets around the word |
|
82
|
|
|
$modifiedText = preg_replace($pattern, '[$0]', $text); |
|
83
|
|
|
|
|
84
|
|
|
return $modifiedText; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|