generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from sowrensen/dev
Support for custom extractor
- Loading branch information
Showing
10 changed files
with
129 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ phpstan.neon | |
testbench.yaml | ||
vendor | ||
node_modules | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace Sowren\SvgAvatarGenerator\Extractors; | ||
|
||
use Str; | ||
|
||
class DefaultExtractor implements Extractor | ||
{ | ||
/** | ||
* Extract initials from given text/name. If only one word is given, | ||
* it will look for second capital character in the word, else | ||
* the consecutive second character will be taken. | ||
*/ | ||
public function extract(string $text): string | ||
{ | ||
if (Str::contains($text, ' ')) { | ||
// If name has more than one part then break each part upto each space | ||
$parts = Str::of($text)->explode(' '); | ||
} else { | ||
// If name has only one part then try to find out if there are | ||
// any uppercase letters in the string. Then break the string | ||
// upto each uppercase letters, this allows to pass names in | ||
// studly case, e.g. 'SowrenSen'. If no uppercase letter is | ||
// found, $parts will have only one item in the array. | ||
$parts = Str::of($text)->kebab()->replace('-', ' ')->explode(' '); | ||
} | ||
|
||
$firstInitial = $parts->first()[0]; | ||
|
||
// If only one part is found, take the second letter as second | ||
// initial, else take the first letter of the last part. | ||
$secondInitial = ($parts->count() === 1) ? $parts->first()[1] : $parts->last()[0]; | ||
|
||
return strtoupper($firstInitial.$secondInitial); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Sowren\SvgAvatarGenerator\Extractors; | ||
|
||
interface Extractor | ||
{ | ||
/** | ||
* Extract initials from given text. | ||
*/ | ||
public function extract(string $text): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.