1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Stack Exchange Api Client library. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2014-2016 Beñat Espiña <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BenatEspina\StackExchangeApiClient\Infrastructure\Domain\Model; |
13
|
|
|
|
14
|
|
|
use BenatEspina\StackExchangeApiClient\Domain\Model\NetworkPost; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The anemic implementation of network post domain class. |
18
|
|
|
* |
19
|
|
|
* @author Beñat Espiña <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class AnemicNetworkPost implements NetworkPost |
22
|
|
|
{ |
23
|
|
|
const POST_TYPE_QUESTION = 'question'; |
24
|
|
|
const POST_TYPE_ANSWER = 'answer'; |
25
|
|
|
|
26
|
|
|
private $id; |
27
|
|
|
private $postType; |
28
|
|
|
private $score; |
29
|
|
|
private $title; |
30
|
|
|
|
31
|
|
|
public static function fromProperties($id, $postType, $score, $title) |
32
|
|
|
{ |
33
|
|
|
return new self($id, $postType, $score, $title); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function fromJson($data) |
37
|
|
|
{ |
38
|
|
|
return new self( |
39
|
|
|
array_key_exists('post_id', $data) ? $data['post_id'] : null, |
40
|
|
|
array_key_exists('post_type', $data) ? $data['post_type'] : null, |
41
|
|
|
array_key_exists('score', $data) ? $data['score'] : null, |
42
|
|
|
array_key_exists('title', $data) ? $data['title'] : null |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function __construct($id = null, $postType = null, $score = null, $title = null) |
47
|
|
|
{ |
48
|
|
|
$this->id = $id; |
49
|
|
|
$this->setPostType($postType); |
50
|
|
|
$this->score = $score; |
51
|
|
|
$this->title = $title; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getId() |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
return $this->id; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function setId($id) |
60
|
|
|
{ |
61
|
|
|
$this->id = $id; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getPostType() |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
return $this->postType; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function setPostType($postType) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
if (in_array($postType, [self::POST_TYPE_QUESTION, self::POST_TYPE_ANSWER], true)) { |
74
|
|
|
$this->postType = $postType; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getScore() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
return $this->score; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setScore($score) |
86
|
|
|
{ |
87
|
|
|
$this->score = $score; |
88
|
|
|
|
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getTitle() |
|
|
|
|
93
|
|
|
{ |
94
|
|
|
return $this->title; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setTitle($title) |
98
|
|
|
{ |
99
|
|
|
$this->title = $title; |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.