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.
| Method | Return 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.
| Method | Result |
|---|---|
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
| Method | Available on | Effect |
|---|---|---|
whereProvince(int $provinceId, int ...$moreProvinceIds) | DistrictQuery, LocalLevelQuery | Keeps records within any supplied province ID. |
whereDistrict(int $districtId, int ...$moreDistrictIds) | LocalLevelQuery | Keeps records within any supplied district ID. |
whereType(string $type, string ...$moreTypes) | LocalLevelQuery | Keeps records matching any supplied LocalLevelType key. |
municipalities() | LocalLevelQuery | Keeps metropolitan cities, sub-metropolitan cities, and municipalities. |
ruralMunicipalities() | LocalLevelQuery | Keeps 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.
| Record | Additional methods | Additional fields in scalar output |
|---|---|---|
Province | districts(): DistrictQuery, localLevels(): LocalLevelQuery | - |
District | province(): Province, provinceId(): int, localLevels(): LocalLevelQuery | province_id |
LocalLevel | district(): District, districtId(): int, province(): Province, provinceId(): int, type(): string, typeLabel(): string | province_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.
| Constant | Classification key | English label | Nepali label |
|---|---|---|---|
METROPOLITAN_CITY | metropolitan_city | Ma.Na.Pa. | म.न.पा. |
SUB_METROPOLITAN_CITY | sub_metropolitan_city | Upa.Ma. | उप-म. |
MUNICIPALITY | municipality | Na.Pa. | न.पा. |
RURAL_MUNICIPALITY | rural_municipality | Ga.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
| Situation | Result |
|---|---|
find(), findByName(), or first() has no match | null |
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 term | InvalidArgumentException |
| Wrong PHP argument type in a strict caller | TypeError |
| Writing properties on a record or query | LogicException |
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.