Skip to content

Commit

Permalink
Abstract string functions to support both mbstring and native
Browse files Browse the repository at this point in the history
  • Loading branch information
wyrfel committed Dec 15, 2019
1 parent 9a54a71 commit 14c9327
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 7 deletions.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -18,6 +18,7 @@
"php-mock/php-mock-phpunit": "^2.1"
},
"autoload": {
"files": ["src/string_functions.php"],
"psr-4": {
"TheIconic\\NameParser\\": ["src/", "tests/"]
}
Expand Down
4 changes: 3 additions & 1 deletion src/Mapper/InitialMapper.php
Expand Up @@ -4,6 +4,8 @@

use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Initial;
use function TheIconic\NameParser\characters;
use function TheIconic\NameParser\strlen;

/**
* single letter, possibly followed by a period
Expand Down Expand Up @@ -46,7 +48,7 @@ public function map(array $parts): array
$length = strlen($stripped);

if (1 < $length && $length <= $this->combinedMax) {
array_splice($parts, $k, 1, str_split($stripped));
array_splice($parts, $k, 1, characters($stripped));
$last = count($parts) - 1;
$part = $parts[$k];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mapper/LastnameMapper.php
Expand Up @@ -2,13 +2,13 @@

namespace TheIconic\NameParser\Mapper;

use TheIconic\NameParser\LanguageInterface;
use TheIconic\NameParser\Part\AbstractPart;
use TheIconic\NameParser\Part\Lastname;
use TheIconic\NameParser\Part\LastnamePrefix;
use TheIconic\NameParser\Part\Nickname;
use TheIconic\NameParser\Part\Salutation;
use TheIconic\NameParser\Part\Suffix;
use function TheIconic\NameParser\strlen;

class LastnameMapper extends AbstractMapper
{
Expand Down
8 changes: 3 additions & 5 deletions src/Part/AbstractPart.php
Expand Up @@ -2,6 +2,8 @@

namespace TheIconic\NameParser\Part;

use function TheIconic\NameParser\tcword;

abstract class AbstractPart
{
/**
Expand Down Expand Up @@ -81,10 +83,6 @@ protected function camelcase($word): string
*/
protected function camelcaseReplace($matches): string
{
if (function_exists('mb_convert_case')) {
return mb_convert_case($matches[0], MB_CASE_TITLE, 'UTF-8');
}

return ucfirst(strtolower($matches[0]));
return tcword($matches[0]);
}
}
9 changes: 9 additions & 0 deletions src/string_functions.php
@@ -0,0 +1,9 @@
<?php

if (!function_exists('TheIconic\NameParser\strlen')) {
if (extension_loaded('mbstring')) {
require __DIR__ . '/string_functions_mbstring.php';
} else {
require __DIR__ . '/string_functions_native.php';
}
}
31 changes: 31 additions & 0 deletions src/string_functions_mbstring.php
@@ -0,0 +1,31 @@
<?php

namespace TheIconic\NameParser;

if (!function_exists('TheIconic\NameParser\strlen')) {
function strlen(string $string): int
{
return \mb_strlen($string, 'UTF-8');
}
}

if (!function_exists('TheIconic\NameParser\tcword')) {
function tcword(string $string): string
{
return \mb_convert_case($string, MB_CASE_TITLE, 'UTF-8');
}
}

if (!function_exists('TheIconic\NameParser\characters')) {
function characters(string $string): array
{
return $charactersAsArray = \preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY);
}
}

if (!function_exists('TheIconic\NameParser\substr')) {
function substr(string $string, int $start, int $length = null): string
{
return \mb_substr($string, $start, $length, 'UTF-8');
}
}
31 changes: 31 additions & 0 deletions src/string_functions_native.php
@@ -0,0 +1,31 @@
<?php

namespace TheIconic\NameParser;

if (!function_exists('TheIconic\NameParser\strlen')) {
function strlen(string $string): int
{
return \strlen($string);
}
}

if (!function_exists('TheIconic\NameParser\tcword')) {
function tcword(string $string): string
{
return \ucfirst(\strtolower($string));
}
}

if (!function_exists('TheIconic\NameParser\characters')) {
function characters(string $string): array
{
return \str_split($string, $split_length = 1);
}
}

if (!function_exists('TheIconic\NameParser\substr')) {
function substr(string $string, int $start, int $length = null): string
{
return \substr($string, $start, $length);
}
}

0 comments on commit 14c9327

Please sign in to comment.