MultipartBuilder
in package
Builds multipart/form-data request bodies for the Apple News API.
Apple News requires articles to be submitted as MIME multipart messages. The message MUST contain an 'article.json' part and can optionally include a 'metadata' part and binary parts for bundle resources (images, fonts, etc.).
Tags
Table of Contents
Constants
- CRLF = "\r\n"
Properties
- $boundary : string
- $parts : array<int, array{name: string, filename: string|null, contentType: string, content: string}>
Methods
- __construct() : mixed
- addArticle() : self
- Convenience method to add the 'article.json' document part.
- addFont() : self
- Add a font file part.
- addImage() : self
- Add an image part from raw binary content.
- addImageFile() : self
- Add an image part from a local file path.
- addJson() : self
- Add a JSON part to the multipart body.
- addMetadata() : self
- Convenience method to add article metadata.
- addPart() : self
- Core method to add a generic part to the multipart body.
- build() : string
- Build the complete multipart raw body string.
- getBoundary() : string
- Get the random boundary string used.
- getContentType() : string
- Get the full Content-Type header value required for this request.
- guessImageContentType() : string
- Guess the MIME content type based on the file extension.
Constants
CRLF
private
mixed
CRLF
= "\r\n"
Properties
$boundary
private
string
$boundary
$parts
private
array<int, array{name: string, filename: string|null, contentType: string, content: string}>
$parts
= []
Methods
__construct()
public
__construct([string|null $boundary = null ]) : mixed
Parameters
- $boundary : string|null = null
-
Optional custom boundary string.
addArticle()
Convenience method to add the 'article.json' document part.
public
addArticle(string $json) : self
Parameters
- $json : string
-
The ANF JSON string.
Return values
selfaddFont()
Add a font file part.
public
addFont(string $name, string $content, string $filename) : self
Parameters
- $name : string
-
The part name.
- $content : string
-
Raw binary font data.
- $filename : string
-
The font filename.
Return values
selfaddImage()
Add an image part from raw binary content.
public
addImage(string $name, string $content, string $filename[, string|null $contentType = null ]) : self
Parameters
- $name : string
-
The part name.
- $content : string
-
Raw binary data.
- $filename : string
-
The filename to present in the part header.
- $contentType : string|null = null
-
Optional MIME type. Guessed from filename if null.
Return values
selfaddImageFile()
Add an image part from a local file path.
public
addImageFile(string $name, string $filePath) : self
Parameters
- $name : string
-
The part name/bundle identifier.
- $filePath : string
-
Absolute path to the local file.
Tags
Return values
selfaddJson()
Add a JSON part to the multipart body.
public
addJson(string $name, string $json[, string|null $filename = null ]) : self
Parameters
- $name : string
-
The part name (e.g., 'article.json', 'metadata').
- $json : string
-
The JSON string.
- $filename : string|null = null
-
Optional filename for the part.
Return values
selfaddMetadata()
Convenience method to add article metadata.
public
addMetadata(array<string, mixed> $metadata) : self
Parameters
- $metadata : array<string, mixed>
-
Metadata array (wrapped in 'data' key automatically).
Tags
Return values
selfaddPart()
Core method to add a generic part to the multipart body.
public
addPart(string $name, string $content, string $contentType[, string|null $filename = null ]) : self
Parameters
- $name : string
-
Part name.
- $content : string
-
Content data.
- $contentType : string
-
MIME content type.
- $filename : string|null = null
-
Optional filename.
Return values
selfbuild()
Build the complete multipart raw body string.
public
build() : string
Return values
stringgetBoundary()
Get the random boundary string used.
public
getBoundary() : string
Return values
stringgetContentType()
Get the full Content-Type header value required for this request.
public
getContentType() : string
Return values
stringguessImageContentType()
Guess the MIME content type based on the file extension.
private
guessImageContentType(string $filename) : string
Parameters
- $filename : string