Skip to content

Commit 9d541cf

Browse files
authored
Merge branch 'master' into drop_php56
2 parents 84b22f3 + d4387f9 commit 9d541cf

File tree

6 files changed

+215
-1
lines changed

6 files changed

+215
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"ext-json": "*",
2020
"intervention/image": "^2.3",
2121
"lasserafn/php-initials": "^2.20",
22-
"lasserafn/php-string-script-language": "^0.1.0"
22+
"lasserafn/php-string-script-language": "^0.1.0",
23+
"overtrue/pinyin": "^4.0"
2324
},
2425
"require-dev": {
2526
"doctrine/instantiator": "1.0.*",

composer.lock

Lines changed: 45 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/InitialAvatar.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Intervention\Image\ImageManager;
88
use LasseRafn\Initials\Initials;
99
use LasseRafn\StringScript;
10+
use LasseRafn\InitialAvatarGenerator\Translator\Base;
1011

1112
class InitialAvatar
1213
{
@@ -29,6 +30,30 @@ class InitialAvatar
2930
protected $fontFile = '/fonts/OpenSans-Regular.ttf';
3031
protected $generated_initials = 'JD';
3132

33+
/**
34+
* Language eg.en zh-CN
35+
*
36+
* @var string
37+
*/
38+
protected $language = 'en';
39+
40+
/**
41+
* Role translator
42+
*
43+
* @var Base
44+
*/
45+
protected $translator;
46+
47+
/**
48+
* Language related to translator
49+
*
50+
* @var array
51+
*/
52+
protected $translatorMap = [
53+
'en' => 'LasseRafn\\InitialAvatarGenerator\\Translator\\En',
54+
'zh-CN' => 'LasseRafn\\InitialAvatarGenerator\\Translator\\ZhCN',
55+
];
56+
3257
public function __construct() {
3358
$this->setupImageManager();
3459
$this->initials_generator = new Initials();
@@ -49,6 +74,7 @@ protected function setupImageManager() {
4974
* @return $this
5075
*/
5176
public function name( $nameOrInitials ) {
77+
$nameOrInitials = $this->translate($nameOrInitials);
5278
$this->name = $nameOrInitials;
5379
$this->initials_generator->name( $nameOrInitials );
5480

@@ -342,6 +368,66 @@ public function getAutoFont() {
342368
return $this->autofont;
343369
}
344370

371+
/**
372+
* Set language of name, pls use `language` before `name`, just like
373+
* ```php
374+
* $avatar->language('en')->name('Mr Green'); // Right
375+
* $avatar->name('Mr Green')->language('en'); // Wrong
376+
* ```
377+
*
378+
* @param string $language
379+
* @return $this
380+
*/
381+
public function language($language)
382+
{
383+
$this->language = $language ? : 'en';
384+
385+
return $this;
386+
}
387+
388+
/**
389+
* Add new translators designed by user
390+
*
391+
* @param array $translatorMap
392+
* ```php
393+
* $translatorMap = [
394+
* 'fr' => 'foo\bar\Fr',
395+
* 'zh-TW' => 'foo\bar\ZhTW'
396+
* ];
397+
* ```
398+
* @return $this
399+
*/
400+
public function addTranslators($translatorMap)
401+
{
402+
$this->translatorMap = array_merge($this->translatorMap, $translatorMap);
403+
404+
return $this;
405+
}
406+
407+
/**
408+
* @inheritdoc
409+
*/
410+
protected function translate($nameOrInitials)
411+
{
412+
return $this->getTranslator()->translate($nameOrInitials);
413+
}
414+
415+
/**
416+
* Instance the translator by language
417+
*
418+
* @return Base
419+
*/
420+
protected function getTranslator()
421+
{
422+
if ($this->translator instanceof Base && $this->translator->getSourceLanguage() === $this->language) {
423+
return $this->translator;
424+
}
425+
426+
$translatorClass = array_key_exists($this->language, $this->translatorMap) ? $this->translatorMap[$this->language] : 'LasseRafn\\InitialAvatarGenerator\\Translator\\En';
427+
428+
return $this->translato = new $translatorClass();
429+
}
430+
345431
/**
346432
* @param ImageManager $image
347433
*

src/Translator/Base.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace LasseRafn\InitialAvatarGenerator\Translator;
4+
5+
interface Base
6+
{
7+
/**
8+
* Translate words to english
9+
*
10+
* @param string $words
11+
* @return mixed
12+
*/
13+
public function translate($words);
14+
15+
/**
16+
* Get the source language of translator
17+
*
18+
* @return string
19+
*/
20+
public function getSourceLanguage();
21+
}

src/Translator/En.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace LasseRafn\InitialAvatarGenerator\Translator;
4+
5+
class En implements Base
6+
{
7+
/**
8+
* @inheritdoc
9+
*/
10+
public function translate($words)
11+
{
12+
return $words;
13+
}
14+
15+
/**
16+
* @inheritdoc
17+
*/
18+
public function getSourceLanguage()
19+
{
20+
return 'en';
21+
}
22+
}

src/Translator/ZhCN.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace LasseRafn\InitialAvatarGenerator\Translator;
4+
5+
use Overtrue\Pinyin\Pinyin;
6+
7+
class ZhCN implements Base
8+
{
9+
/**
10+
* Inherent instance of zh-CN translator
11+
*
12+
* @var Pinyin
13+
*/
14+
protected $inherent;
15+
16+
/**
17+
* ZhCN constructor, set the instance of PinYin
18+
*/
19+
public function __construct()
20+
{
21+
$this->inherent = new Pinyin();
22+
}
23+
24+
/**
25+
* @inheritdoc
26+
*/
27+
public function translate($words)
28+
{
29+
return implode(' ', $this->inherent->name($words));
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function getSourceLanguage()
36+
{
37+
return 'zh-CN';
38+
}
39+
}

0 commit comments

Comments
 (0)