Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Spatie\FlareClient\Truncation;
4
 
5
class TrimContextItemsStrategy extends AbstractTruncationStrategy
6
{
7
    /**
8
     * @return array<int, int>
9
     */
10
    public static function thresholds(): array
11
    {
12
        return [100, 50, 25, 10];
13
    }
14
 
15
    /**
16
     * @param array<int|string, mixed> $payload
17
     *
18
     * @return array<int|string, mixed>
19
     */
20
    public function execute(array $payload): array
21
    {
22
        foreach (static::thresholds() as $threshold) {
23
            if (! $this->reportTrimmer->needsToBeTrimmed($payload)) {
24
                break;
25
            }
26
 
27
            $payload['context'] = $this->iterateContextItems($payload['context'], $threshold);
28
        }
29
 
30
        return $payload;
31
    }
32
 
33
    /**
34
     * @param array<int|string, mixed> $contextItems
35
     * @param int $threshold
36
     *
37
     * @return array<int|string, mixed>
38
     */
39
    protected function iterateContextItems(array $contextItems, int $threshold): array
40
    {
41
        array_walk($contextItems, [$this, 'trimContextItems'], $threshold);
42
 
43
        return $contextItems;
44
    }
45
 
46
    protected function trimContextItems(mixed &$value, mixed $key, int $threshold): mixed
47
    {
48
        if (is_array($value)) {
49
            if (count($value) > $threshold) {
50
                $value = array_slice($value, $threshold * -1, $threshold);
51
            }
52
 
53
            array_walk($value, [$this, 'trimContextItems'], $threshold);
54
        }
55
 
56
        return $value;
57
    }
58
}