joskolenberg / cardinal
Small wrapper for cardinal directions
v1.0.0
2019-09-07 13:42 UTC
Requires
- php: ^7.0
Requires (Dev)
- phpunit/phpunit: ^6.0|^7.0|^8.0
This package is auto-updated.
Last update: 2024-11-08 01:37:25 UTC
README
Cardinal
Little wrapper object to handle cardinal/compass directions.
Installation
composer require joskolenberg/cardinal
Examples
Create object with degrees (0 to 359.999) and format into string.
Cardinal::make(45)->format() // => 'NE'
Use precision parameter to be more or less precise.
Cardinal::make(66)->format(1) // => 'E' Cardinal::make(66)->format(2) // => 'NE' Cardinal::make(66)->format(3) // => 'ENE'
Use second and third parameter to switch to fully written directions with divider.
Cardinal::make(250)->format(3, true, '-') // => 'WEST-SOUTH-WEST'
Create object from string (with or without any divider) and return degrees.
Cardinal::make('NNW')->degrees // => 337.5 Cardinal::make('South-West')->degrees // => 225 Cardinal::make('West NorthWest')->degrees // => 292.5
Use formatLocalized()
to get localized string.
Cardinal::make(250)->formatLocalized(3, true, '-') // => 'West-South-West'
Override lang()
to create your own localization.
use JosKolenberg\Cardinal\Cardinal; class DutchCardinal extends Cardinal { protected function lang(): array { return [ 'N' => 'N', 'E' => 'O', 'S' => 'Z', 'W' => 'W', 'NORTH' => 'Noord', 'EAST' => 'Oost', 'SOUTH' => 'Zuid', 'WEST' => 'West', ]; } }
Cardinal::make(157.5)->formatLocalized(3, true, '-') // => 'Zuid-Zuid-Oost'
Or integrate with localization in your framework. E.g. Laravel:
use JosKolenberg\Cardinal\Cardinal; class LocalizedCardinal extends Cardinal { protected function lang(): array { return [ 'N' => __('app.cardinal.n'), 'E' => __('app.cardinal.e'), 'S' => __('app.cardinal.s'), 'W' => __('app.cardinal.w'), 'NORTH' => __('app.cardinal.north'), 'EAST' => __('app.cardinal.east'), 'SOUTH' => __('app.cardinal.south'), 'WEST' => __('app.cardinal.west'), ]; } }
That's it! Any suggestions or issues? Please contact me!
Happy coding!
Jos Kolenberg jos@kolenberg.net