Reads composer.json and exposes metadata for license generation.
Description
This class parses a composer.json file via SplFileObject and provides methods to extract license information, package name, authors, vendor, and the current year for copyright notices.
Interfaces
Properties
Methods
Creates a new Reader instance.
Retrieves the list of authors from composer.json.
Retrieves the license identifier from composer.json.
Retrieves the package name from composer.json.
Extracts the vendor name from the package name.
Returns the current year for copyright notices.
Extracts a single license from an array of licenses.
Reads and parses the JSON content from the source file.
Creates a new Reader instance.
public
__construct(SplFileObject
$source) : mixed
Parameters
$source
:
SplFileObject
Description
The source file to read from, typically composer.json
Description
if the JSON content is invalid
Retrieves the list of authors from composer.json.
public
getAuthors() : array<int, array{name: string, email: string, homepage: string, role: string}>
Description
Each author is normalized to include name, email, homepage, and role fields. Returns an empty array if no authors are defined.
Return values
Retrieves the license identifier from composer.json.
public
getLicense() : string|null
Description
If the license is a single string, returns it directly. If it's an array with one element, extracts that element. Returns null if no license is set or if multiple licenses are specified.
Return values
Description
the license string, or null if not set or unsupported
Retrieves the package name from composer.json.
public
getPackageName() : string
Return values
Description
the full package name (vendor/package), or empty string if not set
Extracts the vendor name from the package name.
public
getVendor() : string|null
Description
The package name is expected in vendor/package format. Returns null if no package name is set or if the package has no vendor prefix.
Return values
Description
the vendor name, or null if package has no vendor prefix
Returns the current year for copyright notices.
public
getYear() : int
Return values
Description
the current year as an integer
Extracts a single license from an array of licenses.
private
extractLicense(
array<string|int, string>
$license) : string|null
Description
Returns the first license if exactly one element exists. Returns null if the array is empty or contains multiple licenses.
Parameters
$license
:
array<string|int, string>
Description
The license array to extract from
Return values
Description
a single license string, or null if extraction is not possible
Reads and parses the JSON content from the source file.
private
readData(SplFileObject
$source) : array<string|int, mixed>
Parameters
$source
:
SplFileObject
Description
The source file to read from
Description
if the JSON is invalid
Return values
Description
The parsed JSON data as an associative array