1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Contains Drupal\esdportal_api\Serializers\EcSerializer. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Drupal\esdportal_api\Serializers; |
9
|
|
|
|
10
|
|
|
use Tobscure\JsonApi\SerializerAbstract; |
11
|
|
|
use Tobscure\JsonApi\Link; |
12
|
|
|
use Drupal\esdportal_api\Serializers\EcProfileSerializer; |
13
|
|
|
use Drupal\esdportal_api\Serializers\EcStateRatingSerializer; |
14
|
|
|
use Drupal\esdportal_api\Serializers\EsdEl2014Serializer; |
15
|
|
|
use Drupal\esdportal_api\Serializers\EsdEl2015Serializer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Serializes early childhood taxonomy terms. |
19
|
|
|
*/ |
20
|
|
|
class EcSerializer extends SerializerAbstract { |
21
|
|
|
protected $type = 'ecs'; |
22
|
|
|
protected $link = ['ec_profile', 'most_recent_ec_state_rating']; |
23
|
|
|
protected $include = NULL; |
24
|
|
|
|
25
|
|
|
protected static $potentialDataTables; |
26
|
|
|
protected static $potentialDataTableNames; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Removes linked info. |
30
|
|
|
*/ |
31
|
|
View Code Duplication |
protected function attributes($ec_term) { |
32
|
|
|
self::$potentialDataTables = \Drupal\esdportal_api\EcDataUtils::getDataTablesWithProgramIds(); |
33
|
|
|
self::$potentialDataTableNames = \Drupal\esdportal_api\EcDataUtils::extractDataTableNames(self::$potentialDataTables); |
34
|
|
|
|
35
|
|
|
// These turn into linkages: |
36
|
|
|
unset($ec_term->ec_profile); |
37
|
|
|
unset($ec_term->ec_profile_id); |
38
|
|
|
unset($ec_term->most_recent_ec_state_rating); |
39
|
|
|
unset($ec_term->most_recent_ec_state_rating_id); |
40
|
|
|
|
41
|
|
|
foreach (self::$potentialDataTableNames as $name) { |
42
|
|
|
unset($ec_term->{$name}); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $ec_term; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns the id. |
50
|
|
|
*/ |
51
|
|
|
protected function id($ec_term) { |
52
|
|
|
return entity_extract_ids('taxonomy_term', $ec_term)[0]; |
53
|
|
|
} |
54
|
|
|
/** |
55
|
|
|
* Same thing... backwards compatible with bnchdrff/json-api just in case. |
56
|
|
|
*/ |
57
|
|
|
protected function getId($ec_term) { |
58
|
|
|
return entity_extract_ids('taxonomy_term', $ec_term)[0]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Handles inclusion of ec_profiles. |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
protected function ec_profile() { |
65
|
|
|
return function ($ec, $include, $included) { |
66
|
|
|
$serializer = new EcProfileSerializer($included); |
67
|
|
|
|
68
|
|
|
if (!$ec->ec_profile_id) { |
69
|
|
|
return NULL; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$ec_profile = $serializer->resource($include ? $ec->ec_profile : $ec->ec_profile_id); |
73
|
|
|
|
74
|
|
|
$link = new Link($ec_profile); |
75
|
|
|
|
76
|
|
|
return $link; |
77
|
|
|
}; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Handles inclusion of most_recent_ec_state_ratings. |
82
|
|
|
*/ |
83
|
|
|
protected function most_recent_ec_state_rating() { |
84
|
|
|
return function ($ec, $include, $included) { |
85
|
|
|
$serializer = new EcStateRatingSerializer($included); |
86
|
|
|
|
87
|
|
|
if (!isset($ec->most_recent_ec_state_rating)) { |
88
|
|
|
return NULL; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$most_recent_ec_state_rating = $serializer->resource($include ? $ec->most_recent_ec_state_rating : $ec->most_recent_ec_state_rating_id); |
92
|
|
|
|
93
|
|
|
$link = new Link($most_recent_ec_state_rating); |
94
|
|
|
|
95
|
|
|
return $link; |
96
|
|
|
}; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Dynamically construct methods for data tables with bcodes. |
101
|
|
|
* |
102
|
|
|
* @param string $method |
103
|
|
|
* Should be a table_name, and will be converted to camelCase. |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function __call($method, $args) { |
106
|
|
|
self::$potentialDataTables = \Drupal\esdportal_api\EcDataUtils::getDataTablesWithProgramIds(); |
107
|
|
|
self::$potentialDataTableNames = \Drupal\esdportal_api\EcDataUtils::extractDataTableNames(self::$potentialDataTables); |
108
|
|
|
|
109
|
|
|
return function ($ec, $include, $included) use ($method) { |
110
|
|
|
// The actual called method is underscore-separated... |
111
|
|
|
$table_name = $method; |
112
|
|
|
|
113
|
|
|
// ... But we want a method that is camelCased. |
114
|
|
|
$camelized_method = \Drupal\esdportal_api\EcDataUtils::underscoreToCamel($table_name); |
115
|
|
|
|
116
|
|
|
$class_name = 'Drupal\\esdportal_api\\Serializers\\' . $camelized_method . 'Serializer'; |
117
|
|
|
|
118
|
|
|
// Legit data table name? |
119
|
|
|
if (!in_array($table_name, self::$potentialDataTableNames)) { |
120
|
|
|
return NULL; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$serializer = new $class_name($included); |
124
|
|
|
|
125
|
|
|
if (!$ec->program_id) { |
126
|
|
|
return NULL; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (!$ec->{$table_name}) { |
130
|
|
|
return NULL; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$datum = $serializer->resource($include ? $ec->{$table_name} : $ec->program_id); |
134
|
|
|
|
135
|
|
|
$link = new Link($datum); |
136
|
|
|
|
137
|
|
|
return $link; |
138
|
|
|
}; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|