KLYgarage /
onesdk
| 1 | <?php declare(strict_types=1); |
||||
| 2 | |||||
| 3 | namespace One; |
||||
| 4 | |||||
| 5 | use One\Model\Article; |
||||
| 6 | |||||
| 7 | /** |
||||
| 8 | * FactoryArticle Class |
||||
| 9 | * |
||||
| 10 | * @method create |
||||
| 11 | * @method createArticle |
||||
| 12 | * @method validateArray |
||||
| 13 | * @method validateUrl |
||||
| 14 | * @method validateInteger |
||||
| 15 | * @method validateString |
||||
| 16 | * @method checkData |
||||
| 17 | */ |
||||
| 18 | class FactoryArticle |
||||
| 19 | { |
||||
| 20 | /** |
||||
| 21 | * Create article |
||||
| 22 | */ |
||||
| 23 | public static function create(array $data): \One\Model\Article |
||||
| 24 | { |
||||
| 25 | $data = self::validateArray($data); |
||||
| 26 | $title = self::validateString( |
||||
| 27 | (string) self::checkData($data, 'title', '') |
||||
| 28 | ); |
||||
| 29 | $body = self::validateString( |
||||
| 30 | (string) self::checkData($data, 'body', '') |
||||
| 31 | ); |
||||
| 32 | $source = self::validateUrl( |
||||
| 33 | (string) self::checkData($data, 'source', '') |
||||
| 34 | ); |
||||
| 35 | $uniqueId = self::validateString( |
||||
| 36 | (string) self::checkData($data, 'unique_id', '') |
||||
| 37 | ); |
||||
| 38 | $typeId = self::validateInteger( |
||||
| 39 | (int) self::checkData($data, 'type_id', '') |
||||
| 40 | ); |
||||
| 41 | $categoryId = self::validateInteger( |
||||
| 42 | (int) self::checkData($data, 'category_id', '') |
||||
| 43 | ); |
||||
| 44 | $reporter = self::validateString( |
||||
| 45 | (string) self::checkData($data, 'reporter', '') |
||||
| 46 | ); |
||||
| 47 | $lead = self::validateString( |
||||
| 48 | (string) self::checkData($data, 'lead', '') |
||||
| 49 | ); |
||||
| 50 | $tags = self::validateString( |
||||
| 51 | (string) self::checkData($data, 'tags', '') |
||||
| 52 | ); |
||||
| 53 | $publishedAt = self::validateString( |
||||
| 54 | (string) self::checkData($data, 'published_at', '') |
||||
| 55 | ); |
||||
| 56 | $headline = (bool) self::checkData($data, 'headline', false); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 57 | $headlineLip6 = (bool) self::checkData($data, 'headline_lip6', false); |
||||
| 58 | $aiType = self::validateInteger( |
||||
| 59 | (int) self::checkData($data, 'ai_type', 0) |
||||
| 60 | ); |
||||
| 61 | $identifier = self::validateInteger( |
||||
| 62 | (int) self::checkData($data, 'identifier', null) |
||||
| 63 | ); |
||||
| 64 | return self::createArticle( |
||||
| 65 | $title, |
||||
| 66 | $body, |
||||
| 67 | $source, |
||||
| 68 | $uniqueId, |
||||
| 69 | $typeId, |
||||
| 70 | $categoryId, |
||||
| 71 | $reporter, |
||||
| 72 | $lead, |
||||
| 73 | $tags, |
||||
| 74 | $publishedAt, |
||||
| 75 | $headline, |
||||
| 76 | $headlineLip6, |
||||
| 77 | $aiType, |
||||
| 78 | $identifier |
||||
| 79 | ); |
||||
| 80 | } |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * Create Article Object |
||||
| 84 | * |
||||
| 85 | * @return Article Object |
||||
| 86 | */ |
||||
| 87 | public static function createArticle( |
||||
| 88 | string $title, |
||||
| 89 | string $body, |
||||
| 90 | string $source, |
||||
| 91 | string $uniqueId, |
||||
| 92 | int $typeId, |
||||
| 93 | int $categoryId, |
||||
| 94 | string $reporter, |
||||
| 95 | string $lead, |
||||
| 96 | string $tags, |
||||
| 97 | string $publishedAt, |
||||
| 98 | bool $headline, |
||||
| 99 | bool $headlineLip6, |
||||
| 100 | int $aiType, |
||||
| 101 | int $identifier |
||||
| 102 | ): Article { |
||||
| 103 | return new Article( |
||||
| 104 | $title, |
||||
| 105 | $body, |
||||
| 106 | $source, |
||||
| 107 | $uniqueId, |
||||
| 108 | $typeId, |
||||
| 109 | $categoryId, |
||||
| 110 | $reporter, |
||||
| 111 | $lead, |
||||
| 112 | $tags, |
||||
| 113 | $publishedAt, |
||||
| 114 | $identifier, |
||||
| 115 | $headline, |
||||
| 116 | $headlineLip6, |
||||
| 117 | $aiType |
||||
|
0 ignored issues
–
show
$aiType of type integer is incompatible with the type boolean expected by parameter $seo of One\Model\Article::__construct().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 118 | ); |
||||
| 119 | } |
||||
| 120 | |||||
| 121 | /** |
||||
| 122 | * functionality to check whether a variable is set or not. |
||||
| 123 | * @param mixed $key |
||||
| 124 | * @param string $default |
||||
| 125 | * @return mixed |
||||
| 126 | */ |
||||
| 127 | private static function checkData(array $data, $key, $default = '') |
||||
| 128 | { |
||||
| 129 | return $data[$key] ?? $default; |
||||
| 130 | } |
||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * functionality validity for array variables |
||||
| 134 | */ |
||||
| 135 | private static function validateArray(array $var): array |
||||
| 136 | { |
||||
| 137 | if (gettype($var) === 'array') { |
||||
| 138 | return $var; |
||||
| 139 | } |
||||
| 140 | throw new \Exception('The variable type must Array :'); |
||||
| 141 | } |
||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * Make Sure Url in string with correct url format |
||||
| 145 | */ |
||||
| 146 | private static function validateUrl(string $var): string |
||||
| 147 | { |
||||
| 148 | if (filter_var($var, FILTER_VALIDATE_URL) === false) { |
||||
| 149 | throw new \Exception("Invalid url : ${var}"); |
||||
| 150 | } |
||||
| 151 | return $var; |
||||
| 152 | } |
||||
| 153 | |||||
| 154 | /** |
||||
| 155 | * functionality validity for int variables |
||||
| 156 | */ |
||||
| 157 | private static function validateInteger(int $var): int |
||||
| 158 | { |
||||
| 159 | if (filter_var($var, FILTER_VALIDATE_INT) === false) { |
||||
| 160 | throw new \Exception('The variable type must Integer :' . $var); |
||||
| 161 | } |
||||
| 162 | return $var; |
||||
| 163 | } |
||||
| 164 | |||||
| 165 | /** |
||||
| 166 | * functionality validity for string variables |
||||
| 167 | */ |
||||
| 168 | private static function validateString(String $var): String |
||||
| 169 | { |
||||
| 170 | if (gettype($var) === 'string') { |
||||
| 171 | return $var; |
||||
| 172 | } |
||||
| 173 | throw new \Exception('The variable type must String :' . $var); |
||||
| 174 | } |
||||
| 175 | } |
||||
| 176 |