Skip to content

Commit

Permalink
add uppercase option
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Aug 16, 2016
1 parent 1ce5f55 commit 35af0d7
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 10 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ return [
// font size
'fontSize' => 48,

// convert initial letter to uppercase
'uppercase' => false,

// Fonts used to render text.
// If contains more than one fonts, randomly selected based on name supplied
// You can provide absolute path, path relative to folder resources/laravolt/avatar/fonts/, or mixed.
Expand Down
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
// font size
'fontSize' => 48,

// convert initial letter in uppercase
'uppercase' => false,

// Fonts used to render text.
// If contains more than one fonts, randomly selected based on name supplied
// You can provide absolute path, path relative to folder resources/laravolt/avatar/fonts/, or mixed.
Expand Down
12 changes: 11 additions & 1 deletion src/Avatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ public function __construct(array $config, CacheManager $cache, InitialGenerator
$this->borderColor = Arr::get($config, 'border.color');

$this->cache = $cache;
$this->initialGenerator = $initialGenerator;
$this->initialGenerator = $initialGenerator->setUppercase(Arr::get($config, 'uppercase'));
}

/**
* @return String
*/
function __toString()
{
return (string) $this->toBase64();
}


public function create($name)
{
$this->name = $name;
Expand Down Expand Up @@ -272,6 +281,7 @@ protected function cacheKey()
$keys = [];
$attributes = [
'name',
'initials',
'shape',
'chars',
'font',
Expand Down
34 changes: 26 additions & 8 deletions src/InitialGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class InitialGenerator

protected $ascii = false;

protected $uppercase = false;

/**
* Identifier constructor.
*
Expand Down Expand Up @@ -71,25 +73,41 @@ public function setAscii($ascii)
return $this;
}

public function setUppercase($uppercase)
{
$this->uppercase = $uppercase;

return $this;
}

public function getInitial()
{
$words = new Collection(explode(' ', $this->name));

// if name contains single word, use first N character
if ($words->count() === 1) {

$initial = (string)$words->first();

if ($this->name->length() >= $this->length) {
return (string) $this->name->substr(0, $this->length);
$initial = (string)$this->name->substr(0, $this->length);
}

return (string) $words->first();
} else {
// otherwise, use initial char from each word
$initials = new Collection();
$words->each(function ($word) use ($initials) {
$initials->push(Stringy::create($word)->substr(0, 1));
});

$initial = $initials->slice(0, $this->length)->implode('');

}

// otherwise, use initial char from each word
$initials = new Collection();
$words->each(function ($word) use ($initials) {
$initials->push(Stringy::create($word)->substr(0, 1));
});
if ($this->uppercase) {
$initial = strtoupper($initial);
}

return $initials->slice(0, $this->length)->implode('');
return $initial;
}
}
8 changes: 7 additions & 1 deletion tests/AvatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function it_can_instantiated()

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('getInitial')->andReturn('AB');
$generator->shouldReceive('setUppercase')->andReturnSelf();

new \Laravolt\Avatar\Avatar([], $cache, $generator);
}
Expand All @@ -38,6 +39,7 @@ public function it_can_override_attributes_when_instantiated()

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('getInitial')->andReturn('AB');
$generator->shouldReceive('setUppercase');

$avatar = new \Laravolt\Avatar\Avatar($config, $cache, $generator);

Expand All @@ -64,6 +66,7 @@ public function it_can_override_attributes_after_set_name()
$generator->shouldReceive('setName')->andReturnSelf();
$generator->shouldReceive('setLength');
$generator->shouldReceive('getInitial')->andReturn('A');
$generator->shouldReceive('setUppercase')->andReturnSelf();
$generator->shouldReceive('base_path');
$config = ['backgrounds' => ['#000000', '#111111'], 'foregrounds' => ['#EEEEEE', '#FFFFFF']];

Expand All @@ -77,7 +80,7 @@ public function it_can_override_attributes_after_set_name()
/**
* @test
*/
public function it_has_corrent_random_background()
public function it_has_correct_random_background()
{
$config = [
'foregrounds' => ['#000000', '#111111'],
Expand All @@ -87,6 +90,7 @@ public function it_has_corrent_random_background()
$cache = Mockery::mock('Illuminate\Cache\CacheManager');

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('setUppercase')->andReturnSelf();

$avatar = new \Laravolt\Avatar\Avatar($config, $cache, $generator);
$avatar->setFontFolder(['fonts/']);
Expand All @@ -95,6 +99,7 @@ public function it_has_corrent_random_background()

$generator->shouldReceive('setLength')->andReturn(1);
$generator->shouldReceive('setName')->andReturn($name);
$generator->shouldReceive('setUppercase')->andReturnSelf();
$generator->shouldReceive('getInitial')->andReturn('A');
$avatar->create($name);

Expand All @@ -114,6 +119,7 @@ public function it_has_different_random_background()
$cache = Mockery::mock('Illuminate\Cache\CacheManager');

$generator = Mockery::mock('Laravolt\Avatar\InitialGenerator');
$generator->shouldReceive('setUppercase')->andReturnSelf();

$name1 = 'AA';
$name2 = 'AAA';
Expand Down

0 comments on commit 35af0d7

Please sign in to comment.