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

Articus / DataTransfer / 12597154924

03 Jan 2025 11:39AM UTC coverage: 82.183% (-0.05%) from 82.23%
12597154924

push

github

Articus
- now SerializableValue supports InvalidData exceptions inside unserializer

4 of 6 new or added lines in 1 file covered. (66.67%)

1107 of 1347 relevant lines covered (82.18%)

8.07 hits per line

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

69.23
/src/Articus/DataTransfer/Validator/SerializableValue.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Articus\DataTransfer\Validator;
5

6
use Articus\DataTransfer\Exception;
7
use function is_string;
8

9
/**
10
 * Configurable validator that checks if data is a valid serialized value.
11
 */
12
class SerializableValue implements ValidatorInterface
13
{
14
        public const INVALID = 'serializedValueInvalid';
15
        public const INVALID_INNER = 'serializedValueInvalidInner';
16

17
        /**
18
         * Internal validator for unserialized values
19
         */
20
        protected ValidatorInterface $valueValidator;
21

22
        /**
23
         * A way to decode value from string
24
         * @var callable(string): mixed
25
         */
26
        protected $unserializer;
27

28
        /**
29
         * @param ValidatorInterface $valueValidator
30
         * @param callable $unserializer
31
         */
32
        public function __construct(ValidatorInterface $valueValidator, callable $unserializer)
33
        {
34
                $this->valueValidator = $valueValidator;
2✔
35
                $this->unserializer = $unserializer;
2✔
36
        }
37

38
        /**
39
         * @inheritDoc
40
         */
41
        public function validate($data): array
42
        {
43
                $result = [];
2✔
44
                if ($data !== null)
×
45
                {
46
                        if (is_string($data))
×
47
                        {
48
                                try
49
                                {
50
                                        $value = ($this->unserializer)($data);
2✔
51
                                        $valueViolations = $this->valueValidator->validate($value);
2✔
NEW
52
                                        if (!empty($valueViolations))
×
53
                                        {
54
                                                $result[self::INVALID_INNER] = $valueViolations;
2✔
55
                                        }
56
                                }
NEW
57
                                catch (Exception\InvalidData $e)
×
58
                                {
59
                                        $result[self::INVALID_INNER] = $e->getViolations();
2✔
60
                                }
61
                        }
62
                        else
63
                        {
64
                                $result[self::INVALID] = 'Invalid data: expecting string.';
2✔
65
                        }
66
                }
67
                return $result;
2✔
68
        }
69
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc