1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rdx\imap; |
4
|
|
|
|
5
|
|
|
abstract class IMAPMessageContent { |
6
|
|
|
|
7
|
|
|
protected $structure; // stdClass |
8
|
|
|
protected $parts = []; // Array<rdx\imap\IMAPMessagePart> |
9
|
|
|
protected $parameters = []; |
10
|
|
|
|
11
|
|
|
public function allParts( $withContainers = false ) { |
12
|
|
|
$parts = []; |
13
|
|
|
$iterate = function($message) use (&$iterate, &$parts, $withContainers) { |
14
|
|
|
foreach ( $message->parts() as $part ) { |
15
|
|
|
if ( $part->parts() ) { |
16
|
|
|
if ( $withContainers ) { |
17
|
|
|
$parts[] = $part; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
$iterate($part); |
21
|
|
|
} |
22
|
|
|
else { |
23
|
|
|
$parts[] = $part; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
}; |
27
|
|
|
|
28
|
|
|
$iterate($this); |
29
|
|
|
return $parts; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function part( $index ) { |
33
|
|
|
$parts = $this->parts(); |
34
|
|
|
return @$parts[$index]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function parameters() { |
38
|
|
|
if ( empty($this->parameters) ) { |
39
|
|
|
$structure = $this->structure(); |
40
|
|
|
|
41
|
|
|
$this->parameters['bytes'] = @$structure->bytes; |
42
|
|
|
|
43
|
|
|
foreach ((array) @$structure->parameters as $param) { |
44
|
|
|
$this->parameters[ strtolower($param->attribute) ] = $param->value; |
45
|
|
|
} |
46
|
|
|
foreach ((array) @$structure->dparameters as $param) { |
47
|
|
|
$this->parameters[ strtolower($param->attribute) ] = $param->value; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->parameters; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function parameter( $name ) { |
55
|
|
|
$parameters = $this->parameters(); |
56
|
|
|
return @$parameters[ strtolower($name) ]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function subtypePart( $subtypes, $recursive ) { |
60
|
|
|
$subtypes = (array) $subtypes; |
61
|
|
|
$method = [$this, $recursive ? 'allParts' : 'parts']; |
62
|
|
|
$parts = call_user_func($method); |
63
|
|
|
array_unshift($parts, $this); |
64
|
|
|
|
65
|
|
|
foreach ( $parts as $part ) { |
66
|
|
|
if ( in_array($part->subtype(), $subtypes) ) { |
67
|
|
|
return $part; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function subtypeContent( $subtypes, $recursive ) { |
73
|
|
|
if ( $part = $this->subtypePart($subtypes, $recursive) ) { |
74
|
|
|
return $part->content(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function text( $recursive = false ) { |
79
|
|
|
return $this->subtypeContent($this->mailbox()->getTextSubtypes(), $recursive); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function html( $recursive = false ) { |
83
|
|
|
return $this->subtypeContent($this->mailbox()->getHtmlSubtypes(), $recursive); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function deliveryStatus( $recursive = true ) { |
87
|
|
|
return $this->subtypeContent('DELIVERY-STATUS', $recursive); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: