Skip to content

Commit

Permalink
More tests and a length option
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRafn committed Jan 15, 2017
1 parent 50b8a11 commit e8fa38f
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 24 deletions.
60 changes: 43 additions & 17 deletions src/InitialAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class InitialAvatar
private $image;

private $parameter_cacheTime = 0;
private $parameter_name = 'JD';
private $parameter_length = 2;
private $parameter_initials = 'JD';
private $parameter_name = 'John Doe';
private $parameter_size = 48;
private $parameter_bgColor = '#000';
private $parameter_fontColor = '#fff';
Expand All @@ -23,7 +25,16 @@ public function __construct()

public function name( string $nameOrInitials ): self
{
$this->parameter_name = $this->generateInitials( $nameOrInitials );
$this->parameter_name = $nameOrInitials;
$this->parameter_initials = $this->generateInitials();

return $this;
}

public function length( int $length = 2 ): self
{
$this->parameter_length = (int) $length;
$this->parameter_initials = $this->generateInitials();

return $this;
}
Expand Down Expand Up @@ -74,14 +85,14 @@ public function generate( $name = null ): Image
{
if ( $name !== null )
{
$this->parameter_name = $this->generateInitials( $name );
$this->parameter_initials = $this->generateInitials( $this->parameter_name );
}

$fontFile = $this->parameter_fontFile;
$size = $this->parameter_size;
$color = $this->parameter_fontColor;
$bgColor = $this->parameter_bgColor;
$name = $this->parameter_name;
$name = $this->parameter_initials;

$img = $this->image->cache( function ( ImageCache $image ) use ( $size, $bgColor, $color, $fontFile, $name )
{
Expand All @@ -103,9 +114,9 @@ public function generate( $name = null ): Image
*
* @return string
*/
public function getParameterName()
public function getInitials()
{
return $this->parameter_name;
return $this->parameter_initials;
}

/**
Expand All @@ -114,26 +125,41 @@ public function getParameterName()
* For safety, we limit it to two characters,
* in case its a single, but long, name.
*
* @param string $nameOrInitials
*
* @return string
*/
private function generateInitials( string $nameOrInitials = 'John Doe' ): string
private function generateInitials(): string
{
$nameOrInitials = mb_strtoupper( trim( $nameOrInitials ) );

$names = explode( ' ', $nameOrInitials );
$nameOrInitials = mb_strtoupper( trim( $this->parameter_name ) );
$names = explode( ' ', $nameOrInitials );
$initials = $nameOrInitials;
$assignedNames = 0;

if ( count( $names ) > 1 )
{
$firstNameLetter = mb_substr( $names[0], 0, 1 );
$lastNameLetter = mb_substr( $names[ count( $names ) - 1 ], 0, 1 );
$initials = '';
$start = 0;

for ( $i = 0; $i < $this->parameter_length; $i ++ )
{
$index = $i;

if ( ( $index === ( $this->parameter_length - 1 ) && $index > 0 ) || ( $index > ( count( $names ) - 1 ) ) )
{
$index = count( $names ) - 1;
}

if ( $assignedNames >= count( $names ) )
{
$start ++;
}

$nameOrInitials = "{$firstNameLetter}{$lastNameLetter}";
$initials .= mb_substr( $names[ $index ], $start, 1 );
$assignedNames ++;
}
}

$nameOrInitials = mb_substr( $nameOrInitials, 0, 2 );
$initials = mb_substr( $initials, 0, $this->parameter_length );

return $nameOrInitials;
return $initials;
}
}
14 changes: 7 additions & 7 deletions tests/InitialGenerationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ public function testInitialsAreGeneratedFromFullname()

$avatar->name('John Doe');

$this->assertEquals('JD', $avatar->getParameterName());
$this->assertEquals('JD', $avatar->getInitials());

// Single name

$avatar->name('John');

$this->assertEquals('JO', $avatar->getParameterName());
$this->assertEquals('JO', $avatar->getInitials());

// Initials

$avatar->name('MA');

$this->assertEquals('MA', $avatar->getParameterName());
$this->assertEquals('MA', $avatar->getInitials());

// Three names

$avatar->name('John Doe Bergerson');

$this->assertEquals('JB', $avatar->getParameterName());
$this->assertEquals('JB', $avatar->getInitials());

// Other name

$avatar->name('Gustav Årgonson');

$this->assertEquals('', $avatar->getParameterName());
$this->assertEquals('', $avatar->getInitials());

$avatar->name('Chanel Butterman');

$this->assertNotEquals('AB', $avatar->getParameterName());
$this->assertEquals('CB', $avatar->getParameterName());
$this->assertNotEquals('AB', $avatar->getInitials());
$this->assertEquals('CB', $avatar->getInitials());
}
}
62 changes: 62 additions & 0 deletions tests/InitialLimitationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

use PHPUnit\Framework\TestCase;
use LasseRafn\InitialAvatarGenerator\InitialAvatar;

class InitialLimitationTest extends TestCase
{
public function testCanLimitInitials()
{
$avatar = new InitialAvatar();

// One letter

$avatar->name( 'John Doe' )->length( 1 );

$this->assertEquals( 'J', $avatar->getInitials() );
$this->assertEquals( 1, strlen( $avatar->getInitials() ) );

// Two letters
$avatar->name( 'John Doe' )->length( 2 );

$this->assertEquals( 'JD', $avatar->getInitials() );
$this->assertEquals( 2, strlen( $avatar->getInitials() ) );

// Three letters
$avatar->name( 'John Doe Johnson' )->length( 3 );

$this->assertEquals( 'JDJ', $avatar->getInitials() );
$this->assertEquals( 3, strlen( $avatar->getInitials() ) );

// Three letters with only two names
$avatar->name( 'John Doe' )->length( 3 );

$this->assertEquals( 'JDO', $avatar->getInitials() );
$this->assertEquals( 3, strlen( $avatar->getInitials() ) );

// Four letters with only two names
$avatar->name( 'John Doe' )->length( 4 );

$this->assertEquals( 'JDOE', $avatar->getInitials() );
$this->assertEquals( 4, strlen( $avatar->getInitials() ) );

// Five letters with only one name of 4 letters
// This is not possible, of cause, so it will end in 4 letters
$avatar->name( 'John' )->length( 5 );

$this->assertEquals( 'JOHN', $avatar->getInitials() );
$this->assertEquals( 4, strlen( $avatar->getInitials() ) );

// Five letters with only one name of 5 letters
$avatar->name( 'Lasse' )->length( 5 );

$this->assertEquals( 'LASSE', $avatar->getInitials() );
$this->assertEquals( 5, strlen( $avatar->getInitials() ) );

// One letter from a one letter initial
$avatar->name( 'L' )->length( 1 );

$this->assertEquals( 'L', $avatar->getInitials() );
$this->assertEquals( 1, strlen( $avatar->getInitials() ) );
}
}

0 comments on commit e8fa38f

Please sign in to comment.