IMAPMessageContent::header()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace rdx\imap;
4
5
abstract class IMAPMessageContent implements IMAPMessagePartInterface {
6
7
	/** @var object */
8
	protected $structure;
9
10
	/** @var IMAPMessagePart[] */
11
	protected $parts = null;
12
13
	/** @var string[][] */
14
	protected $headers = null;
15
16
	/** @var array */
17
	protected $parameters = null;
18
19
	/** @return IMAPMessagePart[] */
20
	abstract public function parts();
21
22
	/** @return IMAPMessagePart[] */
23
	public function allParts() {
24
		$parts = [];
25
		$iterate = function( IMAPMessagePartInterface $message ) use (&$iterate, &$parts) {
26
			foreach ( $message->parts() as $part ) {
27
				$parts[] = $part;
28
29
				if ( count($part->parts()) ) {
30
					$iterate($part);
31
				}
32
			}
33
		};
34
35
		$iterate($this);
36
		return $parts;
37
	}
38
39
	/** @return IMAPMessagePart */
40
	public function part( $index ) {
41
		$parts = $this->parts();
42
		return @$parts[$index];
43
	}
44
45
	/** @return array */
46
	public function parameters() {
47
		if ( $this->parameters === null ) {
48
			$this->parameters = [];
49
			$structure = $this->structure();
50
51
			$this->parameters['bytes'] = @$structure->bytes;
52
53
			foreach ( (array) @$structure->parameters as $param ) {
54
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
55
			}
56
			foreach ( (array) @$structure->dparameters as $param ) {
57
				$this->parameters[ strtolower($param->attribute) ] = $param->value;
58
			}
59
		}
60
61
		return $this->parameters;
62
	}
63
64
	/** @return mixed */
65
	public function parameter( $name ) {
66
		$parameters = $this->parameters();
67
		$structure = $this->structure();
68
		return $parameters[ strtolower($name) ] ?? $structure->$name ?? null;
69
	}
70
71
	/** @return string[][] */
72
	public function headers() {
73
		if ( $this->headers === null ) {
74
			$header = $this->headerString();
75
			if ( !$header ) {
76
				return $this->headers = [];
77
			}
78
79
			$headers = preg_split('#[\r\n]+(?=\w)#', $header);
80
			$headers = array_map('mb_decode_mimeheader', $headers);
81
82
			$this->headers = [];
83
			foreach ( $headers as $header ) {
84
				$x = explode(':', $header, 2);
85
				$this->headers[ trim(strtolower($x[0])) ][] = trim($x[1]);
86
			}
87
		}
88
89
		return $this->headers;
90
	}
91
92
	/** @return string[] */
93
	public function header( $name ) {
94
		$headers = $this->headers();
95
		return $headers[ strtolower($name) ] ?? [];
96
	}
97
98
	/** @return IMAPMessagePartInterface[] */
99
	public function subtypeParts( $subtypes, $recursive ) {
100
		$subtypes = (array) $subtypes;
101
102
		$parts = $recursive ? $this->allParts() : $this->parts();
103
		array_unshift($parts, $this);
104
105
		return array_values(array_filter($parts, function( IMAPMessagePartInterface $part ) use ($subtypes) {
106
			return in_array($part->subtype(), $subtypes);
107
		}));
108
	}
109
110
	/** @return IMAPMessagePartInterface */
111
	public function subtypePart( $subtypes, $recursive ) {
112
		if ( count($parts = $this->subtypeParts($subtypes, $recursive)) ) {
113
			return $parts[0];
114
		}
115
	}
116
117
	/** @return string */
118
	public function subtypeContent( $subtypes, $recursive ) {
119
		if ( $part = $this->subtypePart($subtypes, $recursive) ) {
120
			return $part->content();
121
		}
122
123
		return '';
124
	}
125
126
	/** @return string */
127
	public function text( $recursive = false ) {
128
		return $this->subtypeContent($this->mailbox()->getTextSubtypes(), $recursive);
129
	}
130
131
	/** @return string */
132
	public function html( $recursive = false ) {
133
		return $this->subtypeContent($this->mailbox()->getHtmlSubtypes(), $recursive);
134
	}
135
136
}
137