MetaDocument::fromArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
namespace alsvanzelf\jsonapi;
4
5
use alsvanzelf\jsonapi\Document;
6
use alsvanzelf\jsonapi\helpers\Converter;
7
use alsvanzelf\jsonapi\objects\MetaObject;
8
9
/**
10
 * this document can be used if neither data nor errors need to be set
11
 * meta can also be added to ResourceDocument, CollectionDocument and ErrorsDocument via `addMeta()`
12
 */
13
class MetaDocument extends Document {
14
	/**
15
	 * human api
16
	 */
17
	
18
	/**
19
	 * @param  array $meta
20
	 * @return MetaDocument
21
	 */
22 2
	public static function fromArray(array $meta) {
23 2
		$metaDocument = new self();
24 2
		$metaDocument->setMetaObject(MetaObject::fromArray($meta));
25
		
26 2
		return $metaDocument;
27
	}
28
	
29
	/**
30
	 * @param  object $meta
31
	 * @return MetaDocument
32
	 */
33 1
	public static function fromObject($meta) {
34 1
		$array = Converter::objectToArray($meta);
35
		
36 1
		return self::fromArray($array);
37
	}
38
	
39
	/**
40
	 * wrapper for Document::addMeta() to the primary data of this document available via `add()`
41
	 * 
42
	 * @param string $key
43
	 * @param mixed  $value
44
	 * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT
45
	 */
46 1
	public function add($key, $value, $level=Document::LEVEL_ROOT) {
47 1
		parent::addMeta($key, $value, $level);
48
	}
49
	
50
	/**
51
	 * spec api
52
	 */
53
	
54
	/**
55
	 * DocumentInterface
56
	 */
57
	
58
	/**
59
	 * @inheritDoc
60
	 */
61 7
	public function toArray() {
62 7
		$array = parent::toArray();
63
		
64
		// force meta to be set, and be an object when converting to json
65 7
		if (isset($array['meta']) === false) {
66 2
			$array['meta'] = new \stdClass();
67
		}
68
		
69 7
		return $array;
70
	}
71
}
72