|
1
|
|
|
<?php |
|
2
|
|
|
namespace One; |
|
3
|
|
|
|
|
4
|
|
|
use One\Model\Article; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* FactoryArticle Class |
|
8
|
|
|
* |
|
9
|
|
|
* @method create |
|
10
|
|
|
* @method createArticle |
|
11
|
|
|
* @method validateArray |
|
12
|
|
|
* @method validateUrl |
|
13
|
|
|
* @method validateInteger |
|
14
|
|
|
* @method validateString |
|
15
|
|
|
* @method checkData |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
class FactoryArticle |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Create Article from array |
|
23
|
|
|
* |
|
24
|
|
|
* @param array $data |
|
25
|
|
|
* @return object Article |
|
26
|
|
|
*/ |
|
27
|
|
|
public static function create($data) |
|
28
|
|
|
{ |
|
29
|
|
|
$data = self::validateArray($data); |
|
|
|
|
|
|
30
|
|
|
$title = self::validateString((string) self::checkData($data, 'title', '')); |
|
31
|
|
|
$body = self::validateString((string) self::checkData($data, 'body', '')); |
|
32
|
|
|
$source = self::validateUrl((string) self::checkData($data, 'source', '')); |
|
33
|
|
|
$uniqueId = self::validateString((string) self::checkData($data, 'unique_id', '')); |
|
34
|
|
|
$typeId = self::validateInteger((int) self::checkData($data, 'type_id', '')); |
|
35
|
|
|
$categoryId = self::validateInteger((int) self::checkData($data, 'category_id', '')); |
|
36
|
|
|
$reporter = self::validateString((string) self::checkData($data, 'reporter', '')); |
|
37
|
|
|
$lead = self::validateString((string) self::checkData($data, 'lead', '')); |
|
38
|
|
|
$tags = self::validateString((string) self::checkData($data, 'tags', '')); |
|
39
|
|
|
$publishedAt = self::validateString((string) self::checkData($data, 'published_at', '')); |
|
40
|
|
|
$identifier = self::validateInteger((int) self::checkData($data, 'identifier', null)); |
|
41
|
|
|
return self::createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* functionality to check whether a variable is set or not. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $parts |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
private static function checkData($data, $key, $default = '') |
|
51
|
|
|
{ |
|
52
|
|
|
return isset($data[$key]) ? $data[$key] : $default; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create Article Object |
|
57
|
|
|
* |
|
58
|
|
|
* @param String $title |
|
59
|
|
|
* @param string $body |
|
60
|
|
|
* @param string $source |
|
61
|
|
|
* @param string $uniqueId |
|
62
|
|
|
* @param int $typeId |
|
63
|
|
|
* @param int $categoryId |
|
64
|
|
|
* @param string $reporter |
|
65
|
|
|
* @param string $lead |
|
66
|
|
|
* @param string $tags |
|
67
|
|
|
* @param string $publishedAt |
|
68
|
|
|
* @param int $identifier |
|
69
|
|
|
* @return Article Object |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function createArticle($title, $body, $source, $uniqueId, $typeId, $categoryId, $reporter, $lead, $tags, $publishedAt, $identifier) |
|
72
|
|
|
{ |
|
73
|
|
|
return new Article( |
|
74
|
|
|
$title, |
|
75
|
|
|
$body, |
|
76
|
|
|
$source, |
|
77
|
|
|
$uniqueId, |
|
78
|
|
|
$typeId, |
|
79
|
|
|
$categoryId, |
|
80
|
|
|
$reporter, |
|
81
|
|
|
$lead, |
|
82
|
|
|
$tags, |
|
83
|
|
|
$publishedAt, |
|
84
|
|
|
$identifier |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* functionality validity for array variables |
|
90
|
|
|
* |
|
91
|
|
|
* @param int $var |
|
92
|
|
|
* @return int |
|
93
|
|
|
*/ |
|
94
|
|
|
private static function validateArray($var) |
|
95
|
|
|
{ |
|
96
|
|
|
if (is_array($var)) { |
|
|
|
|
|
|
97
|
|
|
return $var; |
|
98
|
|
|
} |
|
99
|
|
|
throw new \Exception("The variable type must Array :"); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Make Sure Url in string with correct url format |
|
104
|
|
|
* |
|
105
|
|
|
* @param String $string |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
private static function validateUrl($var) |
|
109
|
|
|
{ |
|
110
|
|
|
if (filter_var($var, FILTER_VALIDATE_URL) === false) { |
|
111
|
|
|
throw new \Exception("Invalid url : $var"); |
|
112
|
|
|
} |
|
113
|
|
|
return $var; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* functionality validity for int variables |
|
118
|
|
|
* |
|
119
|
|
|
* @param int $var |
|
120
|
|
|
* @return int |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function validateInteger($var) |
|
123
|
|
|
{ |
|
124
|
|
|
if (filter_var($var, FILTER_VALIDATE_INT) === false) { |
|
125
|
|
|
throw new \Exception("The variable type must Integer :" . $var); |
|
126
|
|
|
} |
|
127
|
|
|
return $var; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* functionality validity for string variables |
|
132
|
|
|
* |
|
133
|
|
|
* @param String $var |
|
134
|
|
|
* @return String |
|
135
|
|
|
*/ |
|
136
|
|
|
private static function validateString($var) |
|
137
|
|
|
{ |
|
138
|
|
|
if (is_string($var) == true) { |
|
|
|
|
|
|
139
|
|
|
return $var; |
|
140
|
|
|
} |
|
141
|
|
|
throw new \Exception("The variable type must String :" . $var); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|