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

webeweb / xmltv-library / 8876587662

29 Apr 2024 09:48AM UTC coverage: 99.723%. Remained the same
8876587662

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\Common\Helper\ArrayHelper;
19
use WBW\Library\Common\Helper\StringHelper;
20
use WBW\Library\Common\Serializer\XmlDeserializer as BaseXmlDeserializer;
21
use WBW\Library\Common\Serializer\XmlSerializer as BaseXmlSerializer;
22
use WBW\Library\XmlTv\Model\AbstractModel;
23
use WBW\Library\XmlTv\Model\SecondaryTitle;
24

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

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

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

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

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

57
        return $dateTime;
48✔
58
    }
59

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

71
        $value = BaseXmlSerializer::serializeValue($value);
328✔
72

73
        foreach ($attributes as $k => $v) {
328✔
74
            $attributes[$k] = BaseXmlSerializer::serializeValue($v);
160✔
75
        }
76

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

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

89
        $method = "";
448✔
90

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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