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
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-2024 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-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
25 * @license    http://www.popphp.org/license     New BSD License
26 * @version    4.0.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(mixed $body, ?string $boundary = null): array
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): array
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')) && (count($part->getHeader('Content-Type')->getValues()) == 1)) ?
67                        $part->getHeader('Content-Type')->getValue(0) : null,
68                    'attachment' => (($part->hasBody()) && ($part->getBody()->isFile())),
69                    'basename'   => $part->getFilename(),
70                    'content'    => $part->getContents()
71                ]);
72            }
73        }
74
75        return $flattenedParts;
76    }
77
78}