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

webeweb / xmltv-library / 8306651198

16 Mar 2024 08:55AM UTC coverage: 99.723%. Remained the same
8306651198

push

github

webeweb
Update CHANGELOG

1441 of 1445 relevant lines covered (99.72%)

311.43 hits per line

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

95.24
/src/Serializer/SerializerHelper.php
1
<?php
2

3
declare(strict_types = 1);
4

5
/*
6
 * This file is part of the xmltv-library package.
7
 *
8
 * (c) 2019 WEBEWEB
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace WBW\Library\XmlTv\Serializer;
15

16
use DateTime;
17
use DOMNode;
18
use WBW\Library\Serializer\Helper\SerializerHelper as BaseSerializerHelper;
19
use WBW\Library\Serializer\Helper\XmlDeserializerHelper;
20
use WBW\Library\Serializer\Helper\XmlSerializerHelper;
21
use WBW\Library\Types\Helper\ArrayHelper;
22
use WBW\Library\Types\Helper\StringHelper;
23
use WBW\Library\XmlTv\Model\AbstractModel;
24
use WBW\Library\XmlTv\Model\SecondaryTitle;
25

26
/**
27
 * Serializer helper.
28
 *
29
 * @author webeweb <https://github.com/webeweb>
30
 * @package WBW\Library\XmlTv\Serializer
31
 */
32
class SerializerHelper extends BaseSerializerHelper {
33

34
    /**
35
     * Date/time format.
36
     *
37
     * @var string
38
     */
39
    const DATE_TIME_FORMAT = "YmdHis O";
40

41
    /**
42
     * Deserialize a date/time.
43
     *
44
     * @param string|null $value The date/time.
45
     * @return DateTime|null Returns the date/time in case of success, null otherwise.
46
     */
47
    public static function deserializeDateTime(?string $value): ?DateTime {
48

49
        if (null === $value) {
64✔
50
            return null;
16✔
51
        }
52

53
        $dateTime = DateTime::createFromFormat(self::DATE_TIME_FORMAT, $value);
48✔
54
        if (false === $dateTime) {
48✔
55
            return null;
8✔
56
        }
57

58
        return $dateTime;
48✔
59
    }
60

61
    /**
62
     * Create a DOM node.
63
     *
64
     * @param string $name The name.
65
     * @param string|null $value The value.
66
     * @param array<string,string> $attributes The attributes.
67
     * @param bool $shortTag Short tag ?
68
     * @return string Returns the DOM node.
69
     */
70
    public static function domNode(string $name, ?string $value, array $attributes = [], bool $shortTag = false): string {
71

72
        $value = XmlSerializerHelper::xmlSerializeValue($value);
328✔
73

74
        foreach ($attributes as $k => $v) {
328✔
75
            $attributes[$k] = XmlSerializerHelper::xmlSerializeValue($v);
160✔
76
        }
77

78
        return StringHelper::domNode($name, $value, $attributes, $shortTag);
328✔
79
    }
80

81
    /**
82
     * Get a method name.
83
     *
84
     * @param string $action The action.
85
     * @param string $attribute The attribute.
86
     * @return string Returns the method name.
87
     */
88
    public static function getMethodName(string $action, string $attribute): string {
89

90
        $method = "";
448✔
91

92
        $attribute = str_replace(SecondaryTitle::DOM_NODE_NAME, "secondary-title", $attribute);
448✔
93

94
        $parts = explode("-", $attribute);
448✔
95
        foreach ($parts as $current) {
448✔
96
            $method .= ucfirst($current);
448✔
97
        }
98

99
        return implode("", [$action, $method]);
448✔
100
    }
101

102
    /**
103
     * Deserialize an array.
104
     *
105
     * @param mixed[] $array The array.
106
     * @param string $nodeName The node name.
107
     * @param AbstractModel $model The model.
108
     * @return void
109
     */
110
    public static function jsonDeserializeArray(array $array, string $nodeName, AbstractModel $model): void {
111

112
        $fct = __NAMESPACE__ . "\\JsonDeserializer::" . static::getMethodName("deserialize", $nodeName);
48✔
113
        $add = static::getMethodName("add", $nodeName);
48✔
114

115
        foreach ($array as $current) {
48✔
116

117
            if (0 === count($current)) {
48✔
118
                continue;
×
119
            }
120

121
            $model->$add(call_user_func($fct, $current));
48✔
122
        }
123
    }
12✔
124

125
    /**
126
     * Deserialize a model.
127
     *
128
     * @param array<string,mixed> $array The array.
129
     * @param string $nodeName The node name.
130
     * @param AbstractModel $model The model.
131
     * @return void
132
     */
133
    public static function jsonDeserializeModel(array $array, string $nodeName, AbstractModel $model): void {
134

135
        $data = ArrayHelper::get($array, $nodeName, []);
56✔
136
        if (0 === count($data)) {
56✔
137
            return;
×
138
        }
139

140
        $fct = __NAMESPACE__ . "\\JsonDeserializer::" . static::getMethodName("deserialize", $nodeName);
56✔
141
        $set = static::getMethodName("set", $nodeName);
56✔
142

143
        $model->$set(call_user_func($fct, $data));
56✔
144
    }
14✔
145

146
    /**
147
     * Deserialize an array.
148
     *
149
     * @param DOMNode $domNode The DOM node.
150
     * @param string $nodeName The node name.
151
     * @param AbstractModel $model The model.
152
     * @return void
153
     */
154
    public static function xmlDeserializeArray(DomNode $domNode, string $nodeName, AbstractModel $model): void {
155

156
        $fct = __NAMESPACE__ . "\\XmlDeserializer::" . static::getMethodName("deserialize", $nodeName);
416✔
157
        $add = static::getMethodName("add", $nodeName);
416✔
158

159
        $nodes = XmlDeserializerHelper::getDomNodesByName($nodeName, $domNode->childNodes);
416✔
160
        foreach ($nodes as $current) {
416✔
161
            $model->$add(call_user_func_array($fct, [$current]));
416✔
162
        }
163
    }
104✔
164

165
    /**
166
     * Deserialize a model.
167
     *
168
     * @param DOMNode $domNode The DOM node.
169
     * @param string $nodeName The node name.
170
     * @param AbstractModel $model The model.
171
     * @return void
172
     */
173
    public static function xmlDeserializeModel(DomNode $domNode, string $nodeName, AbstractModel $model): void {
174

175
        $fct = __NAMESPACE__ . "\\XmlDeserializer::" . static::getMethodName("deserialize", $nodeName);
424✔
176
        $set = static::getMethodName("set", $nodeName);
424✔
177

178
        $node = XmlDeserializerHelper::getDomNodeByName($nodeName, $domNode->childNodes);
424✔
179
        if (null !== $node) {
424✔
180
            $model->$set(call_user_func_array($fct, [$node]));
424✔
181
        }
182
    }
106✔
183
}
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