1010use alsvanzelf \jsonapi \interfaces \ProfileInterface ;
1111use alsvanzelf \jsonapi \interfaces \ResourceInterface ;
1212use alsvanzelf \jsonapi \objects \ResourceIdentifierObject ;
13- use alsvanzelf \jsonapi \objects \ResourceObject ;
1413
1514ini_set ('display_errors ' , 1 );
1615error_reporting (-1 );
1716
1817require_once __DIR__ .'/../vendor/autoload.php ' ;
1918
2019class ExampleDataset {
21- private static $ records = [
20+ /** @var array<string, array<int, array<string, string|int>>> */
21+ private static array $ records = [
2222 'articles ' => [
2323 1 => [
2424 'title ' => 'JSON:API paints my bikeshed! ' ,
@@ -58,34 +58,43 @@ class ExampleDataset {
5858 ],
5959 ];
6060
61- public static function getRecord ($ type , $ id ) {
62- if (!isset (self ::$ records [$ type ][$ id ])) {
61+ /**
62+ * @return array<string, string|int>
63+ */
64+ public static function getRecord (string $ type , int $ id ): array {
65+ if (isset (self ::$ records [$ type ][$ id ]) === false ) {
6366 throw new \Exception ('sorry, we have a limited dataset ' );
6467 }
6568
6669 return self ::$ records [$ type ][$ id ];
6770 }
6871
69- public static function getEntity ($ type , $ id ) {
72+ public static function getEntity (string $ type , int $ id ): ExampleUser {
7073 $ record = self ::getRecord ($ type , $ id );
7174
7275 $ user = new ExampleUser ($ id );
7376 foreach ($ record as $ key => $ value ) {
74- $ user ->$ key = $ value ;
77+ $ user ->$ key = $ value ; // @phpstan-ignore property.dynamicName
7578 }
7679
7780 return $ user ;
7881 }
7982
80- public static function findRecords ($ type ) {
83+ /**
84+ * @return array<array<string, string|int>>
85+ */
86+ public static function findRecords (string $ type ): array {
8187 return self ::$ records [$ type ];
8288 }
8389
84- public static function findEntities ($ type ) {
85- $ records = self ::findRecords ($ type );
86- $ entities = [];
90+ /**
91+ * @return ExampleUser[]
92+ */
93+ public static function findEntities (string $ type ): array {
94+ $ recordIds = array_keys (self ::findRecords ($ type ));
95+ $ entities = [];
8796
88- foreach ($ records as $ id => $ record ) {
97+ foreach ($ recordIds as $ id ) {
8998 $ entities [$ id ] = self ::getEntity ($ type , $ id );
9099 }
91100
@@ -94,15 +103,15 @@ public static function findEntities($type) {
94103}
95104
96105class ExampleUser {
97- public $ name ;
98- public $ heads ;
99- public $ unknown ;
106+ public ? string $ name = null ;
107+ public null | int | string $ heads = null ;
108+ public mixed $ unknown = null ;
100109
101110 public function __construct (
102- public $ id ,
111+ public int $ id ,
103112 ) {}
104113
105- function getCurrentLocation () {
114+ public function getCurrentLocation (): string {
106115 return 'Earth ' ;
107116 }
108117}
@@ -124,11 +133,7 @@ public function getNamespace(): string {
124133 * optionally helpers for the specific extension
125134 */
126135
127- public function setVersion (ResourceInterface $ resource , $ version ) {
128- if ($ resource instanceof HasExtensionMembersInterface === false ) {
129- throw new \Exception ('resource doesn \'t have extension members ' );
130- }
131-
136+ public function setVersion (ResourceInterface & HasExtensionMembersInterface $ resource , string $ version ): void {
132137 if ($ resource instanceof ResourceDocument) {
133138 $ resource ->getResource ()->addExtensionMember ($ this , 'id ' , $ version );
134139 }
@@ -151,14 +156,11 @@ public function getOfficialLink(): string {
151156 * optionally helpers for the specific profile
152157 */
153158
154- /**
155- * @param ResourceInterface&HasAttributesInterface $resource
156- */
157- public function setTimestamps (ResourceInterface $ resource , ?\DateTimeInterface $ created =null , ?\DateTimeInterface $ updated =null ) {
158- if ($ resource instanceof HasAttributesInterface === false ) {
159- throw new \Exception ('cannot add attributes to identifier objects ' );
160- }
161-
159+ public function setTimestamps (
160+ ResourceInterface & HasAttributesInterface $ resource ,
161+ ?\DateTimeInterface $ created =null ,
162+ ?\DateTimeInterface $ updated =null ,
163+ ): void {
162164 $ timestamps = [];
163165 if ($ created !== null ) {
164166 $ timestamps ['created ' ] = $ created ->format (\DateTime::ISO8601 );
0 commit comments