Skip to content
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
37 changes: 37 additions & 0 deletions .github/workflows/compatibility.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: PHP Compatibility

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
php-compatibility:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
extensions: dom, curl, libxml, mbstring, zip
coverage: none

- name: Install dependencies
run: |
composer update --prefer-dist --no-interaction
composer require --dev phpcompatibility/php-compatibility

- name: Install PHPCS
run: |
composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require --dev dealerdirect/phpcodesniffer-composer-installer

- name: Check PHP compatibility
run: |
vendor/bin/phpcs --config-set installed_paths vendor/phpcompatibility/php-compatibility
vendor/bin/phpcs -p src/ --standard=PHPCompatibility --runtime-set testVersion 8.1-
42 changes: 42 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
tests:
runs-on: ubuntu-latest

strategy:
fail-fast: true
matrix:
php: [8.1, 8.2, 8.3, '8.4']
stability: [prefer-stable]

name: PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip
coverage: none

- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit

- name: Check coding standards
run: vendor/bin/phpcs --standard=PSR12 -n src/ tests/

- name: Static analysis
run: vendor/bin/phpstan analyse
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/vendor/
composer.lock
.idea/
.idea/.phpunit.cache/
11 changes: 6 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
language: php
php:
- "7.3"
- "7.2"
- "7.1"
- "8.1"
- "8.2"
- "8.3"
- "8.4"

install: composer install

script:
- vendor/bin/phpunit
- vendor/bin/phpstan.phar analyse -l max -c phpstan.neon src --no-interaction --no-progress

- vendor/bin/phpstan analyse
- vendor/bin/phpcs --standard=PSR12 src/ tests/

notifications:
recipients:
Expand Down
27 changes: 27 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2025-05-14
### Added
- Support for PHP 8.1, 8.2, 8.3, 8.4
- GitHub Actions workflows for CI
- Improved type declarations with PHPStan annotations
- Enhanced documentation and examples
- PHPStan level 8 compliance
- PSR-12 code style

### Changed
- Modernized code style to follow PSR-12
- Improved null safety with proper null checks
- Refactored to use modern PHP features
- Updated constructor parameter types for better type safety

### Removed
- Support for PHP 7.x and 8.0
- Removed Travis CI in favor of GitHub Actions

## [1.0.0] - Previous version
- Initial release with PHP 7.1+ support
105 changes: 102 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

<p align="center">
<a href="(https://travis-ci.org/davidgorges/HumanNameParser.php"><img src="https://travis-ci.org/davidgorges/HumanNameParser.php.png"></a>
<a href="https://github.com/davidgorges/HumanNameParser.php/actions/workflows/tests.yml"><img src="https://github.com/davidgorges/HumanNameParser.php/actions/workflows/tests.yml/badge.svg" alt="Tests"></a>
<a href="https://packagist.org/packages/davidgorges/human-name-parser"><img src="https://poser.pugx.org/davidgorges/human-name-parser/v/stable" alt="Latest Stable Version"></a>
<a href="https://github.com/davidgorges/HumanNameParser.php"><img src="https://img.shields.io/badge/PHPStan-enabled-brightgreen.svg?style=flat" alt="PHPStan Enabled"></a>
</p>

------

*Note*: The 1.0 release requires PHP > 7.1.
*Note*: Version 2.0 requires PHP >= 8.1. For PHP 7.1-8.0 support, use version 1.x.

# Description
Fork from HumanNameParser.php origninally by Jason Priem <jason@jasonpriem.com>. Takes human names of arbitrary complexity and various wacky formats like:
Expand All @@ -28,6 +28,16 @@ and parses out the:
- title (like 'Dr.', 'Prof') *new*


# Requirements

- PHP 8.1, 8.2, 8.3, 8.4 or higher

# Installation

```bash
composer require davidgorges/human-name-parser
```

# How to use

```php
Expand All @@ -36,5 +46,94 @@ use HumanNameParser\Parser;
$nameparser = new Parser();
$name = $nameparser->parse("Alfonso Ribeiro");

echo "Hello " . $name->getFirstName();
echo "Hello " . $name->getFirstName(); // "Hello Alfonso"
```

## More Examples

```php
// Basic usage
$parser = new Parser();

// Simple name
$name = $parser->parse("John Smith");
echo $name->getFirstName(); // "John"
echo $name->getLastName(); // "Smith"

// Name with title
$name = $parser->parse("Dr. Jane Smith");
echo $name->getAcademicTitle(); // "Dr."
echo $name->getFirstName(); // "Jane"
echo $name->getLastName(); // "Smith"

// Name with middle name
$name = $parser->parse("John William Smith");
echo $name->getFirstName(); // "John"
echo $name->getMiddleName(); // "William"
echo $name->getLastName(); // "Smith"

// Name with suffix
$name = $parser->parse("John Smith Jr.");
echo $name->getFirstName(); // "John"
echo $name->getLastName(); // "Smith"
echo $name->getSuffix(); // "Jr."

// Reversed name (last, first)
$name = $parser->parse("Smith, John");
echo $name->getFirstName(); // "John"
echo $name->getLastName(); // "Smith"

// Complex name
$name = $parser->parse("Dr. John W. ('Johnny') Smith-Brown, III");
echo $name->getAcademicTitle(); // "Dr."
echo $name->getFirstName(); // "John"
echo $name->getMiddleName(); // "W."
echo $name->getNicknames(); // "Johnny"
echo $name->getLastName(); // "Smith-Brown"
echo $name->getSuffix(); // "III"
```

## Custom Configuration

```php
// Configure the parser with custom options
$parser = new Parser([
'suffixes' => ['esq', 'jr', 'sr', 'ii', 'iii', 'iv'],
'prefixes' => ['van', 'von', 'de', 'del', 'da', 'la'],
'academic_titles' => ['dr', 'prof', 'mr', 'mrs', 'ms'],
'mandatory_first_name' => true,
'mandatory_last_name' => true
]);

// Parse name with prefix
$name = $parser->parse("Vincent van Gogh");
echo $name->getFirstName(); // "Vincent"
echo $name->getLastName(); // "van Gogh"
```

# Try It Out

The library includes an example script that you can use to test parsing various names:

```bash
# After installing the library
composer install
php examples/parse_name.php

# Or parse a specific name directly
php examples/parse_name.php "Dr. John Smith Jr."
```

# Testing

```bash
composer install
vendor/bin/phpunit
```

# Static Analysis & Coding Standards

```bash
vendor/bin/phpstan analyse
vendor/bin/phpcs --standard=PSR12 src/ tests/
```
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
}
],
"require": {
"php": ">=7.1"
"php": ">=8.1"
},
"require-dev": {
"phpunit/phpunit": "7.*",
"phpstan/phpstan-shim": "^0.12.0"
"phpunit/phpunit": "^10.0",
"phpstan/phpstan": "^1.10",
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
"psr-4": {"HumanNameParser\\": "src/HumanNameParser"}
Expand Down
66 changes: 66 additions & 0 deletions examples/parse_name.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use HumanNameParser\Parser;

// Check if a name was provided as a command-line argument
if ($argc > 1) {
// Get the name from the command-line arguments
$nameString = implode(' ', array_slice($argv, 1));
} else {
// Example names to parse
$examples = [
"John Smith",
"Dr. Jane Smith",
"Smith, John",
"John William Smith",
"John Smith Jr.",
"Dr. John W. ('Johnny') Smith-Brown, III",
"Vincent van Gogh",
"James C. O'Neill",
"Björn O'Malley, Jr."
];

// Let user choose an example or enter their own name
echo "Choose an example to parse:\n";
foreach ($examples as $i => $example) {
echo ($i + 1) . ". $example\n";
}
echo ($i + 2) . ". Enter your own name\n";
echo "Your choice (1-" . ($i + 2) . "): ";

$choice = (int) trim(fgets(STDIN));

if ($choice === count($examples) + 1) {
echo "Enter a name to parse: ";
$nameString = trim(fgets(STDIN));
} elseif ($choice > 0 && $choice <= count($examples)) {
$nameString = $examples[$choice - 1];
} else {
echo "Invalid choice. Using first example.\n";
$nameString = $examples[0];
}
}

// Create a new Parser instance
$parser = new Parser();

try {
// Parse the name
$name = $parser->parse($nameString);

// Display the results
echo "\nParsing results for: " . $nameString . "\n";
echo str_repeat("-", 40) . "\n";
echo "Academic Title: " . ($name->getAcademicTitle() ?? "N/A") . "\n";
echo "Leading Initial: " . ($name->getLeadingInitial() ?? "N/A") . "\n";
echo "First Name: " . ($name->getFirstName() ?? "N/A") . "\n";
echo "Nicknames: " . ($name->getNicknames() ?? "N/A") . "\n";
echo "Middle Name: " . ($name->getMiddleName() ?? "N/A") . "\n";
echo "Last Name: " . ($name->getLastName() ?? "N/A") . "\n";
echo "Suffix: " . ($name->getSuffix() ?? "N/A") . "\n";

} catch (Exception $e) {
echo "Error parsing name: " . $e->getMessage() . "\n";
}
12 changes: 12 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<ruleset name="HumanNameParser">
<description>PSR-12 coding standard</description>

<arg name="colors"/>
<arg value="sp"/>

<file>src</file>
<file>tests</file>

<rule ref="PSR12"/>
</ruleset>
5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 8
paths:
- src
- tests
12 changes: 10 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
colors="true"
bootstrap="tests/bootstrap.php">
bootstrap="tests/bootstrap.php"
executionOrder="random"
cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Human Name Parser tests">
<directory>Tests</directory>
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<ini name="display_errors" value="On" />
<ini name="error_reporting" value="-1" />
</php>
</phpunit>
Loading