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 | 3 | public function __construct(array $items = []) |
|
39 | { |
||
40 | 3 | parent::__construct(compact('items')); |
|
41 | 3 | } |
|
42 | |||
43 | /** |
||
44 | * @param array $data |
||
45 | * @param array $aliases |
||
46 | * |
||
47 | * @return array |
||
48 | */ |
||
49 | 1 | public function propertiesToArray(array $data, array $aliases = []): array |
|
50 | { |
||
51 | 1 | return ['articles' => array_map(function ($item) { |
|
52 | 1 | if ($item instanceof NewsItem) { |
|
53 | 1 | return $item->toJsonArray(); |
|
54 | } |
||
55 | 1 | }, $this->get('items'))]; |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
56 | } |
||
57 | |||
58 | 2 | public function toXmlArray() |
|
59 | { |
||
60 | 2 | $items = []; |
|
61 | |||
62 | 2 | foreach ($this->get('items') as $item) { |
|
63 | 2 | if ($item instanceof NewsItem) { |
|
64 | 2 | $items[] = $item->toXmlArray(); |
|
65 | } |
||
66 | } |
||
67 | |||
68 | return [ |
||
69 | 2 | 'ArticleCount' => count($items), |
|
70 | 2 | 'Articles' => $items, |
|
71 | ]; |
||
72 | } |
||
73 | } |
||
74 |