/** * @return Response[] * @throws Exception */ public function getResponses() { if (!$this->isMultipart()) { throw new Exception('Response is not multipart'); } $contentType = $this->getHeader('content-type'); if (!stristr($contentType, 'multipart/mixed')) { throw new Exception('Response is not multipart/mixed'); } $body = $this->getBody(); //TODO Read stream as stream // Step 1. Split boundaries preg_match(self::BOUNDARY_REGEXP, $contentType, $matches); $boundary = $matches[1]; $parts = explode(self::BOUNDARY_SEPARATOR . $boundary, $body); // First empty part out if (!$parts[0] || !trim($parts[0])) { array_shift($parts); } // Last "--" part out if (trim($parts[sizeof($parts) - 1]) == self::BOUNDARY_SEPARATOR) { array_pop($parts); } // Step 2. Create status info object $statusInfoObj = new Response($this->getStatus(), array_shift($parts)); $statusInfo = $statusInfoObj->getJson()->response; // Step 3. Parse all parts into Response objects $responses = array(); foreach ($parts as $i => $part) { $partInfo = $statusInfo[$i]; $responses[] = new Response($partInfo->status, $part); } return $responses; }