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

mixerapi / mixerapi-dev / 7434143287

06 Jan 2024 09:54PM UTC coverage: 94.68% (-1.1%) from 95.793%
7434143287

Pull #141

github

web-flow
Merge 4d1f37519 into 39576a822
Pull Request #141: CakePHP 5 support

961 of 1015 relevant lines covered (94.68%)

4.4 hits per line

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

56.52
/plugins/collection-view/src/View/XmlCollectionView.php
1
<?php
2
declare(strict_types=1);
3

4
namespace MixerApi\CollectionView\View;
5

6
use Cake\Core\Configure;
7
use Cake\View\SerializedView;
8
use MixerApi\CollectionView\Serializer;
9

10
class XmlCollectionView extends SerializedView
11
{
12
    /**
13
     * @var string
14
     */
15
    protected string $layoutPath = 'xml';
16

17
    /**
18
     * @var string
19
     */
20
    protected string $subDir = 'xml';
21

22
    /**
23
     * Response type.
24
     *
25
     * @var string
26
     */
27
    protected string $_responseType = 'xml';
28

29
    /**
30
     * Option to allow setting an array of custom options for Xml::fromArray()
31
     *
32
     * For e.g. 'format' as 'attributes' instead of 'tags'.
33
     *
34
     * @var array|null
35
     */
36
    protected ?array $xmlOptions;
37

38
    /**
39
     * Default config options.
40
     *
41
     * Use ViewBuilder::setOption()/setOptions() in your controller to set these options.
42
     *
43
     * - `serialize`: Option to convert a set of view variables into a serialized response.
44
     *   Its value can be a string for single variable name or array for multiple
45
     *   names. If true all view variables will be serialized. If null or false
46
     *   normal view template will be rendered.
47
     * - `xmlOptions`: Option to allow setting an array of custom options for Xml::fromArray().
48
     *   For e.g. 'format' as 'attributes' instead of 'tags'.
49
     * - `rootNode`: Root node name. Defaults to "response".
50
     *
51
     * @var array
52
     * @psalm-var array{serialize:string|bool|null, xmlOptions: int|null, rootNode: string|null}
53
     */
54
    protected array $_defaultConfig = [
55
        'serialize' => null,
56
        'xmlOptions' => null,
57
        'rootNode' => null,
58
    ];
59

60
    /**
61
     * @inheritDoc
62
     */
63
    public function initialize(): void
64
    {
65
        parent::initialize();
1✔
66
        $this->loadHelper('Paginator', [
1✔
67
            'templates' => 'MixerApi/CollectionView.paginator-template',
1✔
68
        ]);
1✔
69
    }
70

71
    /**
72
     * @inheritDoc
73
     */
74
    public static function contentType(): string
75
    {
76
        return 'application/xml';
2✔
77
    }
78

79
    /**
80
     * @inheritDoc
81
     */
82
    protected function _serialize($serialize): string
83
    {
84
        $rootNode = $this->getConfig('rootNode', 'response');
1✔
85

86
        if (is_array($serialize)) {
1✔
87
            if (empty($serialize)) {
×
88
                $serialize = '';
×
89
            } elseif (count($serialize) === 1) {
×
90
                $serialize = current($serialize);
×
91
            }
92
        }
93

94
        if (is_array($serialize)) {
1✔
95
            $data = [];
×
96
            foreach ($serialize as $alias => $key) {
×
97
                if (is_numeric($alias)) {
×
98
                    $alias = $key;
×
99
                }
100
                if (array_key_exists($key, $this->viewVars)) {
×
101
                    $data[$alias] = $this->viewVars[$key];
×
102
                }
103
            }
104
        } else {
105
            $data = $this->viewVars[$serialize] ?? [];
1✔
106
        }
107

108
        $options = $this->getConfig('xmlOptions', []);
1✔
109
        if (Configure::read('debug')) {
1✔
110
            $options['pretty'] = true;
1✔
111
        }
112

113
        return (new Serializer($data, $this->getRequest(), $this->Paginator))->asXml($options, $rootNode);
1✔
114
    }
115
}
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