PropertyHelper::isAttributeProperty()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 3
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 7/25/15
6
 * Time: 4:56 PM.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace NilPortugues\Api\JsonApi\Helpers;
12
13
use NilPortugues\Api\JsonApi\JsonApiTransformer;
14
use NilPortugues\Api\Transformer\Helpers\RecursiveFormatterHelper;
15
use NilPortugues\Serializer\Serializer;
16
17
/**
18
 * Class PropertyHelper.
19
 */
20
class PropertyHelper
21
{
22
    /**
23
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
24
     * @param array                               $value
25
     *
26
     * @return array
27
     */
28
    public static function setResponseDataTypeAndId(array &$mappings, array &$value)
29
    {
30
        $type = $value[Serializer::CLASS_IDENTIFIER_KEY];
31
32
        if (empty($mappings[$type])) {
33
            return [];
34
        }
35
36
        if (!is_scalar($type)) {
37
            return self::setResponseDataTypeAndId($mappings, $type);
38
        }
39
40
        $finalType = ($mappings[$type]->getClassAlias()) ? $mappings[$type]->getClassAlias() : $type;
41
42
        $ids = [];
43
        foreach (\array_keys($value) as $propertyName) {
44
            if (\in_array($propertyName, RecursiveFormatterHelper::getIdProperties($mappings, $type), true)) {
45
                $id = RecursiveFormatterHelper::getIdValue($value[$propertyName]);
46
                $ids[] = (\is_array($id)) ? \implode(JsonApiTransformer::ID_SEPARATOR, $id) : $id;
47
            }
48
        }
49
50
        return [
51
            JsonApiTransformer::TYPE_KEY => RecursiveFormatterHelper::namespaceAsArrayKey($finalType),
52
            JsonApiTransformer::ID_KEY => \implode(JsonApiTransformer::ID_SEPARATOR, $ids),
53
        ];
54
    }
55
56
    /**
57
     * @param \NilPortugues\Api\Mapping\Mapping[] $mappings
58
     * @param string                              $propertyName
59
     * @param string                              $type
60
     *
61
     * @return bool
62
     */
63
    public static function isAttributeProperty(array &$mappings, $propertyName, $type)
64
    {
65
        return Serializer::CLASS_IDENTIFIER_KEY !== $propertyName
66
        && !in_array($propertyName, RecursiveFormatterHelper::getIdProperties($mappings, $type));
67
    }
68
}
69