|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace alsvanzelf\jsonapi\helpers; |
|
4
|
|
|
|
|
5
|
|
|
use alsvanzelf\jsonapi\interfaces\ExtensionInterface; |
|
6
|
|
|
use alsvanzelf\jsonapi\interfaces\ObjectInterface; |
|
7
|
|
|
use alsvanzelf\jsonapi\interfaces\ProfileInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @internal |
|
11
|
|
|
*/ |
|
12
|
|
|
class Converter { |
|
13
|
|
|
/** |
|
14
|
|
|
* @param object $object |
|
15
|
|
|
* @return array |
|
16
|
|
|
*/ |
|
17
|
17 |
|
public static function objectToArray($object) { |
|
18
|
17 |
|
if ($object instanceof ObjectInterface) { |
|
19
|
2 |
|
return $object->toArray(); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
15 |
|
return get_object_vars($object); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @see https://stackoverflow.com/questions/7593969/regex-to-split-camelcase-or-titlecase-advanced/7599674#7599674 |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $camelCase |
|
29
|
|
|
* @return string |
|
30
|
|
|
*/ |
|
31
|
16 |
|
public static function camelCaseToWords($camelCase) { |
|
32
|
16 |
|
$parts = preg_split('/(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/', $camelCase); |
|
33
|
|
|
|
|
34
|
16 |
|
return implode(' ', $parts); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* generates the value for a content type header, with extensions and profiles merged in if available |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $contentType |
|
41
|
|
|
* @param ExtensionInterface[] $extensions |
|
42
|
|
|
* @param ProfileInterface[] $profiles |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
14 |
|
public static function prepareContentType($contentType, array $extensions, array $profiles) { |
|
46
|
14 |
|
if ($extensions !== []) { |
|
47
|
5 |
|
$extensionLinks = []; |
|
48
|
5 |
|
foreach ($extensions as $extension) { |
|
49
|
5 |
|
$extensionLinks[] = $extension->getOfficialLink(); |
|
50
|
|
|
} |
|
51
|
5 |
|
$extensionLinks = implode(' ', $extensionLinks); |
|
52
|
|
|
|
|
53
|
5 |
|
$contentType .= '; ext="'.$extensionLinks.'"'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
14 |
|
if ($profiles !== []) { |
|
57
|
5 |
|
$profileLinks = []; |
|
58
|
5 |
|
foreach ($profiles as $profile) { |
|
59
|
5 |
|
$profileLinks[] = $profile->getOfficialLink(); |
|
60
|
|
|
} |
|
61
|
5 |
|
$profileLinks = implode(' ', $profileLinks); |
|
62
|
|
|
|
|
63
|
5 |
|
$contentType .= '; profile="'.$profileLinks.'"'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
14 |
|
return $contentType; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @deprecated {@see prepareContentType()} |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public static function mergeProfilesInContentType($contentType, array $profiles) { |
|
73
|
1 |
|
return self::prepareContentType($contentType, $extensions=[], $profiles); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|