Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.62% covered (warning)
84.62%
11 / 13
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Text
84.62% covered (warning)
84.62%
11 / 13
66.67% covered (warning)
66.67%
4 / 6
10.36
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unwrap
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 strip
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 json
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Value;
6
7use Cosray\Field\Capability\Translatable;
8use Cosray\Field\Field;
9use Cosray\Field\Owner;
10
11use function Cosray\escape;
12
13/**
14 * @property-read Field&Translatable $field
15 */
16class Text extends Value
17{
18    protected string $value;
19
20    public function __construct(
21        Owner $owner,
22        Field&Translatable $field,
23        ValueContext $context,
24    ) {
25        parent::__construct($owner, $field, $context);
26    }
27
28    public function __toString(): string
29    {
30        return escape($this->unwrap());
31    }
32
33    public function unwrap(): string
34    {
35        if (isset($this->value)) {
36            return $this->value;
37        }
38
39        $value = $this->value();
40
41        if (is_string($value) || is_numeric($value)) {
42            $this->value = (string) $value;
43
44            return $this->value;
45        }
46
47        $this->value = '';
48
49        return '';
50    }
51
52    public function strip(array|string|null $allowed = null): string
53    {
54        /**
55         * As of now (early 2023), psalm does not support the
56         * type array as arguments to strip_tags's $allowed_tags.
57         *
58         * @psalm-suppress PossiblyInvalidArgument
59         */
60        return strip_tags((string) $this->unwrap(), $allowed);
61    }
62
63    public function json(): mixed
64    {
65        return $this->unwrap();
66    }
67
68    public function isset(): bool
69    {
70        return $this->unwrap() ?? null ? true : false;
71    }
72}