Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
IsSyntaxAware
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
3 / 3
7
100.00% covered (success)
100.00%
1 / 1
 syntaxes
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
 getSyntaxes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultSyntax
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Field\Capability;
6
7use Cosray\Exception\RuntimeException;
8use Cosray\Field\CodeSyntaxes;
9
10trait IsSyntaxAware
11{
12    /** @var list<string> */
13    protected array $syntaxes = [CodeSyntaxes::DEFAULT];
14
15    /**
16     * Aliases become the syntax they stand for, so the panel and stored
17     * values only ever see the keys. A syntax the panel's editor lacks is a
18     * schema error; it would otherwise show up as plain text.
19     *
20     * @param array<string> $syntaxes
21     */
22    public function syntaxes(array $syntaxes): void
23    {
24        $values = [];
25
26        foreach ($syntaxes as $syntax) {
27            if (trim($syntax) === '') {
28                continue;
29            }
30
31            $key = CodeSyntaxes::resolve($syntax) ?? throw new RuntimeException(
32                "The field \"{$this->name}\" offers the syntax \"{$syntax}\", which the panel lacks. "
33                    . 'Supported: '
34                    . implode(', ', CodeSyntaxes::KEYS)
35                    . '.',
36            );
37
38            if (!in_array($key, $values, true)) {
39                $values[] = $key;
40            }
41        }
42
43        $this->syntaxes = $values ?: [CodeSyntaxes::DEFAULT];
44    }
45
46    /** @return list<string> */
47    public function getSyntaxes(): array
48    {
49        return $this->syntaxes;
50    }
51
52    public function getDefaultSyntax(): string
53    {
54        return $this->syntaxes[0] ?? CodeSyntaxes::DEFAULT;
55    }
56}