Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/phpgt/typesafegetter
An interface for objects that expose type-safe getter methods.
https://github.com/phpgt/typesafegetter
data-transfer-object getter-functions interface type-safety
Last synced: about 1 month ago
JSON representation
An interface for objects that expose type-safe getter methods.
- Host: GitHub
- URL: https://github.com/phpgt/typesafegetter
- Owner: PhpGt
- Created: 2021-01-16T17:28:10.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2023-04-28T16:09:13.000Z (over 1 year ago)
- Last Synced: 2024-11-14T15:20:55.692Z (about 1 month ago)
- Topics: data-transfer-object, getter-functions, interface, type-safety
- Language: PHP
- Homepage:
- Size: 62.5 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
An interface for objects that expose type-safe getter methods.
==============================================================Throughout PHP.Gt repositories, wherever an object represents enclosed data, a consistent interface is used to expose the data in a type-safe manner.
***
The following methods are defined by this interface:
+ `get(string $name):mixed` - A non-type-safe getter, used for getting keys that are not of an inbuilt type
+ `getString(string $name):?string`
+ `getInt(string $name):?int`
+ `getFloat(string $name):?float`
+ `getBool(string $name):?bool`
+ `getDateTime(string $name):?DateTimeInterface`
+ `getInstance(string $name, class-string $className):?T`Common areas you will see this interface used:
+ Database rows
+ User input (from the query string or posted form data)
+ Session and cookie storage
+ PHP.Gt's [DataObject](https://www.php.gt/dataobject) repository.`NullableTypeSafeGetter` trait
------------------------------A lot of repositories within PHP.Gt that utilise this class were repeating the same getter code, so this trait was introduced to remove the repetition. All getter functions of the interface are implemented, introducing a protected helper function `getNullableType` which removes the repetition of checking null values before casting them. The `getNullableType` function also allows a callback to be passed as the type parameter, allowing more complex nullable types to be constructed.