Completed
Push — master ( 0f0a32...5d3cd3 )
by Sebastian
02:20
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
12
/**
13
 * Class NameHelper
14
 * @package Seboettg\CiteProc\Util
15
 * @author Sebastian Böttger <[email protected]>
16
 */
17
class NameHelper
18
{
19
20
    /**
21
     * @param \stdClass $precedingItem
22
     * @param array $currentAuthor
23
     * @return bool
24
     */
25
    public static function identicalAuthors($precedingItem, $currentAuthor)
26
    {
27
        if (count($precedingItem->author) !== count($currentAuthor)) {
28
            return false;
29
        }
30
        foreach ($currentAuthor as $current) {
31
            if (self::precedingHasAuthor($precedingItem, $current)) {
32
                continue;
33
            }
34
            return false;
35
        }
36
        return true;
37
    }
38
39
    /**
40
     * @param \stdClass $preceding
41
     * @param \stdClass $name
42
     * @return bool
43
     */
44
    public static function precedingHasAuthor($preceding, $name)
45
    {
46
        foreach ($preceding->author as $author) {
47
            if ($author->family === $name->family && $author->given === $name->given) {
48
                return true;
49
            }
50
        }
51
        return false;
52
    }
53
54
55
    /**
56
     * removes the field $particle from $data and appends its content to the $namePart field of $data
57
     * @param \stdClass $data
58
     * @param string $namePart
59
     * @param string $particle
60
     */
61 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...
62
    {
63
        if (isset($data->{$particle}) && isset($data->{$namePart})) {
64
            $data->{$namePart} = $data->{$namePart} . " " . $data->{$particle}; // append $particle to $namePart
65
            unset($data->{$particle}); //remove particle from $data
66
        }
67
    }
68
69
    /**
70
     * removes the field $particle from $data and prepends its content to the $namePart field of $data
71
     * @param \stdClass $data
72
     * @param string $namePart ("given"|"family")
73
     * @param string $particle
74
     */
75 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...
76
    {
77
        if (isset($data->{$particle}) && isset($data->{$namePart})) {
78
            $data->{$namePart} = $data->{$particle} . " " . $data->{$namePart}; //prepend $particle to $namePart
79
            unset($data->{$particle});//remove particle from $data
80
        }
81
    }
82
}