Completed
Push — master ( e8b3d2...889b57 )
by Thomas
14:29
created

ActivityObjectSerializerTrait::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 2
1
<?php
2
namespace keeko\core\serializer\base;
3
4
use keeko\core\model\Activity;
5
use keeko\core\serializer\TypeInferencer;
6
use keeko\framework\utils\HydrateUtils;
7
use Tobscure\JsonApi\Collection;
8
use Tobscure\JsonApi\Relationship;
9
10
/**
11
 */
12
trait ActivityObjectSerializerTrait {
13
14
	/**
15
	 */
16
	private $methodNames = [
17
		'activities' => 'Activity'
18
	];
19
20
	/**
21
	 */
22
	private $methodPluralNames = [
23
		'activities' => 'Activities'
24
	];
25
26
	/**
27
	 * @param mixed $model
28
	 * @return Relationship
29
	 */
30
	public function activities($model) {
31
		$method = 'get' . $this->getCollectionMethodPluralName('activities');
32
		$relationship = new Relationship(new Collection($model->$method(), Activity::getSerializer()));
33
		return $this->addRelationshipSelfLink($relationship, $model, 'activity');
34
	}
35
36
	/**
37
	 * @param mixed $model
38
	 * @param array $fields
39
	 */
40
	public function getAttributes($model, array $fields = null) {
41
		return [
42
			'class-name' => $model->getClassName(),
43
			'type' => $model->getType(),
44
			'display-name' => $model->getDisplayName(),
45
			'url' => $model->getUrl(),
46
			'reference-id' => $model->getReferenceId(),
47
			'version' => $model->getVersion(),
48
			'extra' => $model->getExtra()
49
		];
50
	}
51
52
	/**
53
	 */
54
	public function getFields() {
55
		return ['class-name', 'type', 'display-name', 'url', 'reference-id', 'version', 'extra'];
56
	}
57
58
	/**
59
	 * @param mixed $model
60
	 * @return string
61
	 */
62
	public function getId($model) {
63
		if ($model !== null) {
64
			return $model->getId();
65
		}
66
67
		return null;
68
	}
69
70
	/**
71
	 */
72
	public function getRelationships() {
73
		return [
74
			'activities' => Activity::getSerializer()->getType(null)
75
		];
76
	}
77
78
	/**
79
	 */
80
	public function getSortFields() {
81
		return ['class-name', 'type', 'display-name', 'url', 'reference-id', 'version', 'extra'];
82
	}
83
84
	/**
85
	 * @param mixed $model
86
	 * @return string
87
	 */
88
	public function getType($model) {
89
		return 'core/activity-object';
90
	}
91
92
	/**
93
	 * @param mixed $model
94
	 * @param mixed $data
95
	 * @return mixed The model
96
	 */
97
	public function hydrate($model, $data) {
98
		// attributes
99
		$attribs = isset($data['attributes']) ? $data['attributes'] : [];
100
101
		$model = HydrateUtils::hydrate($attribs, $model, ['id', 'class-name', 'type', 'display-name', 'url', 'reference-id', 'version', 'extra']);
102
103
		// relationships
104
		//$this->hydrateRelationships($model, $data);
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
105
106
		return $model;
107
	}
108
109
	/**
110
	 * @param Relationship $relationship
111
	 * @param mixed $model
112
	 * @param string $related
113
	 * @return Relationship
114
	 */
115
	abstract protected function addRelationshipSelfLink(Relationship $relationship, $model, $related);
116
117
	/**
118
	 * @param mixed $relatedName
119
	 */
120
	protected function getCollectionMethodName($relatedName) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
121
		if (isset($this->methodNames[$relatedName])) {
122
			return $this->methodNames[$relatedName];
123
		}
124
		return null;
125
	}
126
127
	/**
128
	 * @param mixed $relatedName
129
	 */
130
	protected function getCollectionMethodPluralName($relatedName) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
131
		if (isset($this->methodPluralNames[$relatedName])) {
132
			return $this->methodPluralNames[$relatedName];
133
		}
134
		return null;
135
	}
136
137
	/**
138
	 */
139
	protected function getTypeInferencer() {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
140
		return TypeInferencer::getInstance();
141
	}
142
}
143