NameHelper   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
c 1
b 0
f 0
dl 0
loc 123
rs 10
wmc 27

7 Methods

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