Completed
Push — master ( 9c9dd6...7e9b13 )
by Joschi
03:42
created

NameMutatorTrait::setFamilyName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 9.4285
1
<?php
2
3
/**
4
 * apparat-dev
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Dev
8
 * @subpackage  Apparat\Dev\Infrastructure
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Dev\Infrastructure\Mutator\Traits;
38
39
use Apparat\Object\Application\Model\Properties\Domain\Contact as ContactProperties;
40
use Apparat\Object\Domain\Model\Object\ObjectInterface;
41
use Apparat\Object\Domain\Model\Properties\InvalidArgumentException;
42
use Faker\Generator;
43
44
/**
45
 * Name mutator trait
46
 *
47
 * @package Apparat\Dev
48
 * @subpackage Apparat\Dev\Infrastructure
49
 * @property Generator $generator
50
 * @method ObjectInterface setRandomHonorificPrefix(ObjectInterface $object, $probability)
51
 * @method ObjectInterface setRandomGivenName(ObjectInterface $object, $probability)
52
 * @method ObjectInterface setRandomAdditionalName(ObjectInterface $object, $probability)
53
 * @method ObjectInterface setRandomFamilyName(ObjectInterface $object, $probability)
54
 * @method ObjectInterface setRandomHonorificSuffix(ObjectInterface $object, $probability)
55
 * @method ObjectInterface setRandomNickname(ObjectInterface $object, $probability)
56
 * @method ObjectInterface setRandomSortString(ObjectInterface $object, $probability)
57
 */
58
trait NameMutatorTrait
59
{
60
    /**
61
     * Set the contact honorific prefix
62
     *
63
     * @param ObjectInterface $object Contact
64
     * @return ObjectInterface $object Contact
65
     */
66 1
    protected function setHonorificPrefix(ObjectInterface $object)
67
    {
68
        /** @noinspection PhpUndefinedMethodInspection */
69 1
        return $object->setDomainProperty(ContactProperties::HONORIFIC_PREFIX, $this->generator->title());
70
    }
71
72
    /**
73
     * Set the contact given name
74
     *
75
     * @param ObjectInterface $object Contact
76
     * @return ObjectInterface $object Contact
77
     */
78 1
    protected function setGivenName(ObjectInterface $object)
79
    {
80
        /** @noinspection PhpUndefinedMethodInspection */
81 1
        return $object->setDomainProperty(ContactProperties::GIVEN_NAME, $this->generator->firstName());
82
    }
83
84
    /**
85
     * Set the contact family name
86
     *
87
     * @param ObjectInterface $object Contact
88
     * @return ObjectInterface $object Contact
89
     */
90 1
    protected function setFamilyName(ObjectInterface $object)
91
    {
92
        /** @noinspection PhpUndefinedMethodInspection */
93 1
        return $object->setDomainProperty(ContactProperties::FAMILY_NAME, $this->generator->lastName());
94
    }
95
96
    /**
97
     * Set the contact additional name
98
     *
99
     * @param ObjectInterface $object Contact
100
     * @return ObjectInterface $object Contact
101
     */
102 1
    protected function setAdditionalName(ObjectInterface $object)
103
    {
104
        /** @noinspection PhpUndefinedMethodInspection */
105 1
        return $object->setDomainProperty(ContactProperties::ADDITIONAL_NAME, $this->generator->firstName());
106
    }
107
108
    /**
109
     * Set the contact nickname
110
     *
111
     * @param ObjectInterface $object Contact
112
     * @return ObjectInterface $object Contact
113
     */
114 1
    protected function setNickname(ObjectInterface $object)
115
    {
116
        /** @noinspection PhpUndefinedMethodInspection */
117 1
        return $object->setDomainProperty(ContactProperties::NICKNAME, $this->generator->firstName());
118
    }
119
120
    /**
121
     * Set the contact honorific suffix
122
     *
123
     * @param ObjectInterface $object Contact
124
     * @return ObjectInterface $object Contact
125
     */
126 1
    protected function setHonorificSuffix(ObjectInterface $object)
127
    {
128
        /** @noinspection PhpUndefinedMethodInspection */
129 1
        return $object->setDomainProperty(ContactProperties::HONORIFIC_SUFFIX, $this->generator->suffix());
130
    }
131
132
    /**
133
     * Set the contact sortstring
134
     *
135
     * @param ObjectInterface $object Contact
136
     * @return ObjectInterface $object Contact
137
     */
138 1
    protected function setSortString(ObjectInterface $object)
139
    {
140
        $nameParts = [
141 1
            ContactProperties::NICKNAME => '',
142 1
            ContactProperties::FAMILY_NAME => '',
143 1
            ContactProperties::HONORIFIC_SUFFIX => '',
144 1
            ContactProperties::HONORIFIC_PREFIX => '',
145 1
            ContactProperties::GIVEN_NAME => '',
146 1
            ContactProperties::ADDITIONAL_NAME => '',
147 1
        ];
148 1
        foreach (array_keys($nameParts) as $property) {
149
            try {
150 1
                $nameParts[$property] = $object->getDomainProperty($property);
151 1
            } catch (InvalidArgumentException $e) {
152 1
                continue;
153
            }
154 1
        }
155 1
        $sortStringPart1 = implode(
156 1
            ' ',
157 1
            array_filter([
158 1
                $nameParts[ContactProperties::FAMILY_NAME],
159 1
                $nameParts[ContactProperties::HONORIFIC_SUFFIX]
160 1
            ])
161 1
        );
162 1
        $sortStringPart2 = implode(
163 1
            ' ',
164 1
            array_filter([
165 1
                $nameParts[ContactProperties::HONORIFIC_PREFIX],
166 1
                $nameParts[ContactProperties::GIVEN_NAME],
167 1
                strlen($nameParts[ContactProperties::NICKNAME]) ?
168 1
                    '"'.$nameParts[ContactProperties::NICKNAME].'"' : '',
169 1
                $nameParts[ContactProperties::ADDITIONAL_NAME],
170 1
            ])
171 1
        );
172 1
        $sortString = ($sortStringPart1 ?: '---').', '.($sortStringPart2 ?: '---');
173 1
        return $object->setDomainProperty(ContactProperties::SORT_STRING, trim($sortString, ' ,'));
174
    }
175
}
176