Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Public API

This chapter lists the consumer API. The namespace prefix is KodeFarmers\NepalData. Import record classes from Entities and query classes from Queries under that namespace. Obtain records and queries from the core, the Laravel facade, or a relationship; their constructors and the internal catalog are not consumer APIs.

Entry points

Create the standalone service with new NepalData(). In Laravel, inject KodeFarmers\NepalData\NepalData or import the facade KodeFarmers\NepalData\Facades\NepalData and call the same entry points statically.

MethodReturn type
provinces(string $language = Language::ENGLISH)ProvinceQuery
districts(string $language = Language::ENGLISH)DistrictQuery
localLevels(string $language = Language::ENGLISH)LocalLevelQuery
raw()RawData

Language::ENGLISH is 'en', and Language::NEPALI is 'ne'. A language applies to the entire query and to relationships reached from its results. See Languages and Identifiers.

Methods shared by all queries

T denotes the specific entity: Province, District, or LocalLevel.

MethodResult
all(), get()list<T>: all current matches as a zero-indexed PHP array.
first()T for the first current match, or null.
find(int $id)The matching T in the current query, or null.
findOrFail(int $id)The matching T, or OutOfBoundsException if the ID has no match.
whereId(int $id, int ...$moreIds)A new query matching any supplied ID in the current selection.
whereName(string $name)A new query keeping every exact name match.
findByName(string $name)A single exact match as T, null if absent, or DomainException if ambiguous.
search(string $term)A new query keeping names that contain the term.
filter(callable $predicate)A new query keeping records accepted by a predicate with the signature callable(T): bool.
orderByName()A new query ordered by name, with ascending ID breaking ties.
count() or count($query)The number of matches. Queries implement Countable.
ids()list<int> of matching IDs in query order.
names()list<string> of matching names in query order, including repeated names.
toArray()A list of scalar row arrays in query order.
jsonSerialize()The same row list as toArray(). json_encode($query) encodes it as JSON.

Use get() or all() before iterating with foreach. Queries themselves provide counting and JSON serialization; they are not iterators. See Queries and Filters for ordering and predicate examples.

Administrative filters

MethodAvailable onEffect
whereProvince(int $provinceId, int ...$moreProvinceIds)DistrictQuery, LocalLevelQueryKeeps records within any supplied province ID.
whereDistrict(int $districtId, int ...$moreDistrictIds)LocalLevelQueryKeeps records within any supplied district ID.
whereType(string $type, string ...$moreTypes)LocalLevelQueryKeeps records matching any supplied LocalLevelType key.
municipalities()LocalLevelQueryKeeps metropolitan cities, sub-metropolitan cities, and municipalities.
ruralMunicipalities()LocalLevelQueryKeeps only rural municipalities.

Each returns the same concrete type of query with the filter applied. Alternatives within a call match any supplied value; separate calls narrow the previous selection.

Records and relationships

Every record has id(): int, name(): string, language(): string, toArray(): array, and jsonSerialize(): array. Records are read-only. Names preserve their source spelling. Array and JSON output include only scalar fields.

RecordAdditional methodsAdditional fields in scalar output
Provincedistricts(): DistrictQuery, localLevels(): LocalLevelQuery-
Districtprovince(): Province, provinceId(): int, localLevels(): LocalLevelQueryprovince_id
LocalLeveldistrict(): District, districtId(): int, province(): Province, provinceId(): int, type(): string, typeLabel(): stringprovince_id, district_id, type, type_label

The common fields are id, name, and language. province_id and district_id are integer package IDs. The three record classes live in KodeFarmers\NepalData\Entities. Named IDs for the first two are listed in Province and District Constants. For examples, see Relationships and Output.

LocalLevelType

Use the constants on KodeFarmers\NepalData\LocalLevelType with whereType(). A record’s type() returns the key; typeLabel() returns the original abbreviation.

ConstantClassification keyEnglish labelNepali label
METROPOLITAN_CITYmetropolitan_cityMa.Na.Pa.म.न.पा.
SUB_METROPOLITAN_CITYsub_metropolitan_cityUpa.Ma.उप-म.
MUNICIPALITYmunicipalityNa.Pa.न.पा.
RURAL_MUNICIPALITYrural_municipalityGa.Pa.गा.पा.

These source abbreviations are labels, not accepted arguments to whereType().

RawData

raw() returns KodeFarmers\NepalData\RawData. Its five methods are all(), provinces(), districts(), provincesWithDistricts(), and districtsWithLocalBodies(). Each accepts string $language = Language::ENGLISH and returns the original PHP array for that view. See Original Arrays for each shape and source file.

Errors and empty results

SituationResult
find(), findByName(), or first() has no matchnull
A valid positive ID has no match for findOrFail()OutOfBoundsException
Several records match findByName()DomainException
No records remain after filtering[] from list/array methods, 0 from count(), null from first()
ID less than 1, unsupported language or classification, or a blank or malformed UTF-8 termInvalidArgumentException
Wrong PHP argument type in a strict callerTypeError
Writing properties on a record or queryLogicException

An unknown positive ID is a valid lookup value that matches no record. It does not cancel other matches in a filter that accepts multiple IDs. A record filtered out of the current query is treated the same as an absent record. User-supplied predicates can also throw their own exceptions, which the package does not intercept.