$parameters */ private function __construct( private readonly ContentType $contentType, private readonly Method $method, private readonly ResourceUri $uri, private readonly array $parameters = [], ) { // .. } /** * Creates a new Payload value object from the given parameters. */ public static function list(string $resource): self { $contentType = ContentType::JSON; $method = Method::GET; $uri = ResourceUri::list($resource); return new self($contentType, $method, $uri); } /** * Creates a new Payload value object from the given parameters. */ public static function retrieve(string $resource, string $id, string $suffix = ''): self { $contentType = ContentType::JSON; $method = Method::GET; $uri = ResourceUri::retrieve($resource, $id, $suffix); return new self($contentType, $method, $uri); } /** * Creates a new Payload value object from the given parameters. */ public static function retrieveContent(string $resource, string $id): self { $contentType = ContentType::JSON; $method = Method::GET; $uri = ResourceUri::retrieveContent($resource, $id); return new self($contentType, $method, $uri); } /** * Creates a new Payload value object from the given parameters. * * @param array $parameters */ public static function create(string $resource, array $parameters): self { $contentType = ContentType::JSON; $method = Method::POST; $uri = ResourceUri::create($resource); return new self($contentType, $method, $uri, $parameters); } /** * Creates a new Payload value object from the given parameters. * * @param array $parameters */ public static function upload(string $resource, array $parameters): self { $contentType = ContentType::MULTIPART; $method = Method::POST; $uri = ResourceUri::upload($resource); return new self($contentType, $method, $uri, $parameters); } /** * Creates a new Payload value object from the given parameters. */ public static function cancel(string $resource, string $id): self { $contentType = ContentType::JSON; $method = Method::POST; $uri = ResourceUri::cancel($resource, $id); return new self($contentType, $method, $uri); } /** * Creates a new Payload value object from the given parameters. */ public static function delete(string $resource, string $id): self { $contentType = ContentType::JSON; $method = Method::DELETE; $uri = ResourceUri::delete($resource, $id); return new self($contentType, $method, $uri); } /** * Creates a new Psr 7 Request instance. */ public function toRequest(BaseUri $baseUri, Headers $headers): Psr7Request { $body = null; $uri = $baseUri->toString().$this->uri->toString(); $headers = $headers->withContentType($this->contentType); if ($this->method === Method::POST) { if ($this->contentType === ContentType::MULTIPART) { $body = new MultipartStream( array_map(fn ($key): array => ['name' => $key, 'contents' => $this->parameters[$key]], array_keys($this->parameters)) ); $headers = $headers->withContentType($this->contentType, '; boundary='.$body->getBoundary()); } else { $body = json_encode($this->parameters, JSON_THROW_ON_ERROR); } } return new Psr7Request($this->method->value, $uri, $headers->toArray(), $body); } }