Completed
Push — master ( bf66ca...210cc2 )
by Sebastian
05:53
created

NameHelper::prependParticleTo()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 3
dl 7
loc 7
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\CiteProc;
12
use Seboettg\CiteProc\Exception\CiteProcException;
13
14
/**
15
 * Class NameHelper
16
 * @package Seboettg\CiteProc\Util
17
 * @author Sebastian Böttger <[email protected]>
18
 */
19
class NameHelper
20
{
21
22
    /**
23
     * @param \stdClass $precedingItem
24
     * @param array $currentAuthor
25
     * @return bool
26
     */
27
    public static function identicalAuthors($precedingItem, $currentAuthor)
28
    {
29
        if (count($precedingItem->author) !== count($currentAuthor)) {
30
            return false;
31
        }
32
        foreach ($currentAuthor as $current) {
33
            if (self::precedingHasAuthor($precedingItem, $current)) {
34
                continue;
35
            }
36
            return false;
37
        }
38
        return true;
39
    }
40
41
    /**
42
     * @param \stdClass $preceding
43
     * @param \stdClass $name
44
     * @return bool
45
     */
46
    public static function precedingHasAuthor($preceding, $name)
47
    {
48
        foreach ($preceding->author as $author) {
49
            if ($author->family === $name->family && $author->given === $name->given) {
50
                return true;
51
            }
52
        }
53
        return false;
54
    }
55
56
57
    /**
58
     * removes the field $particle from $data and appends its content to the $namePart field of $data
59
     * @param \stdClass $data
60
     * @param string $namePart
61
     * @param string $particle
62
     */
63 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...
64
    {
65
        if (isset($data->{$particle}) && isset($data->{$namePart})) {
66
            $data->{$namePart} = $data->{$namePart} . " " . $data->{$particle}; // append $particle to $namePart
67
            unset($data->{$particle}); //remove particle from $data
68
        }
69
    }
70
71
    /**
72
     * removes the field $particle from $data and prepends its content to the $namePart field of $data
73
     * @param \stdClass $data
74
     * @param string $namePart ("given"|"family")
75
     * @param string $particle
76
     */
77 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...
78
    {
79
        if (isset($data->{$particle}) && isset($data->{$namePart})) {
80
            $data->{$namePart} = $data->{$particle} . " " . $data->{$namePart}; //prepend $particle to $namePart
81
            unset($data->{$particle}); //remove particle from $data
82
        }
83
    }
84
85
    /**
86
     * @param array $persons1
87
     * @param array $persons2
88
     * @return bool
89
     */
90
    public static function sameNames($persons1, $persons2)
91
    {
92
        $same = count($persons1) === count($persons2);
93
94
        if (!$same) {
95
            return false;
96
        }
97
98
        array_walk($persons1, function($name, $key) use ($persons2, &$same) {
99
            $family1 = $name->family;
100
            $family2 = $persons2[$key]->family;
101
            $same = $same && ($family1 === $family2);
102
        });
103
104
        return (bool) $same;
105
    }
106
107
    /**
108
     * @param $data
109
     * @return string
110
     * @throws CiteProcException
111
     */
112
    public static function normalizeName($data)
113
    {
114
        if (empty($data->family)) {
115
            throw new CiteProcException("Illegal argument. Name has no family name.");
116
        }
117
        return $data->family . (isset($data->given) ? $data->given : "");
118
    }
119
120
    public static function addExtendedMarkup($nameVar, $nameItem, $formattedName)
121
    {
122
        $markupExtension = CiteProc::getContext()->getMarkupExtension();
123
        if (array_key_exists($nameVar, $markupExtension)) {
124
            $function = $markupExtension[$nameVar];
125
            if (is_callable($function)) {
126
                return $function($nameItem, $formattedName);
127
            }
128 View Code Duplication
        } else if (array_key_exists($mode = CiteProc::getContext()->getMode(), $markupExtension)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
129
            if (array_key_exists($nameVar, $markupExtension[$mode])) {
130
                $function = $markupExtension[$mode][$nameVar];
131
                if (is_callable($function)) {
132
                    return $function($nameItem, $formattedName);
133
                }
134
            }
135
        }
136
        return $formattedName;
137
    }
138
}