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

mixerapi / mixerapi-dev / 13002204893

28 Jan 2025 02:07AM UTC coverage: 86.907%. First build
13002204893

Pull #156

github

web-flow
Merge 01d450ee5 into 9d91f041b
Pull Request #156: Adds beforeSerialize and afterSerialize events

24 of 33 new or added lines in 3 files covered. (72.73%)

916 of 1054 relevant lines covered (86.91%)

3.24 hits per line

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

95.0
/plugins/collection-view/src/Serializer.php
1
<?php
2
declare(strict_types=1);
3

4
namespace MixerApi\CollectionView;
5

6
use Adbar\Dot;
7
use Cake\Core\Configure;
8
use Cake\Datasource\Paging\PaginatedResultSet;
9
use Cake\Datasource\ResultSetInterface;
10
use Cake\Event\Event;
11
use Cake\Event\EventManager;
12
use Cake\Http\ServerRequest;
13
use Cake\Utility\Xml;
14
use Cake\View\Helper\PaginatorHelper;
15
use RuntimeException;
16

17
/**
18
 * Serializes the CollectionView into either JSON or XML.
19
 */
20
class Serializer
21
{
22
    public const BEFORE_SERIALIZE_EVENT = 'MixerApi.CollectionView.beforeSerialize';
23
    public const AFTER_SERIALIZE_EVENT = 'MixerApi.CollectionView.afterSerialize';
24

25
    /**
26
     * serialized data
27
     *
28
     * @var array
29
     */
30
    private mixed $data;
31

32
    /**
33
     * @var array
34
     */
35
    private array $config;
36

37
    /**
38
     * If constructed without parameters collection meta data will not be added to HAL $data
39
     *
40
     * @param mixed $serialize the data to be converted into a HAL array
41
     * @param \Cake\Http\ServerRequest|null $request optional ServerRequest
42
     * @param \Cake\View\Helper\PaginatorHelper|null $paginator optional PaginatorHelper
43
     */
44
    public function __construct(
45
        mixed $serialize,
46
        private ?ServerRequest $request = null,
47
        private ?PaginatorHelper $paginator = null
48
    ) {
49
        $this->config = Configure::read('CollectionView');
5✔
50

51
        if ($serialize instanceof ResultSetInterface || $serialize instanceof PaginatedResultSet) {
5✔
52
            $this->data = $this->collection($serialize);
4✔
53
        } else {
54
            $this->data = $serialize;
1✔
55
        }
56
    }
57

58
    /**
59
     * Serializes as JSON
60
     *
61
     * @param int $jsonOptions JSON options see https://www.php.net/manual/en/function.json-encode.php
62
     * @return string
63
     * @throws \RuntimeException
64
     */
65
    public function asJson(int $jsonOptions = 0): string
66
    {
67
        EventManager::instance()->dispatch(new Event(self::BEFORE_SERIALIZE_EVENT, $this, [
3✔
68
            'type' => 'json',
3✔
69
        ]));
3✔
70

71
        $json = json_encode($this->data, $jsonOptions);
3✔
72

73
        if ($json === false) {
3✔
74
            throw new RuntimeException(json_last_error_msg(), json_last_error());
1✔
75
        }
76

77
        EventManager::instance()->dispatch(new Event(self::AFTER_SERIALIZE_EVENT, $this, [
2✔
78
            'type' => 'json',
2✔
79
            'data' => $json,
2✔
80
        ]));
2✔
81

82
        return $json;
2✔
83
    }
84

85
    /**
86
     * Serializes as XML
87
     *
88
     * @param array $options same as Cake\Utility\Xml
89
     * @param string $rootNode the rootNode
90
     * @return string
91
     * @throws \RuntimeException
92
     */
93
    public function asXml(array $options, string $rootNode = 'response'): string
94
    {
95
        EventManager::instance()->dispatch(new Event(self::BEFORE_SERIALIZE_EVENT, $this, [
2✔
96
            'type' => 'xml',
2✔
97
        ]));
2✔
98

99
        $xml = Xml::fromArray([$rootNode => $this->data], $options)->saveXML();
2✔
100

101
        EventManager::instance()->dispatch(new Event(self::AFTER_SERIALIZE_EVENT, $this, [
2✔
102
            'type' => 'xml',
2✔
103
            'data' => $xml,
2✔
104
        ]));
2✔
105

106
        return $xml;
2✔
107
    }
108

109
    /**
110
     * @return mixed
111
     */
112
    public function getData(): mixed
113
    {
114
        return $this->data;
1✔
115
    }
116

117
    /**
118
     * @param mixed $data The data to be serialized
119
     * @return void
120
     */
121
    public function setData(mixed $data): void
122
    {
NEW
123
        $this->data = $data;
×
124
    }
125

126
    /**
127
     * @return \Cake\Http\ServerRequest|null
128
     */
129
    public function getRequest(): ?ServerRequest
130
    {
NEW
131
        return $this->request;
×
132
    }
133

134
    /**
135
     * @return \Cake\View\Helper\PaginatorHelper|null
136
     */
137
    public function getPaginatorHelper(): ?PaginatorHelper
138
    {
NEW
139
        return $this->paginator;
×
140
    }
141

142
    /**
143
     * @param \Cake\Datasource\ResultSetInterface|\Cake\Datasource\Paging\PaginatedResultSet $resultSet the data to be converted into a HAL array
144
     * @return array
145
     */
146
    private function collection(mixed $resultSet): array
147
    {
148
        $dot = new Dot();
4✔
149
        foreach ($this->config as $key => $value) {
4✔
150
            $dot->set($key, $value);
4✔
151
        }
152

153
        $return = $dot->all();
4✔
154

155
        $collection = array_search('{{collection}}', $this->config);
4✔
156
        $url = array_search('{{url}}', $return[$collection]);
4✔
157
        $count = array_search('{{count}}', $return[$collection]);
4✔
158
        $pages = array_search('{{pages}}', $return[$collection]);
4✔
159
        $total = array_search('{{total}}', $return[$collection]);
4✔
160
        $next = array_search('{{next}}', $return[$collection]);
4✔
161
        $prev = array_search('{{prev}}', $return[$collection]);
4✔
162
        $first = array_search('{{first}}', $return[$collection]);
4✔
163
        $last = array_search('{{last}}', $return[$collection]);
4✔
164
        $data = array_search('{{data}}', $this->config);
4✔
165

166
        $return[$collection][$count] = intval($resultSet->count());
4✔
167
        $return[$collection][$url] = '';
4✔
168

169
        if ($this->request instanceof ServerRequest) {
4✔
170
            $uri = $this->request->getUri();
4✔
171
            $query = $uri->getQuery();
4✔
172
            $return[$collection][$url] = $uri->getPath();
4✔
173
            $return[$collection][$url] .= !empty($query) ? '?' . $query : '';
4✔
174
        }
175

176
        if ($this->paginator instanceof PaginatorHelper) {
4✔
177
            $return[$collection][$next] = $this->paginator->next();
4✔
178
            $return[$collection][$prev] = $this->paginator->prev();
4✔
179
            $return[$collection][$first] = $this->paginator->first();
4✔
180
            $return[$collection][$last] = $this->paginator->last();
4✔
181
            $return[$collection][$pages] = $this->paginator->total();
4✔
182
            $return[$collection][$total] = intval($this->paginator->param('totalCount'));
4✔
183
        }
184

185
        if (empty($return[$collection][$first]) && !empty($return[$collection][$url])) {
4✔
186
            $return[$collection][$first] = $return[$collection][$url];
4✔
187
        }
188

189
        $return[$data] = $resultSet->toArray();
4✔
190

191
        return $return;
4✔
192
    }
193
}
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