Passed
Push — master ( 2cda12...3e0ae5 )
by Melech
02:04 queued 37s
created

ResponseStruct   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A getStructuredData() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Http\Struct\Response\Trait;
15
16
use Valkyrja\Type\BuiltIn\Enum\Trait\Arrayable;
17
18
use function array_key_exists;
19
20
/**
21
 * Trait ResponseStruct.
22
 *
23
 * @author Melech Mizrachi
24
 */
25
trait ResponseStruct
26
{
27
    use Arrayable;
28
29
    /**
30
     * @inheritDoc
31
     */
32
    public static function getStructuredData(array $data, bool $includeAll = true): array
33
    {
34
        $asArray    = self::asArray();
35
        $structured = [];
36
37
        foreach ($asArray as $key => $value) {
38
            if (! $includeAll && ! array_key_exists($key, $data)) {
39
                continue;
40
            }
41
42
            $structured[$value] = $data[$key] ?? null;
43
        }
44
45
        return $structured;
46
    }
47
}
48