1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace rdx\imap; |
4
|
|
|
|
5
|
|
|
use rdx\imap\IMAPException; |
6
|
|
|
use rdx\imap\IMAPTransportInterface; |
7
|
|
|
|
8
|
|
|
class IMAPTransport implements IMAPTransportInterface { |
9
|
|
|
|
10
|
|
|
protected $resource; // imap_open() resource |
11
|
|
|
|
12
|
|
|
public function open( $server, $username, $password, $mailbox, array $flags ) { |
13
|
|
|
if ( !empty($flags) ) { |
14
|
|
|
$server .= '/' . implode('/', $flags); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
$mailbox = '{' . $server . '}' . $mailbox; |
18
|
|
|
$this->resource = @imap_open($mailbox, $username, $password); |
19
|
|
|
if ( !$this->resource ) { |
20
|
|
|
$error = imap_last_error(); |
21
|
|
|
imap_errors(); |
22
|
|
|
imap_alerts(); |
23
|
|
|
throw new IMAPException($error); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
return $this; |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function headers() { |
30
|
|
|
return imap_headers($this->resource); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function utf8( $string ) { |
34
|
|
|
return imap_utf8($string); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function headerinfo( $msgNumber ) { |
38
|
|
|
return $this->iteratorToLowercaseArray(imap_headerinfo($this->resource, $msgNumber)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function unflag( $msgNumber, $flag ) { |
42
|
|
|
return imap_clearflag_full($this->resource, $msgNumber, $flag); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function flag( $msgNumber, $flag ) { |
46
|
|
|
return imap_setflag_full($this->resource, $msgNumber, $flag); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function fetchstructure( $msgNumber ) { |
50
|
|
|
return imap_fetchstructure($this->resource, $msgNumber); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function fetchbody( $msgNumber, $section ) { |
54
|
|
|
return quoted_printable_decode(imap_fetchbody($this->resource, $msgNumber, $section, FT_PEEK)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function expunge() { |
58
|
|
|
return imap_expunge($this->resource); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function delete( $msgNumber ) { |
62
|
|
|
return imap_delete($this->resource, $msgNumber); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function mailboxmsginfo() { |
66
|
|
|
return (object) $this->iteratorToLowercaseArray(imap_mailboxmsginfo($this->resource)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function iteratorToLowercaseArray( $iterator ) { |
70
|
|
|
$data = []; |
71
|
|
|
foreach ( $iterator as $name => $value ) { |
72
|
|
|
$data[ strtolower($name) ] = $value; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.