Completed
Branch BUG/batch-strings-overridden (c36a9a)
by
unknown
10:01 queued 01:18
created

EE_File_Normalization   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 40
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 10 3
A unnormalize() 0 10 3
1
<?php
2
3
use EventEspresso\core\services\request\files\FileSubmissionInterface;
4
5
/**
6
 * EE_File_Normalization
7
 * Takes the input from $_FILES and creates an FileSubmissionInterface object.
8
 *
9
 * @package               Event Espresso
10
 * @subpackage
11
 * @author                Mike Nelson
12
 */
13
class EE_File_Normalization extends EE_Normalization_Strategy_Base
14
{
15
16
    /**
17
     * Keep in mind $value_to_normalize should be a FileSubmissionInterface or null, so this shouldn't really do
18
     * much (other than NOT convert it to a string or something).
19
     * @param string $value_to_normalize
20
     * @return FileSubmissionInterface
21
     */
22
    public function normalize($value_to_normalize)
23
    {
24
        if ($value_to_normalize instanceof FileSubmissionInterface || is_null($value_to_normalize)) {
25
            return $value_to_normalize;
26
        } else {
27
            throw new EE_Validation_Error(
28
                esc_html__('The file input has an invalid format.', 'event_espresso')
29
            );
30
        }
31
    }
32
33
34
    /**
35
     * This may be called prematurely on submitted data, so we actually don't want to convert it into a string because
36
     * we'll lose all the FileSubmissionInterface data. So prefer to leave it alone. FileSubmissionInterface
37
     * can be cast to a string just fine so it's good as-is.
38
     *
39
     * @param string $normalized_value
40
     * @return string
41
     */
42
    public function unnormalize($normalized_value)
43
    {
44
        if ($normalized_value instanceof FileSubmissionInterface || is_null($normalized_value)) {
45
            // Leave it as the object, it can be treated like a string because it
46
            // overrides __toString()
47
            return $normalized_value;
48
        } else {
49
            return (string) $normalized_value;
50
        }
51
    }
52
}
53