KLYgarage /
onesdk
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | namespace One; |
||
| 4 | |||
| 5 | use One\Model\Livestreaming; |
||
| 6 | |||
| 7 | class FactoryLivestreaming |
||
| 8 | { |
||
| 9 | public static function create(array $data): \One\Model\Livestreaming |
||
| 10 | { |
||
| 11 | $data = self::validateArray($data); |
||
| 12 | $title = self::validateString( |
||
| 13 | (string) self::checkData($data, 'title', '') |
||
| 14 | ); |
||
| 15 | $desc = self::validateString( |
||
| 16 | (string) self::checkData($data, 'desc', '') |
||
| 17 | ); |
||
| 18 | $urlLive = self::validateString( |
||
| 19 | (string) self::checkData($data, 'url_live', '') |
||
| 20 | ); |
||
| 21 | $urlThumbnail = self::validateString( |
||
| 22 | (string) self::checkData($data, 'url_thumbnail', '') |
||
| 23 | ); |
||
| 24 | $publishedAt = self::validateString( |
||
| 25 | (string) self::checkData($data, 'published_at', '') |
||
| 26 | ); |
||
| 27 | $endAt = self::validateString( |
||
| 28 | (string) self::checkData($data, 'end_at', '') |
||
| 29 | ); |
||
| 30 | $publishStatus = (bool) self::checkData($data, 'publish_status', false); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 31 | $uniqueId = self::validateString( |
||
| 32 | (string) self::checkData($data, 'unique_id', '') |
||
| 33 | ); |
||
| 34 | $identifier = self::validateInteger( |
||
| 35 | (int) self::checkData($data, 'identifier', null) |
||
| 36 | ); |
||
| 37 | |||
| 38 | return self::createLivestreaming( |
||
| 39 | $uniqueId, |
||
| 40 | $title, |
||
| 41 | $desc, |
||
| 42 | $urlLive, |
||
| 43 | $urlThumbnail, |
||
| 44 | $publishedAt, |
||
| 45 | $endAt, |
||
| 46 | $publishStatus, |
||
| 47 | $identifier |
||
| 48 | ); |
||
| 49 | } |
||
| 50 | |||
| 51 | public static function createLivestreaming( |
||
| 52 | string $uniqueId, |
||
| 53 | string $title, |
||
| 54 | string $desc, |
||
| 55 | string $urlLive, |
||
| 56 | string $urlThumbnail, |
||
| 57 | string $publishedAt, |
||
| 58 | string $endAt, |
||
| 59 | bool $publishStatus, |
||
| 60 | int $identifier |
||
| 61 | ): Livestreaming { |
||
| 62 | return new Livestreaming( |
||
| 63 | $uniqueId, |
||
| 64 | $title, |
||
| 65 | $desc, |
||
| 66 | $urlLive, |
||
| 67 | $urlThumbnail, |
||
| 68 | $publishedAt, |
||
| 69 | $endAt, |
||
| 70 | $publishStatus, |
||
| 71 | $identifier |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * functionality to check whether a variable is set or not. |
||
| 77 | * @param mixed $key |
||
| 78 | * @param string $default |
||
| 79 | * @return mixed |
||
| 80 | */ |
||
| 81 | private static function checkData(array $data, $key, $default = '') |
||
| 82 | { |
||
| 83 | return $data[$key] ?? $default; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * functionality validity for array variables |
||
| 88 | */ |
||
| 89 | private static function validateArray(array $var): array |
||
| 90 | { |
||
| 91 | if (gettype($var) === 'array') { |
||
| 92 | return $var; |
||
| 93 | } |
||
| 94 | throw new \Exception('The variable type must Array :'); |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Make Sure Url in string with correct url format |
||
| 99 | */ |
||
| 100 | private static function validateUrl(string $var): string |
||
|
0 ignored issues
–
show
|
|||
| 101 | { |
||
| 102 | if (filter_var($var, FILTER_VALIDATE_URL) === false) { |
||
| 103 | throw new \Exception("Invalid url : ${var}"); |
||
| 104 | } |
||
| 105 | return $var; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * functionality validity for string variables |
||
| 110 | */ |
||
| 111 | private static function validateString(String $var): String |
||
| 112 | { |
||
| 113 | if (gettype($var) === 'string') { |
||
| 114 | return $var; |
||
| 115 | } |
||
| 116 | throw new \Exception('The variable type must String :' . $var); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * functionality validity for int variables |
||
| 121 | */ |
||
| 122 | private static function validateInteger(int $var): int |
||
| 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 |