@@ -49,14 +49,16 @@ public function hasAnyRelationships($name, $withIncluded = false)
4949 {
5050 $ type = JsonApi::getResourceType ($ name );
5151
52- PHPUnit::assertTrue (count (array_filter ($ this ->relationships , function ($ relation ) use ($ type ) {
53- return $ relation ['data ' ]['type ' ] === $ type ;
54- })) > 0 );
52+ PHPUnit::assertTrue (
53+ count ($ this ->filterResources ($ this ->relationships , $ type )) > 0 ,
54+ sprintf ('There is not any relationship with type "%s" ' , $ type )
55+ );
5556
5657 if ($ withIncluded ) {
57- PHPUnit::assertTrue (count (array_filter ($ this ->includeds , function ($ included ) use ($ type ) {
58- return $ included ['type ' ] === $ type ;
59- })) > 0 );
58+ PHPUnit::assertTrue (
59+ count ($ this ->filterResources ($ this ->includeds , $ type )) > 0 ,
60+ sprintf ('There is not any relationship with type "%s" ' , $ type )
61+ );
6062 }
6163
6264 return $ this ;
@@ -74,14 +76,16 @@ public function hasNotAnyRelationships($name, $withIncluded = false)
7476 {
7577 $ type = JsonApi::getResourceType ($ name );
7678
77- PHPUnit::assertFalse (count (array_filter ($ this ->relationships , function ($ relation ) use ($ type ) {
78- return $ relation ['data ' ]['type ' ] === $ type ;
79- })) > 0 , sprintf ('There is a relationship with type "%s" for resource "%s" ' , $ type , $ this ->getIdentifierMessageFor ()));
79+ PHPUnit::assertFalse (
80+ count ($ this ->filterResources ($ this ->relationships , $ type )) > 0 ,
81+ sprintf ('There is a relationship with type "%s" for resource "%s" ' , $ type , $ this ->getIdentifierMessageFor ())
82+ );
8083
8184 if ($ withIncluded ) {
82- PHPUnit::assertFalse (count (array_filter ($ this ->includeds , function ($ included ) use ($ type ) {
83- return $ included ['type ' ] === $ type ;
84- })) > 0 , sprintf ('There is a included relationship with type "%s" ' , $ type ));
85+ PHPUnit::assertFalse (
86+ count ($ this ->filterResources ($ this ->includeds , $ type )) > 0 ,
87+ sprintf ('There is a included relationship with type "%s" ' , $ type )
88+ );
8589 }
8690
8791 return $ this ;
@@ -99,14 +103,16 @@ public function hasRelationshipWith(Model $model, $withIncluded = false)
99103 {
100104 $ type = JsonApi::getResourceType ($ model );
101105
102- PHPUnit::assertTrue (count (array_filter ($ this ->relationships , function ($ relation ) use ($ type , $ model ) {
103- return $ relation ['data ' ]['type ' ] === $ type && $ relation ['data ' ]['id ' ] == $ model ->getKey ();
104- })) > 0 , sprintf ('There is no relationship "%s" for resource "%s" ' , $ this ->getIdentifierMessageFor ($ model ->getKey (), $ type ), $ this ->getIdentifierMessageFor ()));
106+ PHPUnit::assertTrue (
107+ count ($ this ->filterResources ($ this ->relationships , $ type , $ model ->getKey ())) > 0 ,
108+ sprintf ('There is no relationship "%s" for resource "%s" ' , $ this ->getIdentifierMessageFor ($ model ->getKey (), $ type ), $ this ->getIdentifierMessageFor ())
109+ );
105110
106111 if ($ withIncluded ) {
107- PHPUnit::assertTrue (count (array_filter ($ this ->includeds , function ($ included ) use ($ type , $ model ) {
108- return $ included ['type ' ] === $ type && $ included ['id ' ] == $ model ->getKey ();
109- })) > 0 , sprintf ('There is no included relationship "%s" ' , $ this ->getIdentifierMessageFor ($ model ->getKey (), $ type )));
112+ PHPUnit::assertTrue (
113+ count ($ this ->filterResources ($ this ->includeds , $ type , $ model ->getKey ())) > 0 ,
114+ sprintf ('There is no included relationship "%s" ' , $ this ->getIdentifierMessageFor ($ model ->getKey (), $ type ))
115+ );
110116 }
111117
112118 return $ this ;
@@ -124,16 +130,56 @@ public function hasNotRelationshipWith(Model $model, $withIncluded = false)
124130 {
125131 $ type = JsonApi::getResourceType ($ model );
126132
127- PHPUnit::assertFalse (count (array_filter ($ this ->relationships , function ($ relation ) use ($ type , $ model ) {
128- return $ relation ['data ' ]['type ' ] === $ type && $ relation ['data ' ]['id ' ] == $ model ->getKey ();
129- })) > 0 , sprintf ('There is a relationship "%s" for resource "%s" ' , $ this ->getIdentifierMessageFor ($ model ->getKey (), $ type ), $ this ->getIdentifierMessageFor ()));
133+ PHPUnit::assertFalse (
134+ count ($ this ->filterResources ($ this ->relationships , $ type , $ model ->getKey ())) > 0 ,
135+ sprintf ('There is a relationship "%s" for resource "%s" ' , $ this ->getIdentifierMessageFor ($ model ->getKey (), $ type ), $ this ->getIdentifierMessageFor ())
136+ );
130137
131138 if ($ withIncluded ) {
132- PHPUnit::assertFalse (count (array_filter ($ this ->includeds , function ($ included ) use ($ type , $ model ) {
133- return $ included ['type ' ] === $ type && $ included ['id ' ] == $ model ->getKey ();
134- })) > 0 , sprintf ('There is a included relationship "%s" ' , $ this ->getIdentifierMessageFor ($ model ->getKey (), $ type )));
139+ PHPUnit::assertFalse (
140+ count ($ this ->filterResources ($ this ->includeds , $ type , $ model ->getKey ())) > 0 ,
141+ sprintf ('There is a included relationship "%s" ' , $ this ->getIdentifierMessageFor ($ model ->getKey (), $ type ))
142+ );
135143 }
136144
137145 return $ this ;
138146 }
147+
148+ /**
149+ * Filter array of resources by a provided identifier.
150+ *
151+ * @param array $resources
152+ * @param string $type
153+ * @param mixed $id
154+ * @return array
155+ */
156+ protected function filterResources (array $ resources , string $ type , $ id = null )
157+ {
158+ return array_filter ($ resources , function ($ resource ) use ($ type , $ id ) {
159+ return $ this ->filterResourceWithIdentifier ($ resource , $ type , $ id );
160+ });
161+ }
162+
163+ /**
164+ * Filter provided resource with given identifier.
165+ *
166+ * @param array $resource
167+ * @param string $type
168+ * @param mixed $id
169+ * @return bool
170+ */
171+ protected function filterResourceWithIdentifier (array $ resource , string $ type , $ id = null )
172+ {
173+ if (is_array ($ resource ) && ! isset ($ resource ['type ' ])) {
174+ return count ($ this ->filterResources ($ resource , $ type , $ id )) > 0 ;
175+ }
176+
177+ $ condition = $ resource ['type ' ] === $ type ;
178+
179+ if ($ id ) {
180+ $ condition &= $ resource ['id ' ] == $ id ;
181+ }
182+
183+ return (bool ) $ condition ;
184+ }
139185}
0 commit comments