Completed
Branch master (e8947e)
by Andreas
15:09
created

hcard.php ➔ hKit_implied_n_optimization()   D

Complexity

Conditions 9
Paths 5

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 5
nop 1
dl 0
loc 25
rs 4.909
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 54 and the first side effect is on line 12.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @package org.openpsa.httplib
4
 * @author The Midgard Project, http://www.midgard-project.org
5
 * @copyright The Midgard Project, http://www.midgard-project.org
6
 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * hcard profile for hkit
11
 */
12
    $this->root_class = 'vcard';
13
14
    $this->classes = array(
15
        'fn', array('honorific-prefix', 'given-name', 'additional-name', 'family-name', 'honorific-suffix'),
16
        'n', array('honorific-prefix', 'given-name', 'additional-name', 'family-name', 'honorific-suffix'),
17
        'adr', array('post-office-box', 'extended-address', 'street-address', 'postal-code', 'country-name', 'type', 'region', 'locality'),
18
        'label', 'bday', 'agent', 'nickname', 'photo', 'class',
19
        'email', array('type', 'value'),
20
        'category', 'key', 'logo', 'mailer', 'note',
21
        'org', array('organization-name', 'organization-unit'),
22
        'tel', array('type', 'value'),
23
        'geo', array('latitude', 'longitude'),
24
        'tz', 'uid', 'url', 'rev', 'role', 'sort-string', 'sound', 'title'
25
    );
26
27
    // classes that must only appear once per card
28
    $this->singles = array(
29
        'fn'
30
    );
31
32
    // classes that are required (not strictly enforced - give at least one!)
33
    $this->required = array(
34
        'fn'
35
    );
36
37
    $this->att_map = array(
38
        'fn'    => array('IMG|alt'),
39
        'url'    => array('A|href', 'IMG|src', 'AREA|href'),
40
        'photo'    => array('IMG|src'),
41
        'bday'    => array('ABBR|title'),
42
        'logo'    => array('IMG|src'),
43
        'email' => array('A|href'),
44
        'geo'    => array('ABBR|title')
45
    );
46
47
    $this->callbacks = array(
48
        'url'    => array($this, 'resolvePath'),
49
        'photo'    => array($this, 'resolvePath'),
50
        'logo'    => array($this, 'resolvePath'),
51
        'email'    => array($this, 'resolveEmail')
52
    );
53
54
    function hKit_hcard_post($a)
55
    {
56
        foreach ($a as &$vcard)
57
        {
58
            hKit_implied_n_optimization($vcard);
59
            hKit_implied_n_from_fn($vcard);
60
        }
61
62
        return $a;
63
    }
64
65
    function hKit_implied_n_optimization(&$vcard)
66
    {
67
        if (array_key_exists('fn', $vcard) && !is_array($vcard['fn']) &&
68
            !array_key_exists('n', $vcard) && (!array_key_exists('org', $vcard) || $vcard['fn'] != $vcard['org']))
69
            {
70
            if (sizeof(explode(' ', $vcard['fn'])) == 2){
71
                $patterns    = array();
72
                $patterns[] = array('/^(\S+),\s*(\S{1})$/', 2, 1);         // Lastname, Initial
73
                $patterns[] = array('/^(\S+)\s*(\S{1})\.*$/', 2, 1);     // Lastname Initial(.)
74
                $patterns[] = array('/^(\S+),\s*(\S+)$/', 2, 1);         // Lastname, Firstname
75
                $patterns[] = array('/^(\S+)\s*(\S+)$/', 1, 2);         // Firstname Lastname
76
77
                foreach ($patterns as $pattern){
78
                    if (preg_match($pattern[0], $vcard['fn'], $matches) === 1){
79
                        $n                    = array();
80
                        $n['given-name']    = $matches[$pattern[1]];
81
                        $n['family-name']    = $matches[$pattern[2]];
82
                        $vcard['n']            = $n;
83
84
                        break;
85
                    }
86
                }
87
            }
88
        }
89
    }
90
91
    function hKit_implied_n_from_fn(&$vcard)
92
    {
93
        if (array_key_exists('fn', $vcard) && is_array($vcard['fn'])
94
            && !array_key_exists('n', $vcard) && (!array_key_exists('org', $vcard) || $vcard['fn'] != $vcard['org']))
95
            {
96
            $vcard['n']        = $vcard['fn'];
97
        }
98
99
        if (array_key_exists('fn', $vcard) && is_array($vcard['fn'])){
100
            $vcard['fn']    = $vcard['fn']['text'];
101
        }
102
    }
103