Passed
Push — master ( a6819e...14f211 )
by
unknown
09:28 queued 02:18
created

Office::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Office Class
4
 *
5
 * @package TheyWorkForYou
6
 */
7
8
namespace MySociety\TheyWorkForYou;
9
10
/**
11
 * Office
12
 */
13
14
class Office {
15
    public $title;
16
    public $from_date;
17
    public $to_date;
18
    public $source;
19
    public $position = "";
20
    public $dept = "";
21
    public $slug = "";
22
    public $desc = "";
23
    public $external_url = "";
24
25
26
    /**
27
     * To String
28
     *
29
     * Return the office title as a string.
30
     *
31
     * @return string The title of the office, or "Unnamed Office"
32
     */
33
34
    public function __toString() {
35
        if (isset($this->title)) {
36
            return (string) $this->title;
37
        } else {
38
            return 'Unnamed Office';
39
        }
40
    }
41
42
43
    /**
44
     * Converts the description text into HTML paragraphs.
45
     *
46
     * This method takes the description text stored in the `$desc` property,
47
     * splits it into paragraphs based on newline characters, and wraps each
48
     * non-empty paragraph in `<p>` tags.
49
     * @return string The HTML representation of the description text.
50
     */
51
    public function htmlDesc() {
52
        $paragraphs = explode("\n", $this->desc);
53
        $html = '';
54
55
        foreach ($paragraphs as $paragraph) {
56
            $trimmed = trim($paragraph);
57
            if (!empty($trimmed)) {
58
                $html .= '<p>' . htmlspecialchars($trimmed, ENT_QUOTES, 'UTF-8') . '</p>';
59
            }
60
        }
61
62
        return $html;
63
    }
64
65
    /**
66
     * Pretty Dates
67
     *
68
     * Return a string containing prettified dates of this office.
69
     *
70
     * 2004-05-28 and 2004-05-13 are the first dates for data scraped from the
71
     * old selctee/privsec pages on parliament.uk (you can see this in
72
     * cmpages/chgpages' privsec0001_2004-06-08.html and
73
     * selctee0001_2004-06-08.html). So if the date is those dates for those two
74
     * things, you don't want to display it because it's not a known start date,
75
     * it could have been before that date. 2005-11-10 is because the PPS changes
76
     * did not all happen on that date but the website did not update until that
77
     * date so it outputs "before" in either from/to date in that case.
78
     * 2009-01-16 is the last date before the page disappeared off parliament.uk
79
     * entirely so that displays that fact that after then we don't know.
80
     *
81
     * @todo https://github.com/mysociety/theyworkforyou/issues/632
82
     *
83
     * @return string The dates of this office in a readable form.
84
     */
85
86
    public function pretty_dates() {
87
88
        if ($this->to_date == '9999-12-31') {
89
            return 'since ' . format_date($this->from_date, SHORTDATEFORMAT);
90
        }
91
92
        $output = '';
93
94
        if (
95
            !($this->source == 'chgpages/selctee' && $this->from_date == '2004-05-28') and
96
            !($this->source == 'chgpages/privsec' && $this->from_date == '2004-05-13')
97
        ) {
98
            if ($this->source == 'chgpages/privsec' && $this->from_date == '2005-11-10') {
99
                $output .= 'before ';
100
            }
101
            $output .= format_date($this->from_date, SHORTDATEFORMAT) . ' ';
102
        }
103
104
        $output .= 'to ';
105
106
        if ($this->source == 'chgpages/privsec' && $this->to_date == '2005-11-10') {
107
            $output .= 'before ';
108
        }
109
110
        if ($this->source == 'chgpages/privsec' && $this->to_date == '2009-01-16') {
111
            $output .= '<a href="/help/#pps_unknown">unknown</a>';
112
        } else {
113
            $output .= format_date($this->to_date, SHORTDATEFORMAT);
114
        }
115
116
        return $output;
117
118
    }
119
120
}
121