Skip to content

Commit ce16821

Browse files
authored
Merge pull request nilportugues#75 from nilportugues/getRequiredFields
Mapping now has required properties to be used in the server.
2 parents f886952 + 6ba74de commit ce16821

File tree

6 files changed

+76
-4
lines changed

6 files changed

+76
-4
lines changed

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ class PostMapping implements JsonApiMapping
122122
]
123123
];
124124
}
125+
126+
/**
127+
* {@inheritdoc}
128+
*/
129+
public function getRequiredProperties()
130+
{
131+
return ['author', 'title', 'body'];
132+
}
125133
}
126134
```
127135

@@ -188,6 +196,14 @@ class PostIdMapping implements JsonApiMapping
188196
],
189197
];
190198
}
199+
200+
/**
201+
* {@inheritdoc}
202+
*/
203+
public function getRequiredProperties()
204+
{
205+
return [];
206+
}
191207
}
192208
```
193209

@@ -243,7 +259,15 @@ class UserMapping implements JsonApiMapping
243259
'friends' => 'http://example.com/users/{userId}/friends',
244260
'comments' => 'http://example.com/users/{userId}/comments',
245261
];
246-
}
262+
}
263+
264+
/**
265+
* {@inheritdoc}
266+
*/
267+
public function getRequiredProperties()
268+
{
269+
return [];
270+
}
247271
}
248272
```
249273

@@ -285,7 +309,7 @@ class UserIdMapping implements JsonApiMapping
285309
* {@inheritdoc}
286310
*/
287311
public function getIdProperties()
288-
return [ 'userId',];
312+
return ['userId'];
289313
}
290314
/**
291315
* {@inheritdoc}
@@ -298,6 +322,14 @@ class UserIdMapping implements JsonApiMapping
298322
'comments' => 'http://example.com/users/{userId}/comments',
299323
];
300324
}
325+
326+
/**
327+
* {@inheritdoc}
328+
*/
329+
public function getRequiredProperties()
330+
{
331+
return [];
332+
}
301333
}
302334
```
303335

@@ -362,6 +394,14 @@ class CommendMapping implements JsonApiMapping
362394
]
363395
];
364396
}
397+
398+
/**
399+
* {@inheritdoc}
400+
*/
401+
public function getRequiredProperties()
402+
{
403+
return [];
404+
}
365405
}
366406
```
367407

@@ -425,6 +465,14 @@ class CommentId implements JsonApiMapping
425465
]
426466
];
427467
}
468+
469+
/**
470+
* {@inheritdoc}
471+
*/
472+
public function getRequiredProperties()
473+
{
474+
return [];
475+
}
428476
}
429477
```
430478

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
],
1515
"require": {
1616
"php": ">=5.5.0",
17-
"nilportugues/api-transformer": "^1.6",
17+
"nilportugues/api-transformer": "^3.0.0",
1818
"symfony/psr-http-message-bridge" : "0.2"
1919
},
2020
"require-dev": {

src/Server/Data/DataObject.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ protected static function missingCreationAttributes(array $data, JsonApiSerializ
9090

9191
$diff = [];
9292
if (null !== $mapping) {
93+
$required = $mapping->getRequiredProperties();
9394
$properties = str_replace(
9495
array_keys($mapping->getAliasedProperties()),
9596
array_values($mapping->getAliasedProperties()),
96-
$mapping->getProperties()
97+
!empty($required) ? $required : $mapping->getProperties()
9798
);
9899
$properties = array_diff($properties, $mapping->getIdProperties());
99100

tests/Integrations/Doctrine/Mappings/CommentMapping.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,11 @@ public function getRelationships()
6161
{
6262
return [];
6363
}
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function getRequiredProperties()
68+
{
69+
return [];
70+
}
6471
}

tests/Integrations/Doctrine/Mappings/CustomerMapping.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,12 @@ public function getRelationships()
6161
{
6262
return [];
6363
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function getRequiredProperties()
69+
{
70+
return [];
71+
}
6472
}

tests/Integrations/Doctrine/Mappings/PostMapping.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,12 @@ public function getRelationships()
6161
{
6262
return [];
6363
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function getRequiredProperties()
69+
{
70+
return [];
71+
}
6472
}

0 commit comments

Comments
 (0)