Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
IsToolsAware
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 tools
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 getTools
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Field\Capability;
6
7use Cosray\Schema\Tool;
8
9/** Requires the using field's `$owner` for the project-level fallback. */
10trait IsToolsAware
11{
12    /** @var list<Tool> */
13    protected array $tools = [];
14
15    public function tools(Tool ...$tools): void
16    {
17        $values = [];
18
19        foreach ($tools as $tool) {
20            if (!in_array($tool, $values, true)) {
21                $values[] = $tool;
22            }
23        }
24
25        $this->tools = $values;
26    }
27
28    /**
29     * Resolved for the panel: the field's own `#[Tools]` list, else the
30     * project's `richtext.tools`, else the built-in default.
31     *
32     * @return list<string>
33     */
34    public function getTools(): array
35    {
36        $tools = $this->tools ?: $this->owner->config()->richtext->tools ?: Tool::DEFAULT;
37
38        return array_map(static fn(Tool $tool): string => $tool->value, $tools);
39    }
40}