Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1294 lars 1
<?php
2
 
3
namespace Spatie\Ignition\Solutions\OpenAi;
4
 
5
class OpenAiSolutionResponse
6
{
7
    protected string $rawText;
8
 
9
    public function __construct(string $rawText)
10
    {
11
        $this->rawText = trim($rawText);
12
    }
13
 
14
    public function description(): string
15
    {
16
        return $this->between('FIX', 'ENDFIX', $this->rawText);
17
    }
18
 
19
    public function links(): array
20
    {
21
        $textLinks = $this->between('LINKS', 'ENDLINKS', $this->rawText);
22
 
23
        $textLinks = explode(PHP_EOL, $textLinks);
24
 
25
        $textLinks = array_map(function ($textLink) {
26
            return json_decode($textLink, true);
27
        }, $textLinks);
28
 
29
        array_filter($textLinks);
30
 
31
        $links = [];
32
        foreach ($textLinks as $textLink) {
33
            $links[$textLink['title']] = $textLink['url'];
34
        }
35
 
36
        return $links;
37
    }
38
 
39
    protected function between(string $start, string $end, string $text): string
40
    {
41
        $startPosition = strpos($text, $start);
42
        if ($startPosition === false) {
43
            return "";
44
        }
45
 
46
        $startPosition += strlen($start);
47
        $endPosition = strpos($text, $end, $startPosition);
48
 
49
        if ($endPosition === false) {
50
            return "";
51
        }
52
 
53
        return trim(substr($text, $startPosition, $endPosition - $startPosition));
54
    }
55
}