From db7559068ad38f35f3c68f8daf661e90862bd8bc Mon Sep 17 00:00:00 2001 From: Maximilian Beckers Date: Thu, 3 Aug 2017 21:12:29 +0200 Subject: [PATCH] handle request --- .gitignore | 3 ++ LICENSE | 21 +++++++++ README.md | 2 + composer.json | 34 ++++++++++++++ examples/simple-request.php | 19 ++++++++ phpunit.xml.dist | 28 +++++++++++ src/Request/Argument.php | 61 ++++++++++++++++++++++++ src/Request/Capability.php | 28 +++++++++++ src/Request/Conversation.php | 44 ++++++++++++++++++ src/Request/Date.php | 40 ++++++++++++++++ src/Request/DateTime.php | 34 ++++++++++++++ src/Request/Device.php | 28 +++++++++++ src/Request/Input.php | 48 +++++++++++++++++++ src/Request/LatLng.php | 34 ++++++++++++++ src/Request/Location.php | 70 ++++++++++++++++++++++++++++ src/Request/PostalAddress.php | 88 +++++++++++++++++++++++++++++++++++ src/Request/RawInput.php | 47 +++++++++++++++++++ src/Request/Request.php | 64 +++++++++++++++++++++++++ src/Request/Surface.php | 32 +++++++++++++ src/Request/Time.php | 46 ++++++++++++++++++ src/Request/User.php | 57 +++++++++++++++++++++++ src/Request/UserProfile.php | 40 ++++++++++++++++ src/Response/Response.php | 10 ++++ 23 files changed, 878 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 examples/simple-request.php create mode 100644 phpunit.xml.dist create mode 100644 src/Request/Argument.php create mode 100644 src/Request/Capability.php create mode 100644 src/Request/Conversation.php create mode 100644 src/Request/Date.php create mode 100644 src/Request/DateTime.php create mode 100644 src/Request/Device.php create mode 100644 src/Request/Input.php create mode 100644 src/Request/LatLng.php create mode 100644 src/Request/Location.php create mode 100644 src/Request/PostalAddress.php create mode 100644 src/Request/RawInput.php create mode 100644 src/Request/Request.php create mode 100644 src/Request/Surface.php create mode 100644 src/Request/Time.php create mode 100644 src/Request/User.php create mode 100644 src/Request/UserProfile.php create mode 100644 src/Response/Response.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e26f45e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +composer.phar +/vendor/ +composer.lock diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ff69edb --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Maximilian Beckers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..56e0ab9 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# Amazon alexa php library +This library is a helper for google actions with php. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f9b4758 --- /dev/null +++ b/composer.json @@ -0,0 +1,34 @@ +{ + "name": "maxbeckers/google-actions-php", + "type": "library", + "description": "This library is a helper for google actions with php.", + "keywords": [ + "google", + "actions", + "php" + ], + "license": "MIT", + "support": { + "issues": "https://github.com/maxbeckers/google-actions-php/issues", + "source": "https://github.com/maxbeckers/google-actions-php" + }, + "authors": [ + { + "name": "Maximilian Beckers", + "email": "beckers.maximilian@gmail.com", + "role": "Developer" + } + ], + "minimum-stability": "dev", + "require": { + "php": ">=7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.2" + }, + "autoload": { + "psr-4": { + "MaxBeckers\\GoogleActions\\": "src/" + } + } +} diff --git a/examples/simple-request.php b/examples/simple-request.php new file mode 100644 index 0000000..dff94b4 --- /dev/null +++ b/examples/simple-request.php @@ -0,0 +1,19 @@ + + + + + ./test/Tests + + + + + ./src + + ./test + ./vendor + + + + \ No newline at end of file diff --git a/src/Request/Argument.php b/src/Request/Argument.php new file mode 100644 index 0000000..7e1f2ab --- /dev/null +++ b/src/Request/Argument.php @@ -0,0 +1,61 @@ + + */ +class Argument +{ + /** + * @var string|null + */ + public $name; + + /** + * @var string|null + */ + public $rawText; + + /** + * @var bool + */ + public $boolValue = false; + + /** + * @var string|null + */ + public $textValue; + + /** + * @var DateTime|null + */ + public $datetimeValue; + + /** + * TODO + * + * @var array + */ + public $extension; + + /** + * @param array $googleRequest + * + * @return Argument + */ + public static function fromGoogleRequest(array $googleRequest): Argument + { + $argument = new self(); + + $argument->name = isset($googleRequest['name']) ? $googleRequest['name'] : null; + $argument->rawText = isset($googleRequest['rawText']) ? $googleRequest['rawText'] : null; + $argument->boolValue = isset($googleRequest['boolValue']) ? (bool)$googleRequest['boolValue'] : false; + $argument->textValue = isset($googleRequest['textValue']) ? $googleRequest['textValue'] : null; + $argument->datetimeValue = isset($googleRequest['datetimeValue']) ? DateTime::fromGoogleRequest($googleRequest['datetimeValue']) : null; + // TODO + $argument->extension = isset($googleRequest['extension']) ? (array)$googleRequest['extension'] : []; + + return $argument; + } +} diff --git a/src/Request/Capability.php b/src/Request/Capability.php new file mode 100644 index 0000000..2d09591 --- /dev/null +++ b/src/Request/Capability.php @@ -0,0 +1,28 @@ + + */ +class Capability +{ + /** + * @var string|null + */ + public $name; + + /** + * @param array $googleRequest + * + * @return Capability + */ + public static function fromGoogleRequest(array $googleRequest): Capability + { + $capability = new self(); + + $capability->name = isset($googleRequest['name']) ? $googleRequest['name'] : null; + + return $capability; + } +} diff --git a/src/Request/Conversation.php b/src/Request/Conversation.php new file mode 100644 index 0000000..d751b3c --- /dev/null +++ b/src/Request/Conversation.php @@ -0,0 +1,44 @@ + + */ +class Conversation +{ + const TYPE_UNSPECIFIED = 'TYPE_UNSPECIFIED'; + const TYPE_NEW = 'NEW'; + const TYPE_ACTIVE = 'ACTIVE'; + + /** + * @var string|null + */ + public $conversationId; + + /** + * @var string|null + */ + public $type; + + /** + * @var string|null + */ + public $conversationToken; + + /** + * @param array $googleRequest + * + * @return Conversation + */ + public static function fromGoogleRequest(array $googleRequest): Conversation + { + $conversation = new self(); + + $conversation->conversationId = isset($googleRequest['conversationId']) ? $googleRequest['conversationId'] : null; + $conversation->type = isset($googleRequest['type']) ? $googleRequest['type'] : null; + $conversation->conversationToken = isset($googleRequest['conversationToken']) ? $googleRequest['conversationToken'] : null; + + return $conversation; + } +} diff --git a/src/Request/Date.php b/src/Request/Date.php new file mode 100644 index 0000000..555fb22 --- /dev/null +++ b/src/Request/Date.php @@ -0,0 +1,40 @@ + + */ +class Date +{ + /** + * @var int|null + */ + public $year; + + /** + * @var int|null + */ + public $month; + + /** + * @var int|null + */ + public $day; + + /** + * @param array $googleRequest + * + * @return Date + */ + public static function fromGoogleRequest(array $googleRequest): Date + { + $date = new self(); + + $date->year = isset($googleRequest['year']) ? (int)$googleRequest['year'] : null; + $date->month = isset($googleRequest['month']) ? (int)$googleRequest['month'] : null; + $date->day = isset($googleRequest['day']) ? (int)$googleRequest['day'] : null; + + return $date; + } +} diff --git a/src/Request/DateTime.php b/src/Request/DateTime.php new file mode 100644 index 0000000..f3df332 --- /dev/null +++ b/src/Request/DateTime.php @@ -0,0 +1,34 @@ + + */ +class DateTime +{ + /** + * @var Date|null + */ + public $date; + + /** + * @var Time|null + */ + public $time; + + /** + * @param array $googleRequest + * + * @return DateTime + */ + public static function fromGoogleRequest(array $googleRequest): DateTime + { + $dateTime = new self(); + + $dateTime->date = isset($googleRequest['date']) ? Date::fromGoogleRequest($googleRequest['date']) : null; + $dateTime->time = isset($googleRequest['time']) ? Time::fromGoogleRequest($googleRequest['time']) : null; + + return $dateTime; + } +} diff --git a/src/Request/Device.php b/src/Request/Device.php new file mode 100644 index 0000000..d2132eb --- /dev/null +++ b/src/Request/Device.php @@ -0,0 +1,28 @@ + + */ +class Device +{ + /** + * @var Location|null + */ + public $location; + + /** + * @param array $googleRequest + * + * @return Device + */ + public static function fromGoogleRequest(array $googleRequest): Device + { + $device = new self(); + + $device->location = isset($googleRequest['location']) ? Location::fromGoogleRequest($googleRequest['location']) : null; + + return $device; + } +} diff --git a/src/Request/Input.php b/src/Request/Input.php new file mode 100644 index 0000000..59fa1fd --- /dev/null +++ b/src/Request/Input.php @@ -0,0 +1,48 @@ + + */ +class Input +{ + /** + * @var RawInput[] + */ + public $rawInputs = []; + + /** + * @var string|null + */ + public $intent; + + /** + * @var Argument[] + */ + public $arguments = []; + + /** + * @param array $googleRequest + * + * @return Input + */ + public static function fromGoogleRequest(array $googleRequest): Input + { + $input = new self(); + + if(isset($googleRequest['rawInputs'])) { + foreach ($googleRequest['rawInputs'] as $rawInput) { + $input->rawInputs[] = RawInput::fromGoogleRequest($rawInput); + } + } + $input->intent = isset($googleRequest['intent']) ? $googleRequest['intent'] : null; + if(isset($googleRequest['arguments'])) { + foreach ($googleRequest['arguments'] as $argument) { + $input->arguments[] = Argument::fromGoogleRequest($argument); + } + } + + return $input; + } +} diff --git a/src/Request/LatLng.php b/src/Request/LatLng.php new file mode 100644 index 0000000..f9995f7 --- /dev/null +++ b/src/Request/LatLng.php @@ -0,0 +1,34 @@ + + */ +class LatLng +{ + /** + * @var float|null + */ + public $latitude; + + /** + * @var float|null + */ + public $longitude; + + /** + * @param array $googleRequest + * + * @return LatLng + */ + public static function fromGoogleRequest(array $googleRequest): LatLng + { + $latLng = new self(); + + $latLng->latitude = isset($googleRequest['latitude']) ? (float)$googleRequest['latitude'] : null; + $latLng->longitude = isset($googleRequest['longitude']) ? (float)$googleRequest['longitude'] : null; + + return $latLng; + } +} diff --git a/src/Request/Location.php b/src/Request/Location.php new file mode 100644 index 0000000..f3bdb54 --- /dev/null +++ b/src/Request/Location.php @@ -0,0 +1,70 @@ + + */ +class Location +{ + /** + * @var LatLng|null + */ + public $coordinates; + + /** + * @var string|null + */ + public $formattedAddress; + + /** + * @var string|null + */ + public $zipCode; + + /** + * @var string|null + */ + public $city; + + /** + * @var PostalAddress|null + */ + public $postalAddress; + + /** + * @var string|null + */ + public $name; + + /** + * @var string|null + */ + public $phoneNumber; + + /** + * @var string|null + */ + public $notes; + + /** + * @param array $googleRequest + * + * @return Location + */ + public static function fromGoogleRequest(array $googleRequest): Location + { + $location = new self(); + + $location->coordinates = isset($googleRequest['coordinates']) ? LatLng::fromGoogleRequest($googleRequest['coordinates']) : null; + $location->formattedAddress = isset($googleRequest['formattedAddress']) ? $googleRequest['formattedAddress'] : null; + $location->zipCode = isset($googleRequest['zipCode']) ? $googleRequest['zipCode'] : null; + $location->city = isset($googleRequest['city']) ? $googleRequest['city'] : null; + $location->postalAddress = isset($googleRequest['postalAddress']) ? PostalAddress::fromGoogleRequest($googleRequest['postalAddress']) : null; + $location->name = isset($googleRequest['name']) ? $googleRequest['name'] : null; + $location->phoneNumber = isset($googleRequest['phoneNumber']) ? $googleRequest['phoneNumber'] : null; + $location->notes = isset($googleRequest['notes']) ? $googleRequest['notes'] : null; + + return $location; + } +} diff --git a/src/Request/PostalAddress.php b/src/Request/PostalAddress.php new file mode 100644 index 0000000..1b3f8e7 --- /dev/null +++ b/src/Request/PostalAddress.php @@ -0,0 +1,88 @@ + + */ +class PostalAddress +{ + /** + * @var int|null + */ + public $revision; + + /** + * @var string|null + */ + public $regionCode; + + /** + * @var string|null + */ + public $languageCode; + + /** + * @var string|null + */ + public $postalCode; + + /** + * @var string|null + */ + public $sortingCode; + + /** + * @var string|null + */ + public $administrativeArea; + + /** + * @var string|null + */ + public $locality; + + /** + * @var string|null + */ + public $sublocality; + + /** + * @var string[] + */ + public $addressLines = []; + + /** + * @var string[] + */ + public $recipients = []; + + /** + * @var string|null + */ + public $organization; + + /** + * @param array $googleRequest + * + * @return PostalAddress + */ + public static function fromGoogleRequest(array $googleRequest): PostalAddress + { + $postalAddress = new self(); + + $postalAddress->revision = isset($googleRequest['revision']) ? (int)$googleRequest['revision'] : null; + $postalAddress->regionCode = isset($googleRequest['regionCode']) ? $googleRequest['regionCode'] : null; + $postalAddress->languageCode = isset($googleRequest['languageCode']) ? $googleRequest['languageCode'] : null; + $postalAddress->postalCode = isset($googleRequest['postalCode']) ? $googleRequest['postalCode'] : null; + $postalAddress->sortingCode = isset($googleRequest['sortingCode']) ? $googleRequest['sortingCode'] : null; + $postalAddress->administrativeArea = isset($googleRequest['administrativeArea']) ? $googleRequest['administrativeArea'] : null; + $postalAddress->locality = isset($googleRequest['locality']) ? $googleRequest['locality'] : null; + $postalAddress->sublocality = isset($googleRequest['sublocality']) ? $googleRequest['sublocality'] : null; + $postalAddress->addressLines = isset($googleRequest['addressLines']) ? (array)$googleRequest['addressLines'] : []; + $postalAddress->recipients = isset($googleRequest['recipients']) ? (array)$googleRequest['recipients'] : []; + $postalAddress->organization = isset($googleRequest['organization']) ? $googleRequest['organization'] : null; + + return $postalAddress; + } +} diff --git a/src/Request/RawInput.php b/src/Request/RawInput.php new file mode 100644 index 0000000..cc32184 --- /dev/null +++ b/src/Request/RawInput.php @@ -0,0 +1,47 @@ + + */ +class RawInput +{ + const INPUT_TYPE_UNSPECIFIED = 'UNSPECIFIED_INPUT_TYPE'; + const INPUT_TYPE_TOUCH = 'TOUCH'; + const INPUT_TYPE_VOICE = 'VOICE'; + const INPUT_TYPE_KEYBOARD = 'KEYBOARD'; + + /** + * A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds. Example: "2014-10-02T15:01:23.045123456Z". + * + * @var string|null + */ + public $createTime; + + /** + * @var string|null + */ + public $inputType; + + /** + * @var string|null + */ + public $query; + + /** + * @param array $googleRequest + * + * @return RawInput + */ + public static function fromGoogleRequest(array $googleRequest): RawInput + { + $rawInput = new self(); + + $rawInput->createTime = isset($googleRequest['createTime']) ? $googleRequest['createTime'] : null; + $rawInput->inputType = isset($googleRequest['inputType']) ? $googleRequest['inputType'] : null; + $rawInput->query = isset($googleRequest['query']) ? $googleRequest['query'] : null; + + return $rawInput; + } +} diff --git a/src/Request/Request.php b/src/Request/Request.php new file mode 100644 index 0000000..a137a3e --- /dev/null +++ b/src/Request/Request.php @@ -0,0 +1,64 @@ + + */ +class Request +{ + /** + * @var User|null + */ + public $user; + + /** + * @var Device|null + */ + public $device; + + /** + * @var Surface|null + */ + public $surface; + + /** + * @var Conversation|null + */ + public $conversation; + + /** + * @var Input[] + */ + public $inputs = []; + + /** + * @var bool|null + */ + public $isInSandbox; + + /** + * @param string $googleRequestBody + * + * @return Request + */ + public static function fromGoogleRequest(string $googleRequestBody): Request + { + $request = new self(); + + $googleRequest = json_decode($googleRequestBody, true); + + $request->user = isset($googleRequest['user']) ? User::fromGoogleRequest($googleRequest['user']) : null; + $request->device = isset($googleRequest['device']) ? Device::fromGoogleRequest($googleRequest['device']) : null; + $request->surface = isset($googleRequest['surface']) ? Surface::fromGoogleRequest($googleRequest['surface']) : null; + $request->conversation = isset($googleRequest['conversation']) ? Conversation::fromGoogleRequest($googleRequest['conversation']) : null; + if (isset($googleRequest['inputs'])) { + foreach ($googleRequest['inputs'] as $input) { + $request->inputs[] = Input::fromGoogleRequest($input); + } + } + $request->isInSandbox = isset($googleRequest['isInSandbox']) ? (bool)$googleRequest['isInSandbox'] : false; + + return $request; + } +} diff --git a/src/Request/Surface.php b/src/Request/Surface.php new file mode 100644 index 0000000..59f8491 --- /dev/null +++ b/src/Request/Surface.php @@ -0,0 +1,32 @@ + + */ +class Surface +{ + /** + * @var Capability[] + */ + public $capabilities = []; + + /** + * @param array $googleRequest + * + * @return Surface + */ + public static function fromGoogleRequest(array $googleRequest): Surface + { + $surface = new self(); + + if(isset($googleRequest['capabilities'])) { + foreach ($googleRequest['capabilities'] as $capability) { + $surface->capabilities[] = Capability::fromGoogleRequest($capability); + } + } + + return $surface; + } +} diff --git a/src/Request/Time.php b/src/Request/Time.php new file mode 100644 index 0000000..50c066a --- /dev/null +++ b/src/Request/Time.php @@ -0,0 +1,46 @@ + + */ +class Time +{ + /** + * @var int|null + */ + public $hours; + + /** + * @var int|null + */ + public $minutes; + + /** + * @var int|null + */ + public $seconds; + + /** + * @var int|null + */ + public $nanos; + + /** + * @param array $googleRequest + * + * @return Time + */ + public static function fromGoogleRequest(array $googleRequest): Time + { + $time = new self(); + + $time->hours = isset($googleRequest['hours']) ? (int)$googleRequest['hours'] : null; + $time->minutes = isset($googleRequest['minutes']) ? (int)$googleRequest['minutes'] : null; + $time->seconds = isset($googleRequest['seconds']) ? (int)$googleRequest['seconds'] : null; + $time->nanos = isset($googleRequest['nanos']) ? (int)$googleRequest['nanos'] : null; + + return $time; + } +} diff --git a/src/Request/User.php b/src/Request/User.php new file mode 100644 index 0000000..30e01de --- /dev/null +++ b/src/Request/User.php @@ -0,0 +1,57 @@ + + */ +class User +{ + const PERMISSION_UNSPECIFIED_PERMISSION = 'UNSPECIFIED_PERMISSION'; + const PERMISSION_NAME = 'NAME'; + const PERMISSION_DEVICE_PRECISE_LOCATION = 'DEVICE_PRECISE_LOCATION'; + const PERMISSION_DEVICE_COARSE_LOCATION = 'DEVICE_COARSE_LOCATION'; + + /** + * @var string|null + */ + public $userId; + + /** + * @var UserProfile|null + */ + public $profile; + + /** + * @var string|null + */ + public $accessToken; + + /** + * @var string[] + */ + public $permissions = []; + + /** + * @var string|null + */ + public $locale; + + /** + * @param array $googleRequest + * + * @return User + */ + public static function fromGoogleRequest(array $googleRequest): User + { + $user = new self(); + + $user->userId = isset($googleRequest['userId']) ? $googleRequest['userId'] : null; + $user->profile = isset($googleRequest['profile']) ? UserProfile::fromGoogleRequest($googleRequest['profile']) : null; + $user->accessToken = isset($googleRequest['accessToken']) ? $googleRequest['accessToken'] : null; + $user->permissions = isset($googleRequest['permissions']) ? (array)$googleRequest['permissions'] : []; + $user->locale = isset($googleRequest['locale']) ? $googleRequest['locale'] : null; + + return $user; + } +} diff --git a/src/Request/UserProfile.php b/src/Request/UserProfile.php new file mode 100644 index 0000000..3a84c05 --- /dev/null +++ b/src/Request/UserProfile.php @@ -0,0 +1,40 @@ + + */ +class UserProfile +{ + /** + * @var string|null + */ + public $displayName; + + /** + * @var string|null + */ + public $givenName; + + /** + * @var string|null + */ + public $familyName; + + /** + * @param array $googleRequest + * + * @return UserProfile + */ + public static function fromGoogleRequest(array $googleRequest): UserProfile + { + $userProfile = new self(); + + $userProfile->displayName = isset($googleRequest['displayName']) ? $googleRequest['displayName'] : null; + $userProfile->givenName = isset($googleRequest['givenName']) ? $googleRequest['givenName'] : null; + $userProfile->familyName = isset($googleRequest['familyName']) ? $googleRequest['familyName'] : null; + + return $userProfile; + } +} diff --git a/src/Response/Response.php b/src/Response/Response.php new file mode 100644 index 0000000..b03ec0e --- /dev/null +++ b/src/Response/Response.php @@ -0,0 +1,10 @@ + + */ +class Response +{ +}