From a3aaac1d0e8b3dce8cab1713f7e102f685bf23d0 Mon Sep 17 00:00:00 2001 From: Alexander Vasilyev Date: Fri, 20 Dec 2019 11:00:00 +0300 Subject: [PATCH] Init First public commit --- .gitignore | 5 + LICENSE | 21 +++ README.md | 20 +++ composer.json | 41 +++++ phpunit.xml | 37 +++++ src/Provider/SeptemberFirstProvider.php | 66 ++++++++ src/Provider/SeptemberFirstUser.php | 194 ++++++++++++++++++++++++ 7 files changed, 384 insertions(+) create mode 100755 .gitignore create mode 100755 LICENSE create mode 100755 README.md create mode 100755 composer.json create mode 100755 phpunit.xml create mode 100755 src/Provider/SeptemberFirstProvider.php create mode 100755 src/Provider/SeptemberFirstUser.php diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..9c9c8f2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/build +/vendor +composer.phar +composer.lock +.DS_Store diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..8d2f4c7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2019 Alexander Vasilyev + +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 100755 index 0000000..ad6e846 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# September First OAuth2 client provider +[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) + +This package provides [September First](https://api.1sept.ru) integration for [OAuth2 Client](https://github.com/thephpleague/oauth2-client) by the League. + +## Installation + +```sh +composer require 1sept/oauth2-1sept +``` + +## Usage + +```php +$provider = new Sept\OAuth2\Client\Provider\SeptemberFirst([ + 'clientId' => 'client_id', + 'clientSecret' => 'secret', + 'redirectUri' => 'https://example.org/oauth-endpoint', +]); +``` diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..feb75fc --- /dev/null +++ b/composer.json @@ -0,0 +1,41 @@ +{ + "name": "1sept/oauth2-1sept", + "description": "September First OAuth 2.0 Client Provider for The PHP League OAuth2-Client", + "license": "MIT", + "authors": [ + { + "name": "Alexander Vasilyev", + "email": "a.vasilyev@1sept.ru" + } + ], + "keywords": [ + "oauth", + "oauth2", + "client", + "authorization", + "authorisation", + "1sept" + ], + "require": { + "php": "^5.6|^7.0", + "league/oauth2-client": "^2.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "autoload": { + "psr-4": { + "Sept\\OAuth2\\Client\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Sept\\OAuth2\\Client\\Test\\": "test/src/" + } + }, + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + } +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100755 index 0000000..289c3f5 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,37 @@ + + + + + + + + + ./test/ + + + + + ./ + + ./vendor + ./test + + + + \ No newline at end of file diff --git a/src/Provider/SeptemberFirstProvider.php b/src/Provider/SeptemberFirstProvider.php new file mode 100755 index 0000000..9ae6e91 --- /dev/null +++ b/src/Provider/SeptemberFirstProvider.php @@ -0,0 +1,66 @@ +data = $response; + } + + /** + * Значение массива (многомерного) + * + * @param string $key Ключ поля (например: `email` или `name.first` — вложенность оформляется точкой) + * @return mixed|null + */ + public static function getFieldFromArray(string $key, ?array $array) + { + if (strpos($key, '.')) { // key.subKey.subSubKey + list ($key, $subKey) = explode('.', $key, 2); + return isset($array[$key]) ? static::getFieldFromArray($subKey, $array[$key]) : null; + } + + return isset($array[$key]) ? $array[$key] : null; + } + + /** + * Элемент массива данных о пользователе + * + * @param string $key Ключ поля (например: email или name.first — вложенность оформляется точкой) + * @return mixed|null + */ + protected function getField(string $key) + { + return static::getFieldFromArray($key, $this->data); + } + + /** + * Данные о пользователе в виде массива + * + * @return array + */ + public function toArray() + { + return $this->data; + } + + /** + * ID пользователя + */ + public function getId(): ?string + { + return $this->getField('id'); + } + + /** + * {@inheritdoc} + */ + public function getLastName(): ?string + { + return $this->getField('personal_name.surname'); + } + + /** + * {@inheritdoc} + */ + public function getFirstName(): ?string + { + return $this->getField('personal_name.name'); + } + + /** + * {@inheritdoc} + */ + public function getMiddleName(): ?string + { + return $this->getField('personal_name.patronymic'); + } + + /** + * Девичья фамилия пользователя (только для женского пола) + * + * @return string|null + */ + public function getMaidenName(): ?string + { + return null; + } + + /** + * {@inheritdoc} + */ + public function getDisplayName(): ?string + { + return $this->getField('display_name'); + } + + /** + * {@inheritdoc} + */ + public function getSex(): ?string + { + return $this->getField('sex'); + } + + /** + * {@inheritdoc} + */ + public function getEmail(): ?string + { + return $this->getField('email'); + } + + /** + * {@inheritdoc} + */ + public function getBirthday(): ?\DateTime + { + return !empty($this->data['birthday']) ? new \DateTime($this->data['birthday']) : null; + } + + /** + * {@inheritdoc} + */ + public function getAvatarUrl(bool $rejectEmptyAvatar = false): ?string + { + if (empty($this->data['avatar']) or ((!isset($this->data['has_avatar']) or !$this->data['has_avatar']) and $rejectEmptyAvatar)) { + return null; + } + + return $this->getField('avatar'); + } + + /** + * Адрес аватарки 50x50 + * + * @param bool $rejectEmptyAvatar Если true, то пустые аватарки (заглушки) не отдаются + * @return string|null + */ + public function getAvatar50Url(bool $rejectEmptyAvatar = false): ?string + { + return $this->getAvatarUrl($rejectEmptyAvatar); + } + + /** + * {@inheritdoc} + */ + public function getProfileUrl(): ?string + { + return $this->getField('profile_url'); + } + + /** + * Номер телефона + * + * @return string|null + */ + public function getPhone(): ?string + { + return null; + } + + /** + * Локаль + * + * @return string|null + */ + public function getLocale(): ?string + { + return null; + } + + /** + * Имя временной зоны (от -24 до 24) + * @example Europe/Moscow + * + * @return string|null + */ + public function getTimezone(): ?string + { + return $this->getField('timezone'); + } +}