Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ResourceCustomActionResourceHeader
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 getFallbackText
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 createFromDefinition
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Core\Translator\ValueObjects;
3
4use Apie\Common\ActionDefinitions\RunResourceMethodDefinition;
5use Apie\Core\Attributes\Description;
6use Apie\Core\Attributes\ExampleValue;
7use Apie\Core\Identifiers\SnakeCaseSlug;
8use Apie\Core\Lists\ItemHashmap;
9
10#[Description('Header shown on form for a domain specific action')]
11#[ExampleValue('apie.bounded.test.example.user.action.custom.1234.deactivate.header.authenticated')]
12class ResourceCustomActionResourceHeader extends AbstractTranslation
13{
14    protected const MIDDLE_REGEX = 'action\.custom\.:id\.:action\.header';
15
16    public function getFallbackText(): string
17    {
18        $resourceId = $this->getPlaceholders()['id'] ?? null;
19        $action = ucfirst(SnakeCaseSlug::fromText($this->getPlaceholders()['action'] ?? 'action'));
20        
21        $suffix = $resourceId ? (' on ' . $resourceId) : '';
22        $id = $this->prefix->getResourceIdentifier();
23        if ($id) {
24            return $id . ' ' . $action . $suffix;
25        }
26        return ucfirst($action) . $suffix;
27    }
28
29    public static function createFromDefinition(RunResourceMethodDefinition $actionDefinition, ?bool $authenticated): static
30    {
31        $action = SnakeCaseSlug::fromClass($actionDefinition->getMethod());
32        return new static(
33            new TranslationStringPrefix(
34                $actionDefinition->getBoundedContextId(),
35                SnakeCaseSlug::fromClass($actionDefinition->getResourceName())
36            ),
37            'action.custom.:id.' . $action . '.header',
38            new TranslationStringSuffix(null, $authenticated),
39            new ItemHashmap(['action' => $action])
40        );
41    }
42}