Passed
Push — master ( 5dc036...8f7930 )
by Carlos
03:16
created

News::propertiesToArray()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel\Messages;
13
14
/**
15
 * Class News.
16
 *
17
 * @author overtrue <[email protected]>
18
 */
19
class News extends Message
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $type = 'news';
25
26
    /**
27
     * @var array
28
     */
29
    protected $properties = [
30
        'items',
31
    ];
32
33
    /**
34
     * News constructor.
35
     *
36
     * @param array $items
37
     */
38
    public function __construct(array $items = [])
39
    {
40
        parent::__construct(compact('items'));
41
    }
42
43
    /**
44
     * @param array $data
45
     * @param array $aliases
46
     *
47
     * @return array
48
     */
49
    public function propertiesToArray(array $data, array $aliases = []): array
50
    {
51
        return ['articles' => array_map(function ($item) {
52
            if ($item instanceof NewsItem) {
53
                return $item->toJsonArray();
54
            }
55
        }, $this->get('items'))];
56
    }
57
58
    public function toXmlArray()
59
    {
60
        $items = [];
61
62
        foreach ($this->get('items') as $item) {
63
            if ($item instanceof NewsItem) {
64
                $items[] = $item->toXmlArray();
65
            }
66
        }
67
68
        return [
69
            'ArticleCount' => count($items),
70
            'Articles' => $items,
71
        ];
72
    }
73
}
74