Giter Site home page Giter Site logo

cardinal's Introduction

Scrutinizer Code Quality Code Coverage Build Status Total Downloads Latest Stable Version License

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 [email protected]

cardinal's People

Watchers

Jos Kolenberg avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.