Passed
Push — main ( f8d78a...6afd83 )
by Lode
01:12 queued 12s
created

Converter::prepareContentType()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 13
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 22
ccs 14
cts 14
cp 1
crap 5
rs 9.5222
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