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

Articus / DataTransfer / 9052205688

12 May 2024 02:47PM UTC coverage: 82.23% (-1.4%) from 83.617%
9052205688

push

github

Articus
- new configurable strategy and validator to deal with serialized values

24 of 51 new or added lines in 2 files covered. (47.06%)

1106 of 1345 relevant lines covered (82.23%)

8.08 hits per line

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

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

4
namespace Articus\DataTransfer\Strategy;
5

6
use Articus\DataTransfer\Exception;
7
use InvalidArgumentException;
8
use function get_class;
9
use function gettype;
10
use function is_object;
11
use function is_string;
12
use function sprintf;
13

14
/**
15
 * Configurable strategy to deal with serialized values.
16
 */
17
class SerializableValue implements StrategyInterface
18
{
19
        /**
20
         * Internal strategy to perform data transfer for unserialized values
21
         */
22
        protected StrategyInterface $valueStrategy;
23

24
        /**
25
         * A way to encode typed data to string
26
         * @var callable(mixed): string
27
         */
28
        protected $serializer;
29

30
        /**
31
         * A way to decode typed data from string
32
         * @var callable(string): mixed
33
         */
34
        protected $unserializer;
35

36
        public function __construct(StrategyInterface $valueStrategy, callable $serializer, callable $unserializer)
37
        {
38
                $this->valueStrategy = $valueStrategy;
6✔
39
                $this->serializer = $serializer;
6✔
40
                $this->unserializer = $unserializer;
6✔
41
        }
42

43
        /**
44
         * @inheritDoc
45
         */
46
        public function extract($from)
47
        {
48
                return ($from === null) ? null : ($this->serializer)($this->valueStrategy->extract($from));
2✔
49
        }
50

51
        /**
52
         * @inheritDoc
53
         */
54
        public function hydrate($from, &$to): void
55
        {
NEW
56
                if ($from === null)
×
57
                {
58
                        $to = null;
2✔
59
                }
NEW
60
                elseif (!is_string($from))
×
61
                {
NEW
62
                        throw new Exception\InvalidData(
×
NEW
63
                                Exception\InvalidData::DEFAULT_VIOLATION,
×
NEW
64
                                new InvalidArgumentException(sprintf(
×
NEW
65
                                        'Hydration can be done only from string, not %s',
×
NEW
66
                                        is_object($from) ? get_class($from) : gettype($from)
×
NEW
67
                                ))
×
68
                        );
2✔
69
                }
70
                else
71
                {
72
                        $value = ($this->unserializer)($from);
2✔
73
                        $this->valueStrategy->hydrate($value, $to);
2✔
74
                }
75
        }
76

77
        /**
78
         * @inheritDoc
79
         */
80
        public function merge($from, &$to): void
81
        {
NEW
82
                if ($from === null)
×
83
                {
84
                        $to = null;
2✔
85
                }
NEW
86
                elseif (!is_string($from))
×
87
                {
NEW
88
                        throw new Exception\InvalidData(
×
NEW
89
                                Exception\InvalidData::DEFAULT_VIOLATION,
×
NEW
90
                                new InvalidArgumentException(sprintf(
×
NEW
91
                                        'Merge can be done only from string, not %s',
×
NEW
92
                                        is_object($from) ? get_class($from) : gettype($from)
×
NEW
93
                                ))
×
94
                        );
2✔
95
                }
NEW
96
                elseif ($to === null)
×
97
                {
98
                        $to = $from;
2✔
99
                }
NEW
100
                elseif (!is_string($to))
×
101
                {
NEW
102
                        throw new Exception\InvalidData(
×
NEW
103
                                Exception\InvalidData::DEFAULT_VIOLATION,
×
NEW
104
                                new InvalidArgumentException(sprintf(
×
NEW
105
                                        'Merge can be done only to string, not %s',
×
NEW
106
                                        is_object($to) ? get_class($to) : gettype($to)
×
NEW
107
                                ))
×
108
                        );
2✔
109
                }
110
                else
111
                {
112
                        $fromValue = ($this->unserializer)($from);
2✔
113
                        $toValue = ($this->unserializer)($to);
2✔
114

115
                        $this->valueStrategy->merge($fromValue, $toValue);
2✔
116
                        $to = ($this->serializer)($toValue);
2✔
117
                }
118
        }
119
}
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