55use alsvanzelf \jsonapi \exceptions \Exception ;
66use alsvanzelf \jsonapi \exceptions \InputException ;
77use alsvanzelf \jsonapi \helpers \AtMemberManager ;
8+ use alsvanzelf \jsonapi \helpers \Converter ;
89use alsvanzelf \jsonapi \helpers \HttpStatusCodeManager ;
910use alsvanzelf \jsonapi \helpers \LinksManager ;
1011use alsvanzelf \jsonapi \helpers \Validator ;
1112use alsvanzelf \jsonapi \interfaces \DocumentInterface ;
13+ use alsvanzelf \jsonapi \interfaces \ProfileInterface ;
1214use alsvanzelf \jsonapi \objects \JsonapiObject ;
15+ use alsvanzelf \jsonapi \objects \LinkObject ;
1316use alsvanzelf \jsonapi \objects \LinksObject ;
1417use alsvanzelf \jsonapi \objects \MetaObject ;
18+ use alsvanzelf \jsonapi \objects \ProfileLinkObject ;
1519
1620/**
1721 * @see ResourceDocument, CollectionDocument, ErrorsDocument or MetaDocument
@@ -35,6 +39,8 @@ abstract class Document implements DocumentInterface, \JsonSerializable {
3539 protected $ meta ;
3640 /** @var JsonapiObject */
3741 protected $ jsonapi ;
42+ /** @var ProfileInterface[] */
43+ protected $ profiles = [];
3844 /** @var array */
3945 protected static $ defaults = [
4046 /**
@@ -172,6 +178,33 @@ public function unsetJsonapiObject() {
172178 $ this ->jsonapi = null ;
173179 }
174180
181+ /**
182+ * apply a profile which adds the link and sets a correct content-type
183+ *
184+ * note that the rules from the profile are not automatically enforced
185+ * applying the rules, and applying them correctly, is manual
186+ * however the $profile could have custom methods to help
187+ *
188+ * @see https://jsonapi.org/format/1.1/#profiles
189+ *
190+ * @param ProfileInterface $profile
191+ */
192+ public function applyProfile (ProfileInterface $ profile ) {
193+ $ this ->profiles [] = $ profile ;
194+
195+ if ($ this ->links === null ) {
196+ $ this ->setLinksObject (new LinksObject ());
197+ }
198+
199+ $ link = $ profile ->getAliasedLink ();
200+ if ($ link instanceof LinkObject) {
201+ $ this ->links ->appendLinkObject ('profile ' , $ link );
202+ }
203+ else {
204+ $ this ->links ->append ('profile ' , $ link );
205+ }
206+ }
207+
175208 /**
176209 * DocumentInterface
177210 */
@@ -233,7 +266,9 @@ public function sendResponse(array $options=[]) {
233266 $ json = ($ options ['json ' ] !== null ) ? $ options ['json ' ] : $ this ->toJson ($ options );
234267
235268 http_response_code ($ this ->httpStatusCode );
236- header ('Content-Type: ' .$ options ['contentType ' ]);
269+
270+ $ contentType = Converter::mergeProfilesInContentType ($ options ['contentType ' ], $ this ->profiles );
271+ header ('Content-Type: ' .$ contentType );
237272
238273 echo $ json ;
239274 }
0 commit comments