PostHelper   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isAssociativeArray() 0 3 1
A isEncodedFileWithData() 0 4 2
A toArray() 0 8 4
A isEncodedFile() 0 4 1
A toDecodedFileData() 0 12 3
A toArrayOfFiles() 0 23 3
1
<?php
2
/**
3
 * AssetPond plugin for Craft CMS 3.x
4
 *
5
 * Instant FilePond server that works with Craft Assets.
6
 *
7
 * @link      https://workingconcept.com
8
 * @copyright Copyright (c) 2019 Working Concept
9
 */
10
11
namespace workingconcept\assetpond\helpers;
12
13
/**
14
 * @author    Working Concept
15
 * @package   AssetPond
16
 * @since     1.0.0
17
 */
18
class PostHelper
19
{
20
    // Public Methods
21
    // =========================================================================
22
23
    public static function isEncodedFile($value): bool
24
    {
25
        $data = @json_decode($value, false);
26
        return is_object($data);
27
    }
28
29
    public static function isEncodedFileWithData($value): bool
30
    {
31
        $data = @json_decode($value, false);
32
        return is_object($data) && ! empty($data->data);
33
    }
34
35
    /**
36
     * Returns decoded file data from base64-encoded FilePond file object.
37
     *
38
     * @param $file
39
     * @return bool|object
40
     */
41
    public static function toDecodedFileData($file)
42
    {
43
        // suppress error messages assuming file objects are valid
44
        $file = @json_decode($file, false);
45
46
        // skip files that failed to decode or don't have data
47
        if ( ! is_object($file) || empty($file->data))
48
        {
49
            return false;
50
        }
51
52
        return $file;
53
    }
54
55
    public static function toArrayOfFiles($value): array
56
    {
57
        if (is_array($value['tmp_name']))
58
        {
59
            $results = [];
60
61
            foreach($value['tmp_name'] as $index => $tmpName)
62
            {
63
                $file = [
64
                    'tmp_name' => $value['tmp_name'][$index],
65
                    'name' => $value['name'][$index],
66
                    'size' => $value['size'][$index],
67
                    'error' => $value['error'][$index],
68
                    'type' => $value['type'][$index]
69
                ];
70
71
                $results[] = $file;
72
            }
73
74
            return $results;
75
        }
76
77
        return self::toArray($value);
78
    }
79
80
    public static function isAssociativeArray($arr): bool
81
    {
82
        return array_keys($arr) !== range(0, count($arr) - 1);
83
    }
84
85
    public static function toArray($value): array
86
    {
87
        if (is_array($value) && ! self::isAssociativeArray($value))
88
        {
89
            return $value;
90
        }
91
92
        return isset($value) ? [$value] : [];
93
    }
94
95
}
96