Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ContentLocales
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
20
100.00% covered (success)
100.00%
1 / 1
 used
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 field
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
15
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Panel;
6
7/** Whether a node field tree contains content controlled by an editing locale. */
8final class ContentLocales
9{
10    private const array SWITCHABLE = ['text', 'textarea', 'iframe', 'youtube', 'element'];
11
12    /** @param list<array<string, mixed>> $fields */
13    public static function used(array $fields, int $locales): bool
14    {
15        if ($locales < 2) {
16            return false;
17        }
18
19        foreach ($fields as $field) {
20            if (is_array($field) && self::field($field)) {
21                return true;
22            }
23        }
24
25        return false;
26    }
27
28    /** @param array<string, mixed> $field */
29    private static function field(array $field): bool
30    {
31        if ($field['hidden'] ?? false) {
32            return false;
33        }
34
35        $control = is_array($field['control'] ?? null) ? $field['control'] : [];
36        $name = (string) ($control['name'] ?? '');
37        $translate = (bool) ($field['translate'] ?? false);
38
39        if ($translate && in_array($name, self::SWITCHABLE, true)) {
40            return true;
41        }
42
43        if ($translate && $name === 'blocks' && ($field['translateMode'] ?? null) === 'asymmetric') {
44            return true;
45        }
46
47        $types = match ($name) {
48            'entries' => $control['props']['entryTypes'] ?? [],
49            'blocks' => $control['props']['blockTypes'] ?? [],
50            default => [],
51        };
52
53        foreach (is_array($types) ? $types : [] as $type) {
54            if (is_array($type) && self::used((array) ($type['fields'] ?? []), 2)) {
55                return true;
56            }
57        }
58
59        return false;
60    }
61}