Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
50.00% covered (warning)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Part
93.75% covered (success)
93.75%
15 / 16
50.00% covered (warning)
50.00%
1 / 2
7.01
0.00% covered (danger)
0.00%
0 / 1
 parse
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 parseParts
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
5.02
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Mail\Message;
15
16use Pop\Utils;
17
18/**
19 * Message part object class
20 *
21 * @category   Pop
22 * @package    Pop\Mail
23 * @author     Nick Sagona, III <dev@nolainteractive.com>
24 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    3.6.0
27 */
28class Part extends Utils\ArrayObject
29{
30
31    /**
32     * Parse message parts from string
33     *
34     * @param  mixed $body
35     * @param  string $boundary
36     * @return array
37     */
38    public static function parse($body, $boundary = null)
39    {
40        $partStrings = \Pop\Mime\Message::parseBody($body, $boundary);
41        $parts       = [];
42
43        foreach ($partStrings as $partString) {
44            $parts[] = \Pop\Mime\Message::parsePart($partString);
45        }
46
47        return self::parseParts($parts);
48    }
49
50    /**
51     * Parse message parts from array of parts
52     *
53     * @param  array $parts
54     * @return array
55     */
56    public static function parseParts(array $parts)
57    {
58        $flattenedParts = [];
59
60        foreach ($parts as $part) {
61            if (is_array($part)) {
62                $flattenedParts = array_merge($flattenedParts, self::parseParts($part));
63            } else {
64                $flattenedParts[] = new static([
65                    'headers'    => $part->getHeadersAsArray(),
66                    'type'       => ($part->hasHeader('Content-Type')) ? $part->getHeader('Content-Type')->getValue() : null,
67                    'attachment' => (($part->hasBody()) && ($part->getBody()->isFile())),
68                    'basename'   => $part->getFilename(),
69                    'content'    => $part->getContents()
70                ]);
71            }
72        }
73
74        return $flattenedParts;
75    }
76
77}