Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract string functions to support both mbstring and native #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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);
}
}