|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright 2015 Alexey Maslov <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace alxmsl\Google\GCM\Message; |
|
19
|
|
|
|
|
20
|
|
|
use alxmsl\Google\GCM\Exportable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Abstract payload data |
|
24
|
|
|
* @author alxmsl |
|
25
|
|
|
* @date 5/27/13 |
|
26
|
|
|
*/ |
|
27
|
|
|
abstract class PayloadData implements Exportable { |
|
28
|
|
|
/** |
|
29
|
|
|
* @var int content type identifier for payload data serialization |
|
30
|
|
|
*/ |
|
31
|
|
|
private $type = PayloadMessage::TYPE_PLAIN; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Content type identifier setter |
|
35
|
|
|
* @param int $type content type identifier |
|
36
|
|
|
* @return PayloadData self |
|
37
|
|
|
*/ |
|
38
|
4 |
|
public function setType($type) { |
|
39
|
4 |
|
$this->type = (int) $type; |
|
40
|
4 |
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Content type identifier getter |
|
45
|
|
|
* @return int content type identifier |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function getType() { |
|
48
|
6 |
|
return $this->type; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Abstract getter for payload data |
|
53
|
|
|
* @return array payload data |
|
54
|
|
|
*/ |
|
55
|
|
|
abstract protected function getDataFields(); |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Method for export instance data |
|
59
|
|
|
* @return array exported data |
|
60
|
|
|
*/ |
|
61
|
3 |
|
public function export() { |
|
62
|
3 |
|
$data = array(); |
|
63
|
3 |
|
switch ($this->getType()) { |
|
64
|
3 |
|
case PayloadMessage::TYPE_PLAIN: |
|
65
|
2 |
|
foreach ($this->getDataFields() as $key => $value) { |
|
66
|
2 |
|
$data['data.' . $key] = (string) $value; |
|
67
|
2 |
|
} |
|
68
|
2 |
|
break; |
|
69
|
2 |
|
case PayloadMessage::TYPE_JSON: |
|
70
|
2 |
|
$data['data'] = $this->getDataFields(); |
|
71
|
3 |
|
} |
|
72
|
3 |
|
return $data; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|