• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

smartemailing / types / 38097019585

11 Oct 2026 12:01AM UTC coverage: 93.707% (+1.1%) from 92.657%
38097019585

push

github

web-flow
Merge pull request #151 from smartemailing/m_invalid_type_public_message

InvalidTypeException: add public message and error details (getPublic…

79 of 80 new or added lines in 3 files covered. (98.75%)

2 existing lines in 2 files now uncovered.

1370 of 1462 relevant lines covered (93.71%)

0.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/src/JsonString.php
1
<?php
2

3
declare(strict_types = 1);
4

5
namespace SmartEmailing\Types;
6

7
use Nette\Utils\Json;
8
use Nette\Utils\JsonException;
9
use SmartEmailing\Types\Comparable\ComparableInterface;
10
use SmartEmailing\Types\Comparable\StringComparableTrait;
11
use SmartEmailing\Types\ExtractableTraits\ExtractableTrait;
12

13
final class JsonString implements ToStringInterface, ComparableInterface
14
{
15

16
        use ExtractableTrait;
17
        use ToStringTrait;
18
        use StringComparableTrait;
19

20
        private function __construct(
1 ✔
21
                private string $value
22
        )
23
        {
24
                if (!$this->isValid($value)) {
1 ✔
25
                        throw new InvalidTypeException('Invalid JSON string');
1 ✔
26
                }
27
        }
1 ✔
28

29
        public static function encode(
30
                mixed $value,
31
                bool $oneLine = false
32
        ): self
33
        {
34
                try {
35
                        return new static(
1 ✔
36
                                Json::encode(
1 ✔
UNCOV
37
                                        $value,
×
38
                                        $oneLine ? 0 : Json::PRETTY
1 ✔
39
                                )
40
                        );
41
                } catch (JsonException $e) {
1 ✔
42
                        throw new InvalidTypeException($e->getMessage());
1 ✔
43
                }
44
        }
45

46
        public function getValue(): string
47
        {
48
                return $this->value;
1 ✔
49
        }
50

51
        public function getDecodedValue(): mixed
52
        {
53
                return Json::decode($this->value, \JSON_OBJECT_AS_ARRAY);
1 ✔
54
        }
55

56
        /**
57
         * @throws \SmartEmailing\Types\InvalidTypeException
58
         */
59
        public static function from(
60
                mixed $data
61
        ): static
62
        {
63
                if ($data instanceof self) {
1 ✔
64
                        return $data;
1 ✔
65
                }
66

67
                $string = StringType::fromOrNull($data, true);
1 ✔
68

69
                if (\is_string($string)) {
1 ✔
70
                        return new static($string);
1 ✔
71
                }
72

73
                $array = Arrays::fromOrNull($data, true);
1 ✔
74

75
                if (\is_array($array)) {
1 ✔
76
                        return self::encode($data);
1 ✔
77
                }
78

79
                throw InvalidTypeException::typesError(['string', 'array'], $data);
1 ✔
80
        }
81

82
        private function isValid(
83
                string $value
84
        ): bool
85
        {
86
                try {
87
                        Json::decode($value);
1 ✔
88

89
                        return true;
1 ✔
90
                } catch (JsonException $e) {
1 ✔
91
                        return false;
1 ✔
92
                }
93
        }
94

95
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc