@@ 484-510 (lines=27) @@ | ||
481 | /** |
|
482 | * Create json body |
|
483 | */ |
|
484 | private function createBodyJson() |
|
485 | { |
|
486 | // json encode arrays |
|
487 | if ($this->bodyIsArray) { |
|
488 | $this->methodBody->add('$body = json_encode(%s);', $this->body); |
|
489 | ||
490 | return; |
|
491 | } |
|
492 | ||
493 | // if parts exist, json encode the parts |
|
494 | if (!empty($this->bodyParts)) { |
|
495 | $this->methodBody->add('$body = json_encode(%s);', $this->arrayToString($this->bodyParts)); |
|
496 | ||
497 | return; |
|
498 | } |
|
499 | ||
500 | // if it's an object, serialize it unless it implements \JsonSerializable |
|
501 | if ($this->bodyIsObject) { |
|
502 | if ($this->bodyIsJsonSerializable) { |
|
503 | $this->methodBody->add('$body = json_encode(%s);', $this->body); |
|
504 | ||
505 | return; |
|
506 | } |
|
507 | ||
508 | $this->serializeObject('$bodySerializationContext', '$body', $this->body); |
|
509 | } |
|
510 | } |
|
511 | ||
512 | /** |
|
513 | * Normalize body as array |
|
@@ 515-541 (lines=27) @@ | ||
512 | /** |
|
513 | * Normalize body as array |
|
514 | */ |
|
515 | private function createBodyArray() |
|
516 | { |
|
517 | // if it's already an array, set to variable |
|
518 | if ($this->bodyIsArray) { |
|
519 | $this->methodBody->add('$bodyArray = %s;', $this->body); |
|
520 | ||
521 | return; |
|
522 | } |
|
523 | ||
524 | // if parts exist, set to variable |
|
525 | if (!empty($this->bodyParts)) { |
|
526 | $this->methodBody->add('$bodyArray = %s;', $this->arrayToString($this->bodyParts)); |
|
527 | ||
528 | return; |
|
529 | } |
|
530 | ||
531 | // if it implements \JsonSerializable, call jsonSerialize() to get array |
|
532 | if ($this->bodyIsJsonSerializable) { |
|
533 | $this->methodBody->add('$bodyArray = %s->jsonSerialize();', $this->body); |
|
534 | ||
535 | return; |
|
536 | } |
|
537 | ||
538 | // otherwise, serialize and json_decode() |
|
539 | $this->serializeObject('$bodySerializationContext', '$serializedBody', $this->body); |
|
540 | $this->methodBody->add('$bodyArray = json_decode($serializedBody, true);'); |
|
541 | } |
|
542 | ||
543 | /** |
|
544 | * Helper method to serialize an object |