TrimContextItemsStrategy.php
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace Facade\FlareClient\Truncation;
class TrimContextItemsStrategy extends AbstractTruncationStrategy
{
    public static function thresholds()
    {
        return [100, 50, 25, 10];
    }
    public function execute(array $payload): array
    {
        foreach (static::thresholds() as $threshold) {
            if (! $this->reportTrimmer->needsToBeTrimmed($payload)) {
                break;
            }
            $payload['context'] = $this->iterateContextItems($payload['context'], $threshold);
        }
        return $payload;
    }
    protected function iterateContextItems(array $contextItems, int $threshold): array
    {
        array_walk($contextItems, [$this, 'trimContextItems'], $threshold);
        return $contextItems;
    }
    protected function trimContextItems(&$value, $key, int $threshold)
    {
        if (is_array($value)) {
            if (count($value) > $threshold) {
                $value = array_slice($value, $threshold * -1, $threshold);
            }
            array_walk($value, [$this, 'trimContextItems'], $threshold);
        }
        return $value;
    }
}