name-parser icon indicating copy to clipboard operation
name-parser copied to clipboard

Output in vCard (RFC6350) matching form

Open blacksenator opened this issue 4 years ago • 2 comments

According to this question I really would appreciate to get a feature solving my needs. My idea is, that the return of "vCard-function" returns an array with key is vCard property name and value is name (part). Steps:

  1. Try to figure out whether it´s a company or a person. From my point of view only a language related list of key words (user extendable) can solve this. If this matches, than return 'FN' and 'ORG' containing the name string as befor.
  2. If the name string is obviously not a company name, then try to splitt it into its components
  3. Rearrange components and return them as 'FN', 'N' or maybe 'NICKNAME'.

Here is my attempt from last night (draft):

    /**
     * get an array of name properties with vCard property as key
     *
     * @param string $realname
     * @return array
     */
    public function getNameProperties(string $realName)
    {
        $nameParts = explode(',', $realName);                       // "lastname, firstname"
        if (count($nameParts) == 2) {                               // it`s a person
            $nameParts  = $this->parser->parse($realName);
            $salutation = $nameParts->getSalutation();
            $firstName  = $nameParts->getFirstname();
            $lastName   = $nameParts->getLastname();
            $middleName = $nameParts->getMiddlename();
            $nickName   = $nameParts->getNickname();
            $initials   = $nameParts->getInitials();
            $suffix     = $nameParts->getSuffix();
            if (!empty($middleName) && empty($initials)) {
                $additionalName = $middleName;
            } elseif (empty($middleName) && !empty($initials)) {
                $additionalName = $initials;
            } elseif (empty($middleName) && empty($initials)) {
                $additionalName = '';
            } else {
                $additionalName = implode(',', [$middleName, $initials]);
            }
            $names = implode(';', [$lastName, $firstName, $additionalName, $salutation, $suffix]);
            $fullName = implode(' ', [$salutation, $firstName, $additionalName, $lastName]);
            if (!empty($suffix)) {
                $fullName = $fullName .', '. $suffix;
            }
            $fullName = preg_replace('/\s+/', ' ', $fullName);
            $company = '';
        } else {                                                    // it`s a company
            $names = '';
            $nickName = '';
            $fullName = $realName;
            $company = $realName;
        }

        return [
            'N'        => $names,
            'FN'       => $fullName,
            'NICKNAME' => $nickName,
            'ORG'      => $company,
        ];
    }

blacksenator avatar Dec 06 '19 08:12 blacksenator

@blacksenator Thanks for sharing this use case. An interesting way to use the package.

Is there anything this solution doesn't cover for you? We would like to keep the name-parser package focussed on just the core functionality as possible use cases are plentiful and the package could easily loose focus if we try to incorporate too many of them. However, if there is an aspect about this that the parser doesn't cater for, i'd be happy to look at that.

It might also be nice to permanently make these solutions and use cases available - i could add the github wiki functionality if you feel that might be a good way?

wyrfel avatar Dec 12 '19 11:12 wyrfel

@wyrfel - Thank you Andre for your answer. First of all, I would like to say that your program is clever, so sophisticated, that I am - with my PHP knowledge - be still lost in the code of my fork. In my opinion, there are actually a few things missing - at least for German standards. Whether the repository loses its focus - I can not say.

But step by step: In my view, the main thing that is missing is an examination of whether it is a person or a company. In my opinion, the examination can be made relatively trivial:

  • if the string does not contain a comma (count ($segments) <= 1 in Parser.php line 77 et seqq.)
  • check against language-dependent character strings (e.g., "Ltd.", "Inc.")
  • if true no further mappings are necessary
  • return the whole string in the new "Company" field

Now to the German peculiarities: When checking personal names, it would be very useful if the following two name components were identified individually:

  1. Title - this primarily means German doctoral degrees. They are not officially part of the name - although many believe it - and are therefore typically used as part of the name. These academic titles can occur several times: Example: [Salutation] [Title] [Title] [Firstname] [Prefix] [Lastname] [Lastname], [Title] [Title] [Firstname] [Prefix]

  2. What is not taken into account are nobility predicates. This predicates have been carried out since 1918 (abolition of the nobility) as a name extension (it´s not optional it´s part of the name!). An aristocratic predicate occurs only once in the name, and if so, then usually followed by a prefix ("baron" "of") Example (very simple cases): [Salutation] [Title] [Firstname] [Extension] [Prefix] [Lastname] [Lastname], [Title] [Firstname] [Extension] [Prefix] The good thing is that there is an official source with a list of "allowed" extensions: the German health system.

As I said, the code is programmed so abstractly that I have not yet been able to implement a proper PR. My knowledge is limited, so I just expanded the basics with the lists of parts of the name and copied classes accordingly.

If you are interesseted in this wrought material I will push an PR on your request.

blacksenator avatar Dec 12 '19 13:12 blacksenator