Reader

Class
implements ReaderInterface
Read only: Yes Final: Yes

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.

Table of Contents

Interfaces

ReaderInterface

Reads and exposes metadata from composer.json for license generation.

Properties

 : array<string|int, mixed>

Methods

__construct()

Creates a new Reader instance.

 : mixed
getAuthors()

Retrieves the list of authors from composer.json.

 : array<int, array{name: string, email: string, homepage: string, role: string}>
getLicense()

Retrieves the license identifier from composer.json.

 : string|null
getPackageName()

Retrieves the package name from composer.json.

 : string
getVendor()

Extracts the vendor name from the package name.

 : string|null
getYear()

Returns the current year for copyright notices.

 : int
extractLicense()

Extracts a single license from an array of licenses.

 : string|null
readData()

Reads and parses the JSON content from the source file.

 : array<string|int, mixed>
Properties

$data

Private
private array<string|int, mixed> $data
Methods

__construct()

Public

Creates a new Reader instance.

public __construct(SplFileObject  $source) : mixed
Parameters
$source : SplFileObject

Description

The source file to read from, typically composer.json

Tags
throws
JsonException

Description

if the JSON content is invalid

getAuthors()

Public

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
array<int, array{name: string, email: string, homepage: string, role: string}>

getLicense()

Public

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
string|null

Description

the license string, or null if not set or unsupported

getPackageName()

Public

Retrieves the package name from composer.json.

public getPackageName() : string
Return values
string

Description

the full package name (vendor/package), or empty string if not set

getVendor()

Public

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
string|null

Description

the vendor name, or null if package has no vendor prefix

getYear()

Public

Returns the current year for copyright notices.

public getYear() : int
Return values
int

Description

the current year as an integer

extractLicense()

Private

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
string|null

Description

a single license string, or null if extraction is not possible

readData()

Private

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

Tags
throws
JsonException

Description

if the JSON is invalid

Return values
array<string|int, mixed>

Description

The parsed JSON data as an associative array