├── CHANGELOG.md
├── LICENSE
├── README.md
├── _ide
└── YiiApplication.php
├── composer.json
└── src
├── GoogleApi.php
├── GoogleApiService.php
├── GoogleApiServiceInterface.php
└── services
├── GoogleApiServiceAdsense.php
├── GoogleApiServiceTranslate.php
└── GoogleApiServiceYoutube.php
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | CHANGELOG
2 | ==============
3 |
4 | 2.0.1
5 | -----------------
6 | * New version stable release
7 |
8 | 2.0.0
9 | -----------------
10 | * New version
11 |
12 | 1.1.0
13 | -----------------
14 | * Update
15 |
16 | 1.0.0
17 | -----------------
18 | * Stable release
19 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | BSD 3-Clause License
2 |
3 | Copyright (c) 2019, Semenov Alexander
4 | All rights reserved.
5 |
6 | Redistribution and use in source and binary forms, with or without
7 | modification, are permitted provided that the following conditions are met:
8 |
9 | * Redistributions of source code must retain the above copyright notice, this
10 | list of conditions and the following disclaimer.
11 |
12 | * Redistributions in binary form must reproduce the above copyright notice,
13 | this list of conditions and the following disclaimer in the documentation
14 | and/or other materials provided with the distribution.
15 |
16 | * Neither the name of the copyright holder nor the names of its
17 | contributors may be used to endorse or promote products derived from
18 | this software without specific prior written permission.
19 |
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Component for work with official google api
2 | ===================================
3 |
4 | Partly wrapper over powerful official package from google — google/apiclient
5 |
6 | * [google/apiclient](https://github.com/google/google-api-php-client)
7 | * [google/apiclient-services](https://github.com/google/google-api-php-client-services)
8 | * https://console.cloud.google.com/home/dashboard
9 |
10 |
11 | [](https://packagist.org/packages/skeeks/yii2-google-api)
12 | [](https://packagist.org/packages/skeeks/yii2-google-api)
13 |
14 | Installation
15 | ------------
16 |
17 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/).
18 |
19 | Either run
20 |
21 | ```
22 | php composer.phar require --prefer-dist skeeks/yii2-google-api "^2.0.1"
23 | ```
24 |
25 | or add
26 |
27 | ```
28 | "skeeks/yii2-google-api": "^2.0.1"
29 | ```
30 |
31 |
32 | How to use
33 | ----------
34 |
35 | ### Configuration app
36 | ```php
37 | //App config
38 | [
39 | 'components' =>
40 | [
41 | //....
42 | 'googleApi' =>
43 | [
44 | 'class' => '\skeeks\yii2\googleApi\GoogleApi',
45 | 'key' => 'YOUR_GOOLE_API_KEY',
46 | ],
47 | //....
48 | ]
49 | ]
50 |
51 | ```
52 |
53 | An example of the Api transliteration
54 | ---
55 |
56 | https://cloud.google.com/translate/v2/using_rest
57 |
58 | Translate "apple"
59 |
60 | ```php
61 | $result = \Yii::$app->googleApi->serviceTranslate->translate('apple', 'ru');
62 | print_r($result);
63 | ```
64 |
65 | or
66 |
67 | ```php
68 | $result = \Yii::$app->googleApi->serviceTranslate->googleService->translations->listTranslations('apple', 'ru');
69 | print_r($result);
70 | ```
71 |
72 | or
73 |
74 | ```php
75 | $googleService = \Yii::$app->googleApi->serviceTranslate->googleService;
76 | $result = $googleService->translations->listTranslations('apple', 'ru');
77 | print_r($result);
78 | ```
79 |
80 | or
81 |
82 | ```php
83 | $googleClient = \Yii::$app->googleApi->googleClient;
84 | $googleService = new \Google_Service_Translate($googleClient);
85 | $result = $googleService->translations->listTranslations('apple', 'ru');
86 | print_r($result);
87 | ```
88 |
89 | ```php
90 | $service = \Yii::$app->googleApi->serviceTranslate->googleService;
91 | $result = $service->languages->listLanguages([
92 | 'target' => 'ru'
93 | ]);
94 | print_r($result);
95 | ```
96 |
97 |
98 | An example other google services
99 | ---
100 |
101 | ```php
102 |
103 | $googleClient = \Yii::$app->googleApi->googleClient;
104 | $googleServiceAdsense = new \Google_Service_Adsense($googleClient);
105 | $googleServiceAdsense = new \Google_Service_Youtube($googleClient);
106 | //....
107 |
108 | ```
109 |
110 | Your Google Services
111 | ---
112 |
113 | ```php
114 | //App config
115 | [
116 | 'components' =>
117 | [
118 | //....
119 | 'googleApi' =>
120 | [
121 | 'class' => '\skeeks\yii2\googleApi\GoogleApi',
122 | 'key' => 'YOUR_GOOLE_API_KEY',
123 |
124 | 'serviceTranslateClass' => 'skeeks\cms\googleApi\serviceTranslate\GoogleApiServiceTranslate'
125 |
126 | //or
127 |
128 | 'serviceTranslateClass' => [
129 | 'class' => 'skeeks\cms\googleApi\serviceTranslate\GoogleApiServiceTranslate',
130 |
131 | 'option' => 'value'
132 | ],
133 | ],
134 | //....
135 | ]
136 | ]
137 |
138 | ```
139 |
140 |
141 | ___
142 |
143 | > [](https://skeeks.com)
144 | SkeekS CMS (Yii2) — quickly, easily and effectively!
145 | [skeeks.com](https://skeeks.com) | [cms.skeeks.com](https://cms.skeeks.com)
146 |
147 |
148 |
--------------------------------------------------------------------------------
/_ide/YiiApplication.php:
--------------------------------------------------------------------------------
1 |
4 | * @link http://skeeks.com/
5 | * @copyright 2010 SkeekS (СкикС)
6 | * @date 28.03.2016
7 | */
8 |
9 | namespace yii\web;
10 |
11 | use skeeks\yii2\googleApi\GoogleApi;
12 |
13 | /**
14 | * @property GoogleApi $googleApi
15 | *
16 | * Class Application
17 | * @package yii\web
18 | */
19 | class Application
20 | {
21 | }
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "skeeks/yii2-google-api",
3 | "description": "Component for work with google api based on google/apiclient",
4 | "keywords": ["yii", "yii2", "skeeks", "api", "component", "google", "client"],
5 | "homepage": "https://skeeks.com",
6 | "type": "yii2-extension",
7 | "license": "BSD-3-Clause",
8 | "support": {
9 | "issues": "https://github.com/skeeks-semenov/yii2-google-api/issues",
10 | "source": "https://github.com/skeeks-semenov/yii2-google-api/",
11 | "authors": "https://skeeks.com"
12 | },
13 | "authors": [
14 | {
15 | "name": "SkeekS",
16 | "email": "support@skeeks.com",
17 | "homepage": "https://skeeks.com"
18 | },
19 | {
20 | "name": "Semenov Alexander",
21 | "email": "semenov@skeeks.com"
22 | }
23 | ],
24 | "require": {
25 | "yiisoft/yii2": "^2.0.0",
26 | "google/apiclient": "2.1.3"
27 | },
28 | "autoload": {
29 | "psr-4": {
30 | "skeeks\\yii2\\googleApi\\": "src/"
31 | }
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/GoogleApi.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 |
9 | namespace skeeks\yii2\googleApi;
10 |
11 | use skeeks\yii2\googleApi\services\GoogleApiServiceAdsense;
12 | use skeeks\yii2\googleApi\services\GoogleApiServiceTranslate;
13 | use skeeks\yii2\googleApi\services\GoogleApiServiceYoutube;
14 | use yii\base\Component;
15 | use yii\base\Exception;
16 | use yii\base\InvalidConfigException;
17 | use yii\helpers\ArrayHelper;
18 |
19 | /**
20 | * @property \Google_Client $googleClient
21 | *
22 | * @property GoogleApiServiceTranslate $serviceTranslate
23 | * @property GoogleApiServiceYoutube $serviceYoutube
24 | * @property GoogleApiServiceAdsense $serviceAdsense
25 | *
26 | * @author Semenov Alexander
27 | */
28 | class GoogleApi extends Component
29 | {
30 | /**
31 | * @see https://console.developers.google.com/apis/credentials?project=api-project-1091876680684
32 | * @var string
33 | */
34 | public $key = '';
35 |
36 | /**
37 | * @var string
38 | */
39 | public $serviceTranslateClass = GoogleApiServiceTranslate::class;
40 | /**
41 | * @var string
42 | */
43 | public $serviceYoutubeClass = GoogleApiServiceYoutube::class;
44 | /**
45 | * @var string
46 | */
47 | public $serviceAdsenseClass = GoogleApiServiceAdsense::class;
48 |
49 | /**
50 | * @return \Google_Client
51 | */
52 | public function getGoogleClient()
53 | {
54 | $client = new \Google_Client();
55 | $client->setDeveloperKey($this->key);
56 |
57 | return $client;
58 | }
59 |
60 | /**
61 | * @param $serviceName
62 | * @return GoogleApiService
63 | * @throws InvalidConfigException
64 | */
65 | private function _createService(string $serviceName) {
66 | $serviceName = $serviceName . "Class";
67 | if (!isset($this->{$serviceName})) {
68 | throw new InvalidConfigException("Not exist service");
69 | }
70 |
71 | $serviceData = $this->{$serviceName};
72 | $config = [
73 | 'googleApi' => $this
74 | ];
75 |
76 | if (is_string($serviceData)) {
77 | $config = ArrayHelper::merge($config, [
78 | 'class' => $serviceData
79 | ]);
80 | } elseif(is_array($serviceData)) {
81 | $config = ArrayHelper::merge($serviceData, $config);
82 | }
83 |
84 | return \Yii::createObject($config);
85 | }
86 |
87 | /**
88 | * @return GoogleApiServiceTranslate
89 | * @throws InvalidConfigException
90 | */
91 | public function getServiceTranslate()
92 | {
93 | return $this->_createService("serviceTranslate");
94 | }
95 | /**
96 | * @return GoogleApiServiceYoutube
97 | * @throws InvalidConfigException
98 | */
99 | public function getServiceYoutube()
100 | {
101 | return $this->_createService("serviceYoutube");
102 | }
103 |
104 | /**
105 | * @return GoogleApiServiceAdsense
106 | * @throws InvalidConfigException
107 | */
108 | public function getServiceAdsense()
109 | {
110 | return $this->_createService("serviceAdsense");
111 | }
112 | }
--------------------------------------------------------------------------------
/src/GoogleApiService.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 |
9 | namespace skeeks\yii2\googleApi;
10 |
11 | use yii\base\Component;
12 | use yii\base\InvalidConfigException;
13 |
14 | /**
15 | * @property \Google_Service $googleService
16 | *
17 | * @author Semenov Alexander
18 | */
19 | abstract class GoogleApiService extends Component implements GoogleApiServiceInterface
20 | {
21 | /**
22 | * @var GoogleApi
23 | */
24 | public $googleApi;
25 |
26 | public function __construct(GoogleApi $googleApi = null, $config = [])
27 | {
28 | $this->googleApi = $googleApi;
29 | parent::__construct($config);
30 | }
31 |
32 | /**
33 | * @throws InvalidConfigException
34 | */
35 | public function init()
36 | {
37 | parent::init();
38 |
39 | if (!$this->googleApi && !$this->googleApi instanceof GoogleApi) {
40 | throw new InvalidConfigException("googleApi must be ".GoogleApi::class);
41 | }
42 |
43 | }
44 |
45 | /**
46 | * @return \Google_Service
47 | */
48 | abstract public function getGoogleService();
49 | }
--------------------------------------------------------------------------------
/src/GoogleApiServiceInterface.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 |
9 | namespace skeeks\yii2\googleApi;
10 |
11 | /**
12 | * @author Semenov Alexander
13 | */
14 | interface GoogleApiServiceInterface
15 | {
16 | /**
17 | * @return \Google_Service
18 | */
19 | public function getGoogleService();
20 | }
--------------------------------------------------------------------------------
/src/services/GoogleApiServiceAdsense.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 |
9 | namespace skeeks\yii2\googleApi\services;
10 |
11 | use skeeks\yii2\googleApi\GoogleApi;
12 | use skeeks\yii2\googleApi\GoogleApiService;
13 | use yii\base\Component;
14 |
15 | /**
16 | * @property \Google_Service_AdSense $googleService
17 | *
18 | * @author Semenov Alexander
19 | */
20 | class GoogleApiServiceAdsense extends GoogleApiService
21 | {
22 | /**
23 | * @return \Google_Service_AdSense
24 | */
25 | public function getGoogleService()
26 | {
27 | return new \Google_Service_AdSense($this->googleApi->googleClient);
28 | }
29 | }
--------------------------------------------------------------------------------
/src/services/GoogleApiServiceTranslate.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 |
9 | namespace skeeks\yii2\googleApi\services;
10 |
11 | use skeeks\yii2\googleApi\GoogleApi;
12 | use skeeks\yii2\googleApi\GoogleApiService;
13 | use yii\base\Component;
14 |
15 | /**
16 | * @property \Google_Service_Translate $googleService
17 | *
18 | * @author Semenov Alexander
19 | */
20 | class GoogleApiServiceTranslate extends GoogleApiService
21 | {
22 | /**
23 | * @return \Google_Service_Translate
24 | */
25 | public function getGoogleService()
26 | {
27 | return new \Google_Service_Translate($this->googleApi->googleClient);
28 | }
29 |
30 | /**
31 | *
32 | * @see https://cloud.google.com/translate/docs/translating-text
33 | *
34 | * @param $source_phrase
35 | * @param $target_language
36 | * @param null $source_language
37 | * @param string $source_format
38 | * @return string
39 | * @throws \Exception
40 | */
41 | public function translate($source_phrase, $target_language, $source_language = null, $source_format = 'text')
42 | {
43 | $source_phrase = trim($source_phrase);
44 | $source_format = $source_format == 'html' ? 'html' : 'text';
45 |
46 | try {
47 | //Обратимся к google сервису
48 | $service = $this->googleService;
49 |
50 | $optParams = [];
51 | if ($source_format !== 'text') {
52 | $optParams['format'] = 'html';
53 | }
54 |
55 | if ($source_language) {
56 | $optParams['source'] = $source_language;
57 | }
58 |
59 | $result = $service->translations->listTranslations($source_phrase, $target_language, $optParams);
60 |
61 | $data = $result->offsetGet('data');
62 | if (isset($data['translations'][0]['translatedText'])) {
63 | $translated = $data['translations'][0]['translatedText'];
64 | return (string)$translated;
65 | } else {
66 | throw new Exception("Not translated '{$source_phrase}' to '{$target_language}'");
67 | }
68 | } catch (\Exception $e) {
69 | \Yii::warning($e->getMessage(), self::class);
70 | throw $e;
71 | }
72 | }
73 | }
--------------------------------------------------------------------------------
/src/services/GoogleApiServiceYoutube.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 |
9 | namespace skeeks\yii2\googleApi\services;
10 |
11 | use skeeks\yii2\googleApi\GoogleApi;
12 | use skeeks\yii2\googleApi\GoogleApiService;
13 | use yii\base\Component;
14 |
15 | /**
16 | * @property \Google_Service_YouTube $googleService
17 | *
18 | * @author Semenov Alexander
19 | */
20 | class GoogleApiServiceYoutube extends GoogleApiService
21 | {
22 | /**
23 | * @return \Google_Service_YouTube
24 | */
25 | public function getGoogleService()
26 | {
27 | return new \Google_Service_YouTube($this->googleApi->googleClient);
28 | }
29 | }
--------------------------------------------------------------------------------