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