@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | /** |
| 10 | 10 | * map a single article to main attributes in Article Class |
| 11 | 11 | * @param string $singleJsonArticle JSON response |
| 12 | - * @return Article\Exception |
|
| 12 | + * @return Article |
|
| 13 | 13 | */ |
| 14 | 14 | public function article($singleJsonArticle) { |
| 15 | 15 | if ($this->jsonToArray($singleJsonArticle)) { |
@@ -49,7 +49,7 @@ discard block |
||
| 49 | 49 | /** |
| 50 | 50 | * Make sure value is integer |
| 51 | 51 | * @param int $int |
| 52 | - * @return boolean |
|
| 52 | + * @return integer |
|
| 53 | 53 | */ |
| 54 | 54 | private function filterInteger($int) { |
| 55 | 55 | if (is_int($int)) { |
@@ -92,6 +92,7 @@ discard block |
||
| 92 | 92 | * Get value of array based on attributes(keys) |
| 93 | 93 | * @param supported php variables $attribute |
| 94 | 94 | * @param array $data |
| 95 | + * @param string $attribute |
|
| 95 | 96 | * @return supported php variables |
| 96 | 97 | */ |
| 97 | 98 | private function getValue($attribute, $data) { |
@@ -6,121 +6,121 @@ |
||
| 6 | 6 | |
| 7 | 7 | class FormatMapping { |
| 8 | 8 | |
| 9 | - /** |
|
| 10 | - * map a single article to main attributes in Article Class |
|
| 11 | - * @param string $singleJsonArticle JSON response |
|
| 12 | - * @return Article\Exception |
|
| 13 | - */ |
|
| 14 | - public function article($singleJsonArticle) { |
|
| 15 | - if ($this->jsonToArray($singleJsonArticle)) { |
|
| 16 | - $dataArticle = $this->jsonToArray($singleJsonArticle)['data']; |
|
| 17 | - |
|
| 18 | - $article = new Article( |
|
| 19 | - |
|
| 20 | - $title = $this->filterString($this->getValue('title', $dataArticle)), |
|
| 21 | - |
|
| 22 | - $body = $this->filterString($this->getValue('body', $dataArticle)), |
|
| 23 | - |
|
| 24 | - $source = $this->filterString($this->getValue('source', $dataArticle)), |
|
| 25 | - |
|
| 26 | - $uniqueId = $this->getValue('unique_id', $dataArticle), |
|
| 27 | - |
|
| 28 | - $typeId = $this->filterInteger($this->getValue('type_id', $dataArticle['type'])), |
|
| 29 | - |
|
| 30 | - $categoryId = $this->filterInteger($this->getValue('category_id', $dataArticle['category'])), |
|
| 31 | - |
|
| 32 | - $reporter = $this->getValue('reporter', $dataArticle), |
|
| 33 | - |
|
| 34 | - $lead = $this->filterString($this->getValue('lead', $dataArticle)), |
|
| 35 | - |
|
| 36 | - $tags = $this->getValue('tag_name', $dataArticle['tags']), |
|
| 37 | - |
|
| 38 | - $publishedAt = $this->filterDate($this->getValue('published_at', $dataArticle)), |
|
| 39 | - |
|
| 40 | - $identifier = $this->filterInteger($this->getValue('id', $dataArticle)) |
|
| 41 | - ); |
|
| 42 | - |
|
| 43 | - return $article; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - throw new \Exception("Invalid JSON Response", 1); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Make sure value is integer |
|
| 51 | - * @param int $int |
|
| 52 | - * @return boolean |
|
| 53 | - */ |
|
| 54 | - private function filterInteger($int) { |
|
| 55 | - if (is_int($int)) { |
|
| 56 | - return $int; |
|
| 57 | - } |
|
| 58 | - throw new \Exception("Invalid Integer", 1); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Make sure string is not null or empty |
|
| 63 | - * @param null/string $str |
|
| 64 | - * @return string/exception |
|
| 65 | - */ |
|
| 66 | - private function filterString($str) { |
|
| 67 | - if (strlen($str) > 0 && !is_null($str)) { |
|
| 68 | - return $str; |
|
| 69 | - } |
|
| 70 | - throw new \Exception("String required", 1); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Make Sure Date in string with correct format state |
|
| 75 | - * |
|
| 76 | - * @param \DateTimeInterface|string|int|null $date |
|
| 77 | - * @return string |
|
| 78 | - */ |
|
| 79 | - private function filterDate($date) { |
|
| 80 | - if (empty($date)) { |
|
| 81 | - $date = new \DateTime("now", new \DateTimeZone("Asia/Jakarta")); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - if (is_string($date) || is_int($date)) { |
|
| 85 | - $date = new \DateTime($date); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - return $this->formatDate($date); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Get value of array based on attributes(keys) |
|
| 93 | - * @param supported php variables $attribute |
|
| 94 | - * @param array $data |
|
| 95 | - * @return supported php variables |
|
| 96 | - */ |
|
| 97 | - private function getValue($attribute, $data) { |
|
| 98 | - if (isset($data[$attribute])) { |
|
| 99 | - return $data[$attribute]; |
|
| 100 | - } |
|
| 101 | - return null; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * format date into required format |
|
| 106 | - * |
|
| 107 | - * @param \DateTimeInterface $date |
|
| 108 | - * @return string |
|
| 109 | - */ |
|
| 110 | - private function formatDate($date) { |
|
| 111 | - return $date->format("Y-m-d H:i:s"); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Convert JSON string to associative array |
|
| 116 | - * @param string $jsonResponse |
|
| 117 | - * @return array if it is valid json, null otherwise |
|
| 118 | - */ |
|
| 119 | - public function jsonToArray($jsonResponse) { |
|
| 120 | - try { |
|
| 121 | - return json_decode($jsonResponse, true); |
|
| 122 | - } catch (\Exception $e) { |
|
| 123 | - return false; |
|
| 124 | - } |
|
| 125 | - } |
|
| 9 | + /** |
|
| 10 | + * map a single article to main attributes in Article Class |
|
| 11 | + * @param string $singleJsonArticle JSON response |
|
| 12 | + * @return Article\Exception |
|
| 13 | + */ |
|
| 14 | + public function article($singleJsonArticle) { |
|
| 15 | + if ($this->jsonToArray($singleJsonArticle)) { |
|
| 16 | + $dataArticle = $this->jsonToArray($singleJsonArticle)['data']; |
|
| 17 | + |
|
| 18 | + $article = new Article( |
|
| 19 | + |
|
| 20 | + $title = $this->filterString($this->getValue('title', $dataArticle)), |
|
| 21 | + |
|
| 22 | + $body = $this->filterString($this->getValue('body', $dataArticle)), |
|
| 23 | + |
|
| 24 | + $source = $this->filterString($this->getValue('source', $dataArticle)), |
|
| 25 | + |
|
| 26 | + $uniqueId = $this->getValue('unique_id', $dataArticle), |
|
| 27 | + |
|
| 28 | + $typeId = $this->filterInteger($this->getValue('type_id', $dataArticle['type'])), |
|
| 29 | + |
|
| 30 | + $categoryId = $this->filterInteger($this->getValue('category_id', $dataArticle['category'])), |
|
| 31 | + |
|
| 32 | + $reporter = $this->getValue('reporter', $dataArticle), |
|
| 33 | + |
|
| 34 | + $lead = $this->filterString($this->getValue('lead', $dataArticle)), |
|
| 35 | + |
|
| 36 | + $tags = $this->getValue('tag_name', $dataArticle['tags']), |
|
| 37 | + |
|
| 38 | + $publishedAt = $this->filterDate($this->getValue('published_at', $dataArticle)), |
|
| 39 | + |
|
| 40 | + $identifier = $this->filterInteger($this->getValue('id', $dataArticle)) |
|
| 41 | + ); |
|
| 42 | + |
|
| 43 | + return $article; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + throw new \Exception("Invalid JSON Response", 1); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Make sure value is integer |
|
| 51 | + * @param int $int |
|
| 52 | + * @return boolean |
|
| 53 | + */ |
|
| 54 | + private function filterInteger($int) { |
|
| 55 | + if (is_int($int)) { |
|
| 56 | + return $int; |
|
| 57 | + } |
|
| 58 | + throw new \Exception("Invalid Integer", 1); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Make sure string is not null or empty |
|
| 63 | + * @param null/string $str |
|
| 64 | + * @return string/exception |
|
| 65 | + */ |
|
| 66 | + private function filterString($str) { |
|
| 67 | + if (strlen($str) > 0 && !is_null($str)) { |
|
| 68 | + return $str; |
|
| 69 | + } |
|
| 70 | + throw new \Exception("String required", 1); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Make Sure Date in string with correct format state |
|
| 75 | + * |
|
| 76 | + * @param \DateTimeInterface|string|int|null $date |
|
| 77 | + * @return string |
|
| 78 | + */ |
|
| 79 | + private function filterDate($date) { |
|
| 80 | + if (empty($date)) { |
|
| 81 | + $date = new \DateTime("now", new \DateTimeZone("Asia/Jakarta")); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + if (is_string($date) || is_int($date)) { |
|
| 85 | + $date = new \DateTime($date); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + return $this->formatDate($date); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Get value of array based on attributes(keys) |
|
| 93 | + * @param supported php variables $attribute |
|
| 94 | + * @param array $data |
|
| 95 | + * @return supported php variables |
|
| 96 | + */ |
|
| 97 | + private function getValue($attribute, $data) { |
|
| 98 | + if (isset($data[$attribute])) { |
|
| 99 | + return $data[$attribute]; |
|
| 100 | + } |
|
| 101 | + return null; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * format date into required format |
|
| 106 | + * |
|
| 107 | + * @param \DateTimeInterface $date |
|
| 108 | + * @return string |
|
| 109 | + */ |
|
| 110 | + private function formatDate($date) { |
|
| 111 | + return $date->format("Y-m-d H:i:s"); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Convert JSON string to associative array |
|
| 116 | + * @param string $jsonResponse |
|
| 117 | + * @return array if it is valid json, null otherwise |
|
| 118 | + */ |
|
| 119 | + public function jsonToArray($jsonResponse) { |
|
| 120 | + try { |
|
| 121 | + return json_decode($jsonResponse, true); |
|
| 122 | + } catch (\Exception $e) { |
|
| 123 | + return false; |
|
| 124 | + } |
|
| 125 | + } |
|
| 126 | 126 | } |
@@ -4,14 +4,16 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | use One\Model\Article; |
| 6 | 6 | |
| 7 | -class FormatMapping { |
|
| 7 | +class FormatMapping |
|
| 8 | +{ |
|
| 8 | 9 | |
| 9 | 10 | /** |
| 10 | 11 | * map a single article to main attributes in Article Class |
| 11 | 12 | * @param string $singleJsonArticle JSON response |
| 12 | 13 | * @return Article\Exception |
| 13 | 14 | */ |
| 14 | - public function article($singleJsonArticle) { |
|
| 15 | + public function article($singleJsonArticle) |
|
| 16 | + { |
|
| 15 | 17 | if ($this->jsonToArray($singleJsonArticle)) { |
| 16 | 18 | $dataArticle = $this->jsonToArray($singleJsonArticle)['data']; |
| 17 | 19 | |
@@ -51,7 +53,8 @@ discard block |
||
| 51 | 53 | * @param int $int |
| 52 | 54 | * @return boolean |
| 53 | 55 | */ |
| 54 | - private function filterInteger($int) { |
|
| 56 | + private function filterInteger($int) |
|
| 57 | + { |
|
| 55 | 58 | if (is_int($int)) { |
| 56 | 59 | return $int; |
| 57 | 60 | } |
@@ -63,7 +66,8 @@ discard block |
||
| 63 | 66 | * @param null/string $str |
| 64 | 67 | * @return string/exception |
| 65 | 68 | */ |
| 66 | - private function filterString($str) { |
|
| 69 | + private function filterString($str) |
|
| 70 | + { |
|
| 67 | 71 | if (strlen($str) > 0 && !is_null($str)) { |
| 68 | 72 | return $str; |
| 69 | 73 | } |
@@ -76,7 +80,8 @@ discard block |
||
| 76 | 80 | * @param \DateTimeInterface|string|int|null $date |
| 77 | 81 | * @return string |
| 78 | 82 | */ |
| 79 | - private function filterDate($date) { |
|
| 83 | + private function filterDate($date) |
|
| 84 | + { |
|
| 80 | 85 | if (empty($date)) { |
| 81 | 86 | $date = new \DateTime("now", new \DateTimeZone("Asia/Jakarta")); |
| 82 | 87 | } |
@@ -94,7 +99,8 @@ discard block |
||
| 94 | 99 | * @param array $data |
| 95 | 100 | * @return supported php variables |
| 96 | 101 | */ |
| 97 | - private function getValue($attribute, $data) { |
|
| 102 | + private function getValue($attribute, $data) |
|
| 103 | + { |
|
| 98 | 104 | if (isset($data[$attribute])) { |
| 99 | 105 | return $data[$attribute]; |
| 100 | 106 | } |
@@ -107,7 +113,8 @@ discard block |
||
| 107 | 113 | * @param \DateTimeInterface $date |
| 108 | 114 | * @return string |
| 109 | 115 | */ |
| 110 | - private function formatDate($date) { |
|
| 116 | + private function formatDate($date) |
|
| 117 | + { |
|
| 111 | 118 | return $date->format("Y-m-d H:i:s"); |
| 112 | 119 | } |
| 113 | 120 | |
@@ -116,7 +123,8 @@ discard block |
||
| 116 | 123 | * @param string $jsonResponse |
| 117 | 124 | * @return array if it is valid json, null otherwise |
| 118 | 125 | */ |
| 119 | - public function jsonToArray($jsonResponse) { |
|
| 126 | + public function jsonToArray($jsonResponse) |
|
| 127 | + { |
|
| 120 | 128 | try { |
| 121 | 129 | return json_decode($jsonResponse, true); |
| 122 | 130 | } catch (\Exception $e) { |