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