From 3ce030d92f3ac58362d56b34aae11c115a4bbb9c Mon Sep 17 00:00:00 2001 From: Aleksandr Galchenkov Date: Tue, 20 Oct 2020 11:06:33 +0300 Subject: [PATCH] Update current blocks and actions --- composer.json | 10 +- src/Chatium/Actions.php | 219 +++++++++++++++++++++-- src/Chatium/Blocks.php | 357 ++++++++++++++++++++++++++++++++++++-- src/Chatium/Responses.php | 24 +-- src/Chatium/Types.php | 232 +++++++++++++++++++++++++ tests/index.php | 29 +++- 6 files changed, 821 insertions(+), 50 deletions(-) create mode 100644 src/Chatium/Types.php diff --git a/composer.json b/composer.json index b7d7fd4..cf82313 100644 --- a/composer.json +++ b/composer.json @@ -14,18 +14,20 @@ ], "minimum-stability": "dev", "require": { - "php": ">=5.3.0", - "firebase/php-jwt": "dev-master" + "php": ">=7", + "firebase/php-jwt": "dev-master", + "ext-json": "*" }, "autoload": { "psr-0": { "Chatium": "src/" }, "files": [ + "src/Chatium/Actions.php", "src/Chatium/Blocks.php", + "src/Chatium/Context.php", "src/Chatium/Responses.php", - "src/Chatium/Actions.php", - "src/Chatium/Context.php" + "src/Chatium/Types.php" ] } } diff --git a/src/Chatium/Actions.php b/src/Chatium/Actions.php index 4eab63e..7d6e2df 100644 --- a/src/Chatium/Actions.php +++ b/src/Chatium/Actions.php @@ -1,23 +1,61 @@ 'apiCall', 'url' => $url, 'apiParams' => $apiParams], - isset($options['confirm']) ? ['confirm' => $options['confirm']] : [], - isset($options['sendPlayerState']) ? ['sendPlayerState' => $options['sendPlayerState']] : [], - ); + $action = ['type' => 'apiCall', 'url' => $url, 'apiParams' => $apiParams]; + + if (isset($props['confirm'])) $action['confirm'] = $props['confirm']; + if (isset($props['sendPlayerState'])) $action['sendPlayerState'] = $props['sendPlayerState']; + + return $action; } -function showToast($toast) +/** + * @param string $filePutUrl + * @param array $props + * @return array + */ +function attachMedia(string $filePutUrl, array $props = []) { - return [ - 'type' => 'showToast', - 'toast' => $toast, - ]; + $action = ['type' => 'attachMedia', 'filePutUrl' => $filePutUrl]; + + if (isset($props['menuTitle'])) $action['menuTitle'] = $props['menuTitle']; + if (isset($props['progressTitle'])) $action['progressTitle'] = $props['progressTitle']; + if (isset($props['multiple'])) $action['multiple'] = $props['multiple']; + if (isset($props['mediaType'])) $action['mediaType'] = $props['mediaType']; + if (isset($props['submitUrl'])) $action['submitUrl'] = $props['submitUrl']; + if (isset($props['file'])) $action['file'] = $props['file']; + + return $action; } -function copyToClipboard($value) +/** + * @return array + */ +function confirmEmail() +{ + return ['type' => 'confirmEmail']; +} + +/** + * @return array + */ +function confirmPhone() +{ + return ['type' => 'confirmPhone']; +} + +/** + * @param string $value + * @return array + */ +function copyToClipboard(string $value) { return [ 'type' => 'copyToClipboard', @@ -25,12 +63,165 @@ function copyToClipboard($value) ]; } -function updateCurrentScreenBlock($blockId, $data, $merge = true) +/** + * @return array + */ +function goBack() +{ + return ['type' => 'goBack']; +} + +/** + * @param string $url + * @param array $props + * @return array + */ +function navigate(string $url, array $props = []) +{ + $action = ['type' => 'navigate', 'url' => $url]; + + if (isset($props['replace'])) $action['replace'] = $props['replace']; + if (isset($props['openInExternalApp'])) $action['openInExternalApp'] = $props['openInExternalApp']; + if (isset($props['openInModalScreen'])) $action['openInModalScreen'] = $props['openInModalScreen']; + if (isset($props['fullScreenModal'])) $action['fullScreenModal'] = $props['fullScreenModal']; + if (isset($props['openInCurrentScreen'])) $action['openInCurrentScreen'] = $props['openInCurrentScreen']; + if (isset($props['openInBrowser'])) $action['openInBrowser'] = $props['openInBrowser']; + if (isset($props['resetStack'])) $action['resetStack'] = $props['resetStack']; + + return $action; +} + +/** + * @param string $galleryId + * @return array + */ +function nextSlide(string $galleryId) +{ + return [ + 'type' => 'gallery:nextSlide', + 'galleryId' => $galleryId, + ]; +} + +/** + * @return array + */ +function noop() +{ + return ['type' => 'noop']; +} + +/** + * @param array $media + * @return array + */ +function preloadMedia(array $media) +{ + return [ + 'type' => 'preloadMedia', + 'media' => $media, + ]; +} + +/** + * @return array + */ +function refresh() +{ + return ['type' => 'refresh']; +} + +/** + * @param string $token + * @param int $amount + * @param string $description + * @param string $integration + * @param array $payload + * @return array + */ +function requestPayment(string $token, int $amount, string $description, string $integration, array $payload = []) +{ + return [ + 'type' => 'requestPayment', + 'token' => $token, + 'amount' => $amount, + 'description' => $description, + 'integration' => $integration, + 'payload' => $payload, + ]; +} + +/** + * @return array + */ +function resetSearch() +{ + return ['type' => 'resetSearch']; +} + +/** + * @param string $url + * @return array + */ +function selectContacts(string $url) +{ + return ['type' => 'selectContacts', 'url' => $url]; +} + +/** + * @param array $menu + * @return array + */ +function showContextMenu(array $menu) +{ + return ['type' => 'showContextMenu', 'menu' => $menu]; +} + +/** + * @param string $submitUrl + * @param array $props + * @return array + */ +function showTextDialog(string $submitUrl, array $props) +{ + $type = [ + 'type' => 'showTextDialog', + 'inputType' => 'text', + 'submitUrl' => $submitUrl + ]; + + if (isset($props['title'])) $type['title'] = $props['title']; + if (isset($props['description'])) $type['description'] = $props['description']; + if (isset($props['submitButtonTitle'])) $type['submitButtonTitle'] = $props['submitButtonTitle']; + if (isset($props['cancelButtonTitle'])) $type['cancelButtonTitle'] = $props['cancelButtonTitle']; + + return $type; +} + +/** + * @param string $toast + * @return array + */ +function showToast(string $toast) +{ + return [ + 'type' => 'showToast', + 'toast' => $toast, + ]; +} + +/** + * @param string $blockId + * @param array $update + * @param bool $merge + * @return array + */ +function updateCurrentScreenBlock(string $blockId, array $update, bool $merge = true) { return [ 'type' => 'updateCurrentScreenBlock', 'blockId' => $blockId, - 'data' => $data, + 'update' => $update, 'merge' => $merge, ]; } diff --git a/src/Chatium/Blocks.php b/src/Chatium/Blocks.php index 7650d19..8394675 100644 --- a/src/Chatium/Blocks.php +++ b/src/Chatium/Blocks.php @@ -1,26 +1,351 @@ 'audio']; + + requiredField($block['type'], 'downloadUrl', $props); + + $block['downloadUrl'] = $props['downloadUrl']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['title'])) $block['title'] = $props['title']; + if (isset($props['description'])) $block['description'] = $props['description']; + if (isset($props['fileHash'])) $block['fileHash'] = $props['fileHash']; + if (isset($props['durationSeconds'])) $block['durationSeconds'] = $props['durationSeconds']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return string[] + */ +function Button(array $props, array $blocks = []) { - return [ - 'title' => $props['title'], - 'blocks' => $blocks, + $block = ['type' => 'button']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['bgColor'])) $block['bgColor'] = $props['bgColor']; + if (isset($props['fontColor'])) $block['fontColor'] = $props['fontColor']; + if (isset($props['fontSize'])) $block['fontSize'] = $props['fontSize']; + if (isset($props['icon'])) $block['icon'] = $props['icon']; + if (isset($props['iconPosition'])) $block['iconPosition'] = $props['iconPosition']; + if (isset($props['onClick'])) $block['onClick'] = $props['onClick']; + if (isset($props['title'])) $block['title'] = $props['title']; + if (isset($props['buttonType'])) $block['buttonType'] = $props['buttonType']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return array + */ +function Footer(array $props, array $blocks = []) +{ + $block = ['type' => 'footer', 'visibleAlways' => false]; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['visibleAlways'])) $block['visibleAlways'] = $props['visibleAlways']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return array + */ +function Gallery(array $props, array $blocks = []) +{ + $block = ['type' => 'gallery']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['onClick'])) $block['onClick'] = $props['onClick']; + if (isset($props['initialSlideIndex'])) $block['initialSlideIndex'] = $props['initialSlideIndex']; + if (isset($props['slides'])) $block['slides'] = $props['slides']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return string[] + * @throws Exception + */ +function Header(array $props, array $blocks = []) +{ + $block = ['type' => 'header']; + + requiredField($block['type'], 'compact', $props); + + $block['compact'] = $props['compact']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['title'])) $block['title'] = $props['title']; + if (isset($props['description'])) $block['description'] = $props['description']; + if (isset($props['logo'])) $block['logo'] = $props['logo']; + if (isset($props['bottomGradientColors'])) $block['bottomGradientColors'] = $props['bottomGradientColors']; + if (isset($props['topGradientColors'])) $block['topGradientColors'] = $props['topGradientColors']; + if (isset($props['image'])) $block['image'] = $props['image']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return string[] + * @throws Exception + */ +function HorizontalCards(array $props, array $blocks = []) +{ + $block = ['type' => 'horizontalCards']; + + requiredField($block['type'], 'size', $props); + requiredField($block['type'], 'shape', $props); + requiredField($block['type'], 'textPosition', $props); + requiredField($block['type'], 'cards', $props); + + $block['size'] = $props['size']; + $block['size'] = $props['size']; + $block['textPosition'] = $props['textPosition']; + $block['cards'] = $props['cards']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['initialCardId'])) $block['initialCardId'] = $props['initialCardId']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return array + * @throws Exception + */ +function Image(array $props, array $blocks = []) +{ + $block = ['type' => 'image']; + + requiredField($block['type'], 'downloadUrl', $props); + + $block['downloadUrl'] = $props['downloadUrl']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['onClick'])) $block['onClick'] = $props['onClick']; + if (isset($props['width'])) $block['width'] = $props['width']; + if (isset($props['image'])) $block['image'] = $props['image']; + if (isset($props['resizeMode'])) $block['resizeMode'] = $props['resizeMode']; + if (isset($props['imageSize'])) $block['imageSize'] = $props['imageSize']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return array + * @throws Exception + */ +function ListItem(array $props, array $blocks = []) +{ + $block = ['type' => 'screen']; + + requiredField($block['type'], 'title', $props); + + $block['title'] = $props['title']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['description'])) $block['description'] = $props['description']; + if (isset($props['image'])) $block['image'] = $props['image']; + if (isset($props['imageSize'])) $block['imageSize'] = $props['imageSize']; + if (isset($props['logo'])) $block['logo'] = $props['logo']; + if (isset($props['onClick'])) $block['onClick'] = $props['onClick']; + if (isset($props['status'])) $block['status'] = $props['status']; + if (isset($props['titleColor'])) $block['titleColor'] = $props['titleColor']; + if (isset($props['updatedAtTimestamp'])) $block['updatedAtTimestamp'] = $props['updatedAtTimestamp']; + if (isset($props['unreadBullet'])) $block['unreadBullet'] = $props['unreadBullet']; + if (isset($props['active'])) $block['active'] = $props['active']; + if (isset($props['upTitle'])) $block['upTitle'] = $props['upTitle']; + if (isset($props['upTitleColor'])) $block['upTitleColor'] = $props['upTitleColor']; + if (isset($props['url'])) $block['url'] = $props['url']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return array + * @throws Exception + */ +function Search(array $props, array $blocks = []) +{ + $block = [ + 'type' => 'search', + 'bgColor' => '#ffffff', + 'borderColor' => '#9a9a9a', + 'delayMs' => 500, + 'fontColor' => '#000000', + 'minLength' => 3, + 'placeholderTextColor' => '#9a9a9a', + 'spinnerColor' => '#9a9a9a', ]; + + requiredField($block['type'], 'queryParamKey', $props); + + $block['queryParamKey'] = $props['queryParamKey']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['bgColor'])) $block['bgColor'] = $props['bgColor']; + if (isset($props['borderColor'])) $block['borderColor'] = $props['borderColor']; + if (isset($props['defaultValue'])) $block['defaultValue'] = $props['defaultValue']; + if (isset($props['delayMs'])) $block['delayMs'] = $props['delayMs']; + if (isset($props['fontColor'])) $block['fontColor'] = $props['fontColor']; + if (isset($props['inputPlaceholder'])) $block['inputPlaceholder'] = $props['inputPlaceholder']; + if (isset($props['leftIcon'])) $block['leftIcon'] = $props['leftIcon']; + if (isset($props['minLength'])) $block['minLength'] = $props['minLength']; + if (isset($props['placeholderTextColor'])) $block['placeholderTextColor'] = $props['placeholderTextColor']; + if (isset($props['spinnerColor'])) $block['spinnerColor'] = $props['spinnerColor']; + + return $block; } -function Text($props, array $blocks = []) { - return array_merge( - ['type' => 'text'], - $props, - count($blocks) > 0 ? ['blocks' => $blocks] : [] - ); +/** + * @param array $props + * @param array $blocks + * @return string[] + * @throws Exception + */ +function Text(array $props, array $blocks = []) +{ + $block = ['type' => 'text']; + + if ((!isset($props['text']) && !isset($props['tokens'])) + || (isset($props['text']) && isset($props['tokens']))) { + throw new Exception("'text' or 'tokens' is required text in block props."); + } + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['text'])) $block['text'] = $props['text']; + if (isset($props['tokens'])) $block['tokens'] = $props['tokens']; + if (isset($props['onClick'])) $block['onClick'] = $props['onClick']; + + if (isset($props['color'])) $block['color'] = $props['color']; + if (isset($props['fontSize'])) $block['fontSize'] = $props['fontSize']; + if (isset($props['styles'])) $block['styles'] = $props['styles']; + if (isset($props['isBold'])) $block['isBold'] = $props['isBold']; + if (isset($props['lineHeight'])) $block['lineHeight'] = $props['lineHeight']; + + return $block; } -function Button($props, array $blocks = []) +/** + * @param array $props + * @param array $blocks + * @return string[] + * @throws Exception + */ +function Video(array $props, array $blocks = []) { - return array_merge( - ['type' => 'button'], - $props, - count($blocks) > 0 ? ['blocks' => $blocks] : [] - ); + $block = ['type' => 'video']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['hlsUrl'])) $block['hlsUrl'] = $props['hlsUrl']; + if (isset($props['imageUrl'])) $block['imageUrl'] = $props['imageUrl']; + if (isset($props['mp4Url'])) $block['mp4Url'] = $props['mp4Url']; + if (isset($props['videoAspectRatio'])) $block['videoAspectRatio'] = $props['videoAspectRatio']; + + if (isset($props['url'])) $block['url'] = $props['url']; + if (isset($props['file'])) $block['file'] = $props['file']; + + return $block; +} + +/** + * @param array $props + * @param array $blocks + * @return string[] + * @throws Exception + */ +function InlineVideo(array $props, array $blocks = []) +{ + $block = ['type' => 'inlineVideo', 'ignoreSilentSwitch' => 'ignore']; + + if (isset($props['videoSize']) && $props['videoSize']['height'] > $props['videoSize']['width']) { + $block['resizeMode'] = 'cover'; + } + + requiredField($block['type'], 'url', $props); + + $block['url'] = $props['url']; + + commonBlockProps($block, $props); + + if (count($blocks) > 0) $props['blocks'] = $blocks; + if (isset($props['hlsUrl'])) $block['hlsUrl'] = $props['hlsUrl']; + if (isset($props['imageUrl'])) $block['imageUrl'] = $props['imageUrl']; + if (isset($props['mp4Url'])) $block['mp4Url'] = $props['mp4Url']; + if (isset($props['videoAspectRatio'])) $block['videoAspectRatio'] = $props['videoAspectRatio']; + if (isset($props['ignoreSilentSwitch'])) $block['ignoreSilentSwitch'] = $props['ignoreSilentSwitch']; + if (isset($props['muted'])) $block['muted'] = $props['muted']; + if (isset($props['onProgressChange'])) $block['onProgressChange'] = $props['onProgressChange']; + if (isset($props['overlay'])) $block['overlay'] = $props['overlay']; + if (isset($props['onVideoEnd'])) $block['onVideoEnd'] = $props['onVideoEnd']; + if (isset($props['paused'])) $block['paused'] = $props['paused']; + if (isset($props['playInBackground'])) $block['playInBackground'] = $props['playInBackground']; + if (isset($props['playWhenInactive'])) $block['playWhenInactive'] = $props['playWhenInactive']; + if (isset($props['repeat'])) $block['repeat'] = $props['repeat']; + if (isset($props['resizeMode'])) $block['resizeMode'] = $props['resizeMode']; + if (isset($props['showControls'])) $block['showControls'] = $props['showControls']; + if (isset($props['showTimer'])) $block['showTimer'] = $props['showTimer']; + if (isset($props['videoSize'])) $block['videoSize'] = $props['videoSize']; + + return $block; } diff --git a/src/Chatium/Responses.php b/src/Chatium/Responses.php index be99489..544fc5f 100644 --- a/src/Chatium/Responses.php +++ b/src/Chatium/Responses.php @@ -1,17 +1,21 @@ $data, - 'success' => true, - ]; + $response = ['success' => true, 'data' => $data]; + + if ($screens) $response['appScreens'] = $screens; + if ($preloadMedia) $response['preloadMedia'] = $preloadMedia; + + return $response; } -function ApiCallResponse($data) +function ApiCallResponse($data, $screens = null) { - return [ - 'appAction' => $data, - 'success' => true, - ]; + $response = ['success' => true]; + + if ($data) $response['appAction'] = $data; + if ($screens) $response['appScreens'] = $screens; + + return $response; } diff --git a/src/Chatium/Types.php b/src/Chatium/Types.php new file mode 100644 index 0000000..07844f7 --- /dev/null +++ b/src/Chatium/Types.php @@ -0,0 +1,232 @@ + $props['title'], + 'onClick' => $props['onClick'], + ]; +} + +/** + * @param $props + * @return string[] + */ +function Icon($props) +{ + $block = []; + + if (isset($props['appIcon'])) $block['appIcon'] = $props['appIcon']; + if (isset($props['iconCssClass'])) $block['iconCssClass'] = $props['iconCssClass']; + if (isset($props['bgColor'])) $block['bgColor'] = $props['bgColor']; + if (isset($props['color'])) $block['color'] = $props['color']; + if (isset($props['image'])) $block['image'] = $props['image']; + if (isset($props['imageSize'])) $block['imageSize'] = $props['imageSize']; + if (isset($props['shape'])) $block['shape'] = $props['shape']; + if (isset($props['text'])) $block['text'] = $props['text']; + + return $block; +} + +/** + * @param $props + * @return string[] + * @throws Exception + */ +function StatusIcon($props) +{ + $block = Icon($props); + + requiredField('StatusIcon', 'isAvailable', $props); + + $block['isAvailable'] = $props['isAvailable']; + + return $block; +} + +/** + * @param $props + * @return array + * @throws Exception + */ +function Author($props) +{ + $block = []; + + requiredField('Author', 'id', $props); + requiredField('Author', 'name', $props); + requiredField('Author', 'avatar', $props); + + $block['id'] = $props['id']; + $block['name'] = $props['name']; + $block['avatar'] = $props['avatar']; + + if (isset($props['onClick'])) $block['onClick'] = $props['onClick']; + + return $block; +} + +/** + * @param $props + * @return array + * @throws Exception + */ +function HeaderButton($props) +{ + $block = []; + + if (isset($props['icon'])) $block['icon'] = $props['icon']; + if (isset($props['onClick'])) $block['onClick'] = $props['onClick']; + + return $block; +} + +/** + * @param $props + * @return array + * @throws Exception + */ +function Chat($props) +{ + $block = []; + + requiredField('Chat', 'messages_get_url', $props); + requiredField('Chat', 'messages_edit_url', $props); + requiredField('Chat', 'messages_delete_url', $props); + requiredField('Chat', 'messages_changes_url', $props); + requiredField('Chat', 'messages_react_url', $props); + requiredField('Chat', 'last_message_id', $props); + requiredField('Chat', 'last_read_message_id', $props); + requiredField('Chat', 'support_paging', $props); + requiredField('Chat', 'files_put_url', $props); + requiredField('Chat', 'reply_quotes_enabled', $props); + requiredField('Chat', 'current_author', $props); + requiredField('Chat', 'group_author', $props); + requiredField('Chat', 'messages_socket_id', $props); + requiredField('Chat', 'reactions_socket_id', $props); + requiredField('Chat', 'last_read_at', $props); + requiredField('Chat', 'last_read_socket_id', $props); + + $block['messages_get_url'] = $props['messages_get_url']; + $block['messages_edit_url'] = $props['messages_edit_url']; + $block['messages_delete_url'] = $props['messages_delete_url']; + $block['messages_changes_url'] = $props['messages_changes_url']; + $block['messages_react_url'] = $props['messages_react_url']; + $block['last_read_message_id'] = $props['last_read_message_id']; + $block['support_paging'] = $props['support_paging']; + $block['files_put_url'] = $props['files_put_url']; + $block['reply_quotes_enabled'] = $props['reply_quotes_enabled']; + $block['current_author'] = $props['current_author'] === null ?: Author($props['current_author']); + $block['group_author'] = $props['group_author'] === null ?: Author($props['group_author']); + $block['messages_socket_id'] = $props['messages_socket_id']; + $block['reactions_socket_id'] = $props['reactions_socket_id']; + $block['last_read_at'] = $props['last_read_at']; + $block['last_read_socket_id'] = $props['last_read_socket_id']; + + if (isset($props['messages_add_url'])) $block['messages_add_url'] = $props['messages_add_url']; + if (isset($props['mark_as_read_url'])) $block['mark_as_read_url'] = $props['mark_as_read_url']; + if (isset($props['last_read_get_url'])) $block['last_read_get_url'] = $props['last_read_get_url']; + if (isset($props['typing_socket_data'])) $block['typing_socket_data'] = $props['typing_socket_data']; + if (isset($props['pinned'])) $block['pinned'] = $props['pinned']; + if (isset($props['render_inverted'])) $block['render_inverted'] = $props['render_inverted']; + if (isset($props['on_context_api_call_url'])) $block['on_context_api_call_url'] = $props['on_context_api_call_url']; + + return $block; +} + +/** + * @param $props + * @param array $blocks + * @return array|string[] + * @throws Exception + */ +function Screen($props, array $blocks = []) +{ + $block = []; + + requiredField('Screen', 'title', $props); + + $block['title'] = $props['title']; + if (count($blocks) > 0) $block['blocks'] = array_filter($blocks, function ($block) { + return isset($block['type']) && $block['type'] !== 'header' && $block['type'] !== 'search' && $block['type'] !== 'footer'; + }); + + if (isset($props['description'])) $block['description'] = $props['description']; + if (isset($props['backUrl'])) $block['backUrl'] = $props['backUrl']; + if (isset($props['header'])) $block['header'] = Header($props['header']); + if (isset($props['headerButton'])) $block['headerButton'] = HeaderButton($props['headerButton']); + if (isset($props['headerLeftButton'])) $block['headerLeftButton'] = HeaderButton($props['headerLeftButton']); + if (isset($props['contextLinks'])) $block['contextLinks'] = $props['contextLinks']; + if (isset($props['socketId'])) $block['socketId'] = $props['socketId']; + if (isset($props['socketIds'])) $block['socketIds'] = $props['socketIds']; + if (isset($props['dropdownMenuBlocks'])) $block['dropdownMenuBlocks'] = $props['dropdownMenuBlocks']; + if (isset($props['dropdownMenuInitiallyVisible'])) $block['dropdownMenuInitiallyVisible'] = $props['dropdownMenuInitiallyVisible']; + if (isset($props['pinnedBlocks'])) $block['pinnedBlocks'] = $props['pinnedBlocks']; + if (isset($props['chat'])) $block['chat'] = Chat($props['chat']); + if (isset($props['fullScreenGallery'])) $block['fullScreenGallery'] = $props['fullScreenGallery']; + if (isset($props['footer'])) $block['footer'] = Footer($props['footer']); + if (isset($props['search'])) $block['search'] = Search($props['search']); + if (isset($props['needEmailCheck'])) $block['needEmailCheck'] = $props['needEmailCheck']; + if (isset($props['needPhoneCheck'])) $block['needPhoneCheck'] = $props['needPhoneCheck']; + if (isset($props['scrollTo'])) $block['scrollTo'] = $props['scrollTo']; + if (isset($props['logo'])) $block['logo'] = Icon($props['logo']); + if (isset($props['layout'])) $block['layout'] = $props['layout']; + if (isset($props['bgColor'])) $block['bgColor'] = $props['bgColor']; + + $headerBlocks = array_filter($blocks, function ($block) { + return isset($block['type']) && $block['type'] === 'header'; + }); + if (count($headerBlocks) === 1) $block['header'] = $headerBlocks[0]; + + $searchBlocks = array_filter($blocks, function ($block) { + return isset($block['type']) && $block['type'] === 'search'; + }); + if (count($searchBlocks) === 1) $block['search'] = $searchBlocks[0]; + + $footerBlocks = array_filter($blocks, function ($block) { + return isset($block['type']) && $block['type'] === 'footer'; + }); + if (count($footerBlocks) === 1) $block['footer'] = $footerBlocks[0]; + + return $block; +} diff --git a/tests/index.php b/tests/index.php index ed5a83b..ef01c80 100644 --- a/tests/index.php +++ b/tests/index.php @@ -4,19 +4,36 @@ require_once __DIR__ . '/../vendor/autoload.php'; use function Chatium\Responses\ScreenResponse; +use function Chatium\Types\Screen; +use function Chatium\Types\ContextMenuItem; +use function Chatium\Actions\apiCall; +use function Chatium\Actions\refresh; +use function Chatium\Actions\showContextMenu; use function Chatium\Actions\showToast; use function Chatium\Blocks\Button; -use function Chatium\Blocks\Screen; use function Chatium\Blocks\Text; +use function Chatium\Blocks\Image; + +function AweText(string $text, array $props = []) { return Text(array_merge(['text' => $text], $props)); } +function AweImage(string $url, array $props = []) { return Image(array_merge(['downloadUrl' => $url], $props)); } +function AweButton(string $title, array $onClick, array $props = []) { return Button(array_merge(['title' => $title, 'onClick' => $onClick], $props)); } +function AweContextMenu(string $title, array $onClick) { return ContextMenuItem(['title' => $title, 'onClick' => $onClick]); } echo json_encode( ScreenResponse( Screen(['title' => 'Hello!'], [ - Text(['text' => 'Hello World!']), - Button([ - 'title' => 'HelloWorld', - 'onClick' => showToast('Clicked!') - ]) + AweImage('http://yandex.ru'), + AweText('Hello World!'), + AweButton('Press to showToast', [showToast('showToast!')]), + AweButton('Press to apiCall', [apiCall('showToast!')]), + AweButton('Press to refresh', [refresh()]), + AweButton('Press to showContext', [ + showContextMenu([ + AweContextMenu('Title 1', [showToast('Click 1!')]), + AweContextMenu('Title 2', [showToast('Click 2!')]), + AweContextMenu('Title 3', [showToast('Click 3!')]), + ]), + ]), ]) ) );