Skip to content

Commit 22bd7ab

Browse files
committed
fix review issue, update readme
1 parent 5976c9a commit 22bd7ab

2 files changed

Lines changed: 58 additions & 6 deletions

File tree

README.md

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ composer require heimrichhannot/salutation-creator
1313

1414
## Requirements
1515

16-
- PHP ^8.1
16+
- PHP ^8.2
1717
- symfony/translation-contracts ^1 || ^2 || ^3
1818

1919
## Features
@@ -22,6 +22,7 @@ composer require heimrichhannot/salutation-creator
2222
- ✅ Support for academic titles (Dr., Prof., etc.)
2323
- ✅ Gender-sensitive salutations
2424
- ✅ Prefix and suffix titles with priorities
25+
- ✅ Name parsing from strings and arrays
2526
- ✅ Multilingual translations via Symfony Translation
2627
- ✅ Extensible with custom name, gender, and title classes
2728

@@ -58,9 +59,8 @@ $context = (new SalutationContext())
5859
->setGender(Gender::FEMALE)
5960
->addTitle(new GermanDoctorTitle())
6061
->addTitle(new GermanProfessorTitle())
61-
->addTitle(new PrefixTitle('Hero')
62+
->addTitle(new PrefixTitle('Hero'))
6263
->addTitles(Titles::fromString('dr phd unknown')); // Adding titles from string
63-
;
6464

6565

6666
echo $salutationCreator->generate($context);
@@ -84,6 +84,7 @@ Gender from string:
8484
use HeimrichHannot\SalutationCreator\SalutationCreator;
8585
use HeimrichHannot\SalutationCreator\SalutationContext;
8686
use HeimrichHannot\SalutationCreator\Context\Gender\Gender;
87+
use HeimrichHannot\SalutationCreator\Context\Name\GermanTypeName;
8788

8889
$data = [
8990
'firstname' => 'Lisa',
@@ -93,12 +94,58 @@ $data = [
9394

9495
$salutationContext = (new SalutationContext())
9596
->setName(new GermanTypeName($data['firstname'], $data['lastname']))
96-
->addTitles(Gender::tryFromString($data['gender'])));
97+
->setGender(Gender::tryFromString($data['gender']));
9798

9899
echo (new SalutationCreator($this->translator))->generate($salutationContext);
99100
// Outputs "Sehr geehrte Frau Müller"
100101
```
101102

103+
## Name Parsing
104+
105+
Use the bundled name parsers to create `AbstractName` instances from strings or arrays before passing them to a `SalutationContext`.
106+
107+
### Parse a name string
108+
109+
```php
110+
use HeimrichHannot\SalutationCreator\Context\Name\GermanTypeName;
111+
use HeimrichHannot\SalutationCreator\Parser\Name\StringParser;
112+
113+
$name = StringParser::create(GermanTypeName::class)->parse('Max Mustermann');
114+
115+
echo $name?->getFullName();
116+
// Output: "Max Mustermann"
117+
```
118+
119+
The string parser supports common formats such as `Max Mustermann` and `Mustermann, Max`. For single-part names, parsing returns `null` by default because the result is incomplete.
120+
121+
If incomplete names are acceptable for your use case, enable them explicitly:
122+
123+
```php
124+
$name = StringParser::create(GermanTypeName::class)
125+
->allowIncomplete(true)
126+
->parse('Mustermann');
127+
128+
echo $name?->getFormalName();
129+
// Output: "Mustermann"
130+
```
131+
132+
### Parse name data from arrays
133+
134+
```php
135+
use HeimrichHannot\SalutationCreator\Context\Name\GermanTypeName;
136+
use HeimrichHannot\SalutationCreator\Parser\Name\ArrayParser;
137+
138+
$name = ArrayParser::create(GermanTypeName::class)->parse([
139+
'firstname' => 'Max',
140+
'lastname' => 'Mustermann',
141+
]);
142+
143+
echo $name?->getFullName();
144+
// Output: "Max Mustermann"
145+
```
146+
147+
For `GermanTypeName`, the array parser supports English keys (`firstname`, `lastname`, `name`) and German keys (`vorname`, `nachname`, `zuname`, `name`). Non-string values are ignored.
148+
102149
## Translations / Translation files
103150

104151
Since this library in framework-agnostic, you need to register the translation files manually or copy them to your project's translation directory.
@@ -196,6 +243,11 @@ The following parameters are available in translations:
196243

197244
- `GermanTypeName`: German names with first and last name
198245

246+
### Name Parser Classes
247+
248+
- `StringParser`: Parses full names from plain strings
249+
- `ArrayParser`: Parses first and last names from array data
250+
199251
### Gender
200252

201253
- `Gender::MALE` - Male
@@ -310,4 +362,4 @@ class IcelandTypeName extends AbstractName
310362
return $this->firstName;
311363
}
312364
}
313-
```
365+
```

src/Parser/Name/Rule/Result.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function with(Result $result): Result
2222
true,
2323
'' !== $result->firstName ? $result->firstName : $this->firstName,
2424
'' !== $result->lastName ? $result->lastName : $this->lastName,
25-
$this->origin // oder $result->origin, je nach gewünschter Semantik
25+
$result->origin
2626
);
2727
}
2828

0 commit comments

Comments
 (0)