Test Failed
Push — fix/media-validation ( 3351fe )
by Ben
09:34
created

AbstractMediaFieldRule::normalizePayload()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
rs 9.6111
cc 5
nc 4
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Fields\ValidationRules;
4
5
use Illuminate\Validation\Concerns\ValidatesAttributes;
6
use Thinktomorrow\Chief\Media\Application\MediaRequest;
7
8
abstract class AbstractMediaFieldRule
9
{
10
    use ValidatesAttributes,
0 ignored issues
show
introduced by
The trait Illuminate\Validation\Concerns\ValidatesAttributes requires some properties which are not provided by Thinktomorrow\Chief\Fiel...\AbstractMediaFieldRule: $data, $container, $currentRule, $implicitAttributes
Loading history...
introduced by
The trait Thinktomorrow\Chief\Fiel...ExistingAssetAttributes requires some properties which are not provided by Thinktomorrow\Chief\Fiel...\AbstractMediaFieldRule: $media, $size
Loading history...
11
        ValidatesExistingAssetAttributes;
12
13
    protected function normalizePayload($value): array
14
    {
15
        $payload = $this->emptyPayload();
16
17
        if(!$value || !is_array($value)) return $payload;
18
19
        foreach([MediaRequest::NEW, MediaRequest::REPLACE, MediaRequest::DETACH] as $action) {
20
            if(isset($value[$action])){
21
                $payload[$action] = $value[$action];
22
            }
23
        }
24
25
        return $payload;
26
    }
27
28
    private function emptyPayload(): array
29
    {
30
        return [
31
            MediaRequest::NEW => [],
32
            MediaRequest::REPLACE => [],
33
            MediaRequest::DETACH => [],
34
        ];
35
    }
36
}
37