Completed
Push — master ( 1650a0...29e43d )
by mw
06:30
created

Address   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 71
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hasAddress() 0 3 1
A set() 0 3 1
A text() 0 26 4
1
<?php
2
3
namespace SRF\vCard;
4
5
/**
6
 * Represents a single address entry in an vCard
7
 *
8
 * @see http://www.semantic-mediawiki.org/wiki/vCard
9
 *
10
 * @license GNU GPL v2+
11
 * @since 1.5
12
 *
13
 * @author Markus Krötzsch
14
 * @author Denny Vrandecic
15
 * @author Frank Dengler
16
 */
17
class Address {
18
19
	/**
20
	 * @var string
21
	 */
22
	private $type;
23
24
	/**
25
	 * @var array
26
	 */
27
	private $adr = [];
28
29
	/**
30
	 * @param string $type
31
	 * @param array $adr
32
	 */
33 4
	public function __construct( $type, array $adr = [] ) {
34 4
		$this->type = $type;
35 4
		$this->adr = $adr;
36 4
	}
37
38
	/**
39
	 * @since 3.1
40
	 *
41
	 * @return boolean
42
	 */
43 2
	public function hasAddress() {
44 2
		return $this->adr !== [];
45
	}
46
47
	/**
48
	 * @since 3.1
49
	 *
50
	 * @param string $key
51
	 * @param string $value
52
	 */
53 2
	public function set( $key, $value ) {
54 2
		$this->adr[$key] = $value;
55 2
	}
56
57
	/**
58
	 * @return string
59
	 */
60 2
	public function text() {
61
62 2
		if ( $this->type == "" ) {
63 1
			$this->type = "WORK";
64
		}
65
66 2
		$adr = [];
67
68
		// Expected sequence as defined by
69
		// https://tools.ietf.org/html/rfc6350#section-6.3.1
70
		$map = [
71 2
			'pobox',
72
			'ext',
73
			'street',
74
			'locality',
75
			'region',
76
			'code',
77
			'country'
78
		];
79
80 2
		foreach ( $map as $k ) {
81 2
			$adr[] = isset( $this->adr[$k] ) ? vCard::escape( $this->adr[$k] ) : '';
82
		}
83
84 2
		return "ADR;TYPE=$this->type;CHARSET=UTF-8:" . implode( ';', $adr ) . "\r\n";
85
	}
86
87
}
88