-
Notifications
You must be signed in to change notification settings - Fork 35
/
PropertyHelper.php
80 lines (70 loc) · 2.45 KB
/
PropertyHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Author: Nil Portugués Calderó <contact@nilportugues.com>
* Date: 7/25/15
* Time: 4:56 PM.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace NilPortugues\Api\JsonApi\Helpers;
use NilPortugues\Api\JsonApi\JsonApiTransformer;
use NilPortugues\Api\Transformer\Helpers\RecursiveFormatterHelper;
use NilPortugues\Serializer\Serializer;
/**
* Class PropertyHelper.
*/
class PropertyHelper
{
/**
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings
* @param array $value
*
* @return array
*/
public static function setResponseDataTypeAndId(array &$mappings, array &$value)
{
if (empty($type = $value[Serializer::CLASS_IDENTIFIER_KEY]) || (empty($mappings[$type]))) {
return [];
}
if (!is_scalar($type)) {
return self::setResponseDataTypeAndId($mappings, $type);
}
$finalType = ($mappings[$type]->getClassAlias()) ? $mappings[$type]->getClassAlias() : $type;
$ids = self::getIdValues($mappings, $value, $type);
return [
JsonApiTransformer::TYPE_KEY => RecursiveFormatterHelper::namespaceAsArrayKey($finalType),
JsonApiTransformer::ID_KEY => \implode(JsonApiTransformer::ID_SEPARATOR, $ids),
];
}
/**
* @param array $mappings
* @param array $value
* @param $type
*
* @return array
*/
public static function getIdValues(array $mappings, array $value, $type)
{
$ids = [];
foreach (\array_keys($value) as $propertyName) {
if (\in_array($propertyName, RecursiveFormatterHelper::getIdProperties($mappings, $type), true)) {
$id = RecursiveFormatterHelper::getIdValue($value[$propertyName]);
$ids[] = (\is_array($id)) ? \implode(JsonApiTransformer::ID_SEPARATOR, $id) : $id;
}
}
return $ids;
}
/**
* @param \NilPortugues\Api\Mapping\Mapping[] $mappings
* @param string $propertyName
* @param string $type
*
* @return bool
*/
public static function isAttributeProperty(array &$mappings, $propertyName, $type)
{
return Serializer::CLASS_IDENTIFIER_KEY !== $propertyName
&& !in_array($propertyName, RecursiveFormatterHelper::getIdProperties($mappings, $type));
}
}