Completed
Push — 1.0.0 ( 608796...a0ab23 )
by Zaahid
04:48
created

MimePartChildrenTrait::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 13
ccs 0
cts 9
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the ZBateson\MailMimeParser project.
4
 *
5
 * @license http://opensource.org/licenses/bsd-license.php BSD
6
 */
7
namespace ZBateson\MailMimeParser\Message\Part;
8
9
use Psr\Http\Message\StreamInterface;
10
use ZBateson\MailMimeParser\Message\PartFilterFactory;
11
use ZBateson\MailMimeParser\Message\PartFilter;
12
13
/**
14
 * MimePart methods related to child parts.
15
 *
16
 * @author Zaahid Bateson
17
 */
18
trait MimePartChildrenTrait
19
{
20
    /**
21
     * @var \ZBateson\MailMimeParser\Message\PartFilterFactory factory object
22
     *      responsible for create PartFilters
23
     */
24
    protected $partFilterFactory;
25
26
    /**
27
     * @var \ZBateson\MailMimeParser\Message\Part\MessagePart[] array of child
28
     *      parts
29
     */
30
    protected $children = [];
31
32
    /**
33
     * @param PartFilterFactory $partFilterFactory
34
     * @param PartBuilder $partBuilder
35
     * @param StreamInterface $stream
36
     */
37
    public function __construct(
38
        PartFilterFactory $partFilterFactory,
39
        PartBuilder $partBuilder,
40
        StreamInterface $stream
41
    ) {
42
        $this->partFilterFactory = $partFilterFactory;
43
        $pbChildren = $partBuilder->getChildren();
44
        if (!empty($pbChildren)) {
45
            $this->children = array_map(function ($child) use ($stream) {
46
                $childPart = $child->createMessagePart($stream);
47
                $childPart->parent = $this;
48
                return $childPart;
49
            }, $pbChildren);
50
        }
51
    }
52
53
    /**
54
     * Returns all parts, including the current object, and all children below
55
     * it (including children of children, etc...)
56
     *
57
     * @return MessagePart[]
58
     */
59
    protected function getAllNonFilteredParts()
60
    {
61
        $parts = [ $this ];
62
        foreach ($this->children as $part) {
63
            if ($part instanceof MimePart) {
64
                $parts = array_merge(
65
                    $parts,
66
                    $part->getAllNonFilteredParts()
67
                );
68
            } else {
69
                array_push($parts, $part);
70
            }
71
        }
72
        return $parts;
73
    }
74
75
    /**
76
     * Returns the part at the given 0-based index, or null if none is set.
77
     *
78
     * Note that the first part returned is the current part itself.  This is
79
     * often desirable for queries with a PartFilter, e.g. looking for a
80
     * MessagePart with a specific Content-Type that may be satisfied by the
81
     * current part.
82
     *
83
     * @param int $index
84
     * @param PartFilter $filter
85
     * @return MessagePart
86
     */
87
    public function getPart($index, PartFilter $filter = null)
88
    {
89
        $parts = $this->getAllParts($filter);
90
        if (!isset($parts[$index])) {
91
            return null;
92
        }
93
        return $parts[$index];
94
    }
95
96
    /**
97
     * Returns the current part, all child parts, and child parts of all
98
     * children optionally filtering them with the provided PartFilter.
99
     *
100
     * The first part returned is always the current MimePart.  This is often
101
     * desirable as it may be a valid MimePart for the provided PartFilter.
102
     *
103
     * @param PartFilter $filter an optional filter
104
     * @return MessagePart[]
105
     */
106
    public function getAllParts(PartFilter $filter = null)
107
    {
108
        $parts = $this->getAllNonFilteredParts();
109
        if (!empty($filter)) {
110
            return array_values(array_filter(
111
                $parts,
112
                [ $filter, 'filter' ]
113
            ));
114
        }
115
        return $parts;
116
    }
117
118
    /**
119
     * Returns the total number of parts in this and all children.
120
     *
121
     * Note that the current part is considered, so the minimum getPartCount is
122
     * 1 without a filter.
123
     *
124
     * @param PartFilter $filter
125
     * @return int
126
     */
127
    public function getPartCount(PartFilter $filter = null)
128
    {
129
        return count($this->getAllParts($filter));
130
    }
131
132
    /**
133
     * Returns the direct child at the given 0-based index, or null if none is
134
     * set.
135
     *
136
     * @param int $index
137
     * @param PartFilter $filter
138
     * @return MessagePart
139
     */
140
    public function getChild($index, PartFilter $filter = null)
141
    {
142
        $parts = $this->getChildParts($filter);
143
        if (!isset($parts[$index])) {
144
            return null;
145
        }
146
        return $parts[$index];
147
    }
148
149
    /**
150
     * Returns all direct child parts.
151
     *
152
     * If a PartFilter is provided, the PartFilter is applied before returning.
153
     *
154
     * @param PartFilter $filter
155
     * @return MessagePart[]
156
     */
157
    public function getChildParts(PartFilter $filter = null)
158
    {
159
        if ($filter !== null) {
160
            return array_values(array_filter($this->children, [ $filter, 'filter' ]));
161
        }
162
        return $this->children;
163
    }
164
165
    /**
166
     * Returns the number of direct children under this part.
167
     *
168
     * @param PartFilter $filter
169
     * @return int
170
     */
171
    public function getChildCount(PartFilter $filter = null)
172
    {
173
        return count($this->getChildParts($filter));
174
    }
175
176
    /**
177
     * Returns the part associated with the passed mime type if it exists.
178
     *
179
     * @param string $mimeType
180
     * @return MessagePart or null
181
     */
182
    public function getPartByMimeType($mimeType, $index = 0)
183
    {
184
        $partFilter = $this->partFilterFactory->newFilterFromContentType($mimeType);
185
        return $this->getPart($index, $partFilter);
186
    }
187
188
    /**
189
     * Returns an array of all parts associated with the passed mime type if any
190
     * exist or null otherwise.
191
     *
192
     * @param string $mimeType
193
     * @return MessagePart[] or null
194
     */
195
    public function getAllPartsByMimeType($mimeType)
196
    {
197
        $partFilter = $this->partFilterFactory->newFilterFromContentType($mimeType);
198
        return $this->getAllParts($partFilter);
199
    }
200
201
    /**
202
     * Returns the number of parts matching the passed $mimeType
203
     *
204
     * @param string $mimeType
205
     * @return int
206
     */
207
    public function getCountOfPartsByMimeType($mimeType)
208
    {
209
        $partFilter = $this->partFilterFactory->newFilterFromContentType($mimeType);
210
        return $this->getPartCount($partFilter);
211
    }
212
}
213