Completed
Push — master ( 5d3cd3...160b9c )
by Sebastian
10:22
created

NameHelper::sameNames()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 2
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * citeproc-php
4
 *
5
 * @link        http://github.com/seboettg/citeproc-php for the source repository
6
 * @copyright   Copyright (c) 2017 Sebastian Böttger.
7
 * @license     https://opensource.org/licenses/MIT
8
 */
9
10
namespace Seboettg\CiteProc\Util;
11
use Seboettg\CiteProc\Exception\CiteProcException;
12
13
/**
14
 * Class NameHelper
15
 * @package Seboettg\CiteProc\Util
16
 * @author Sebastian Böttger <[email protected]>
17
 */
18
class NameHelper
19
{
20
21
    /**
22
     * @param \stdClass $precedingItem
23
     * @param array $currentAuthor
24
     * @return bool
25
     */
26
    public static function identicalAuthors($precedingItem, $currentAuthor)
27
    {
28
        if (count($precedingItem->author) !== count($currentAuthor)) {
29
            return false;
30
        }
31
        foreach ($currentAuthor as $current) {
32
            if (self::precedingHasAuthor($precedingItem, $current)) {
33
                continue;
34
            }
35
            return false;
36
        }
37
        return true;
38
    }
39
40
    /**
41
     * @param \stdClass $preceding
42
     * @param \stdClass $name
43
     * @return bool
44
     */
45
    public static function precedingHasAuthor($preceding, $name)
46
    {
47
        foreach ($preceding->author as $author) {
48
            if ($author->family === $name->family && $author->given === $name->given) {
49
                return true;
50
            }
51
        }
52
        return false;
53
    }
54
55
56
    /**
57
     * removes the field $particle from $data and appends its content to the $namePart field of $data
58
     * @param \stdClass $data
59
     * @param string $namePart
60
     * @param string $particle
61
     */
62 View Code Duplication
    public static function appendParticleTo(&$data, $namePart, $particle)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
    {
64
        if (isset($data->{$particle}) && isset($data->{$namePart})) {
65
            $data->{$namePart} = $data->{$namePart} . " " . $data->{$particle}; // append $particle to $namePart
66
            unset($data->{$particle}); //remove particle from $data
67
        }
68
    }
69
70
    /**
71
     * removes the field $particle from $data and prepends its content to the $namePart field of $data
72
     * @param \stdClass $data
73
     * @param string $namePart ("given"|"family")
74
     * @param string $particle
75
     */
76 View Code Duplication
    public static function prependParticleTo(&$data, $namePart, $particle)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        if (isset($data->{$particle}) && isset($data->{$namePart})) {
79
            $data->{$namePart} = $data->{$particle} . " " . $data->{$namePart}; //prepend $particle to $namePart
80
            unset($data->{$particle});//remove particle from $data
81
        }
82
    }
83
84
    /**
85
     * @param array $persons1
86
     * @param array $persons2
87
     * @return bool
88
     */
89
    public static function sameNames($persons1, $persons2)
90
    {
91
        $same = count($persons1) === count($persons2);
92
93
        if (!$same) {
94
            return false;
95
        }
96
97
        array_walk($persons1, function($name, $key) use ($persons2, &$same) {
98
            $family1 = $name->family;
99
            $family2 = $persons2[$key]->family;
100
            $same = $same && ($family1 === $family2);
101
        });
102
103
        return (bool) $same;
104
    }
105
106
    /**
107
     * @param $data
108
     * @return string
109
     * @throws CiteProcException
110
     */
111
    public static function normalizeName($data)
112
    {
113
        if (empty($data->family)) {
114
            throw new CiteProcException("Illegal argument. Name has no family name.");
115
        }
116
        return $data->family . (isset($data->given) ? $data->given : "");
117
    }
118
}