@@ -85,7 +85,7 @@ public function __construct(Client $client, $type, array $attributes = [])
8585 public function __call ($ method , array $ arguments )
8686 {
8787 if ($ this ->isBuilderMethod ($ method )) {
88- return call_user_func_array ([ $ this ->newBuilder (), $ method], $ arguments );
88+ return $ this ->newBuilder ()-> $ method(... $ arguments );
8989 }
9090
9191 return $ this ->getRelatedFromMethod ($ method );
@@ -179,13 +179,13 @@ public function create(array $attributes)
179179 /**
180180 * Delete the model from the web service.
181181 *
182- * @param string $primaryKey
182+ * @param string $keyName
183183 * @return bool|null
184184 */
185- public function delete ($ primaryKey = null )
185+ public function delete ($ keyName = null )
186186 {
187187 if ($ this ->exists ) {
188- $ this ->client ->deleteObject ($ this ->type , $ this ->key ($ primaryKey ));
188+ $ this ->client ->deleteObject ($ this ->type , $ this ->key ($ keyName ));
189189 $ this ->exists = false ;
190190
191191 return true ;
@@ -201,9 +201,9 @@ public function delete($primaryKey = null)
201201 public function duplicate ($ newKey = null )
202202 {
203203 if ($ this ->exists ) {
204- $ response = $ this ->client ->cloneObject ($ this ->type , $ this ->original , $ this ->getDirty (), $ newKey );
204+ $ attributes = $ this ->client ->cloneObject ($ this ->type , $ this ->original , $ this ->getDirty (), $ newKey );
205205
206- $ model = $ this ->newInstance ($ response );
206+ $ model = $ this ->newInstance ($ attributes );
207207 $ model ->exists = true ;
208208
209209 $ this ->restore ();
@@ -229,16 +229,16 @@ public function find($filter, $sort = null)
229229 /**
230230 * Refresh the attributes of the model from the web service.
231231 *
232- * @param string $primaryKey
232+ * @param string $keyName
233233 * @return Model|null
234234 */
235- public function fresh ($ primaryKey = null )
235+ public function fresh ($ keyName = null )
236236 {
237237 if (!$ this ->exists ) {
238238 return null ;
239239 }
240240
241- $ fresh = $ this ->read ($ this ->key ($ primaryKey ));
241+ $ fresh = $ this ->read ($ this ->key ($ keyName ));
242242
243243 return $ fresh ;
244244 }
@@ -292,19 +292,19 @@ public function hasAttribute($attribute)
292292 *
293293 * @param string $relatedType
294294 * @param string $foreignKey
295- * @param string $primaryKey
295+ * @param string $keyName
296296 * @return Builder
297297 */
298- public function hasMany ($ relatedType , $ foreignKey , $ primaryKey = null )
298+ public function hasMany ($ relatedType , $ foreignKey , $ keyName = null )
299299 {
300300 $ builder = $ this ->client ->model ($ relatedType )->newBuilder ();
301301
302302 if ($ this ->isCompoundKey ($ foreignKey )) {
303- foreach ($ this ->getCompoundKeyArray ($ foreignKey , $ primaryKey ) as $ attribute => $ value ) {
303+ foreach ($ this ->getCompoundKeyArray ($ foreignKey , $ keyName ) as $ attribute => $ value ) {
304304 $ builder ->filter ('@ ' . $ attribute , $ value );
305305 }
306306 } else {
307- $ builder ->filter ('@ ' . $ foreignKey , $ this ->key ($ primaryKey ));
307+ $ builder ->filter ('@ ' . $ foreignKey , $ this ->key ($ keyName ));
308308 }
309309
310310 return $ builder ;
@@ -338,19 +338,19 @@ public function joinKeys(array $keys)
338338 */
339339 public function jsonSerialize ()
340340 {
341- return $ this ->attributes ;
341+ return $ this ->toArray () ;
342342 }
343343
344344 /**
345345 * Get the model's primary key.
346346 *
347- * @param string $primaryKey
347+ * @param string $keyName
348348 * @return string|int
349349 * @throws UnexpectedValueException if the key is null.
350350 */
351- public function key ($ primaryKey = null )
351+ public function key ($ keyName = null )
352352 {
353- $ key = $ this ->getAttribute ($ primaryKey ?: $ this ->guessPrimaryKey ());
353+ $ key = $ this ->getAttribute ($ keyName ?: $ this ->guessPrimaryKey ());
354354
355355 if ($ key == null ) {
356356 throw new UnexpectedValueException ('Key must not be null. ' );
@@ -365,14 +365,17 @@ public function key($primaryKey = null)
365365 * @param string $relatedType
366366 * @param string $baseObject
367367 * @param string $baseObjectKey
368- * @param string|null $primaryKey
368+ * @param string|null $keyName
369369 * @return Builder
370370 */
371- public function morphMany ($ relatedType , $ baseObject = 'baseObject ' , $ baseObjectKey = 'baseObjectKey ' , $ primaryKey = null )
371+ public function morphMany ($ relatedType , $ baseObject = 'baseObject ' , $ baseObjectKey = 'baseObjectKey ' , $ keyName = null )
372372 {
373- return $ this ->client ->model ($ relatedType )
374- ->filter ('@ ' . $ baseObject , $ this ->type )
375- ->filter ('@ ' . $ baseObjectKey , $ this ->key ($ primaryKey ));
373+ $ builder = $ this ->client ->model ($ relatedType )->newBuilder ();
374+
375+ $ builder ->filter ('@ ' . $ baseObject , $ this ->type );
376+ $ builder ->filter ('@ ' . $ baseObjectKey , $ this ->key ($ keyName ));
377+
378+ return $ builder ;
376379 }
377380
378381 /**
@@ -437,8 +440,8 @@ public function offsetUnset($offset)
437440 */
438441 public function read ($ key )
439442 {
440- // This is intentionally not strict. The API considers an
441- // integer 0 to be null and will respond with a fault.
443+ // This is intentionally not strict. The web service considers
444+ // an integer 0 to be null and will respond with a fault.
442445 if ($ key == null ) {
443446 return null ;
444447 }
@@ -481,11 +484,11 @@ public function readOrFail($key)
481484 public function save ()
482485 {
483486 if ($ this ->exists ) {
484- // Update an existing object in Pace .
487+ // Update an existing object.
485488 $ this ->attributes = $ this ->client ->updateObject ($ this ->type , $ this ->attributes );
486489
487490 } else {
488- // Create a new object in Pace and fill default values.
491+ // Create a new object and fill default values.
489492 $ this ->attributes = $ this ->client ->createObject ($ this ->type , $ this ->attributes );
490493 $ this ->exists = true ;
491494 }
@@ -526,6 +529,16 @@ public function splitKey($key = null)
526529 return explode (': ' , $ key );
527530 }
528531
532+ /**
533+ * Convert the instance to an array.
534+ *
535+ * @return array
536+ */
537+ public function toArray ()
538+ {
539+ return $ this ->attributes ;
540+ }
541+
529542 /**
530543 * Unset the specified model attribute.
531544 *
@@ -557,14 +570,14 @@ protected function getCompoundKey($foreignKey)
557570 * Get a compound key array for a "has many" relationship.
558571 *
559572 * @param string $foreignKey
560- * @param string $primaryKey
573+ * @param string $keyName
561574 * @return array
562575 */
563- protected function getCompoundKeyArray ($ foreignKey , $ primaryKey )
576+ protected function getCompoundKeyArray ($ foreignKey , $ keyName )
564577 {
565578 return array_combine (
566579 $ this ->splitKey ($ foreignKey ),
567- $ this ->splitKey ($ this ->key ($ primaryKey ))
580+ $ this ->splitKey ($ this ->key ($ keyName ))
568581 );
569582 }
570583
@@ -596,12 +609,16 @@ protected function getRelatedFromMethod($method)
596609 }
597610
598611 /**
599- * Attempt to guess the primary key field .
612+ * Attempt to guess the primary key name .
600613 *
601614 * @return string
602615 */
603616 protected function guessPrimaryKey ()
604617 {
618+ if ($ keyName = Type::keyName ($ this ->type )) {
619+ return $ keyName ;
620+ }
621+
605622 if ($ this ->hasAttribute (Client::PRIMARY_KEY )) {
606623 return Client::PRIMARY_KEY ;
607624 }
0 commit comments