From d8c6494dbdf58c6b88cd744d6a7a51d012a0f5e5 Mon Sep 17 00:00:00 2001 From: luke Date: Tue, 10 Nov 2020 16:39:32 +0000 Subject: [PATCH] Initial commit. --- .phpunit.result.cache | 1 + composer.json | 8 ++--- src/Facades/GiveAnInch.php | 13 +++++++ src/GiveAnInch.php | 41 +++++++++++++++++++++ src/GiveAnInchServiceProvider.php | 32 +++++++++++++++++ src/Giveaninch.php | 8 ----- src/GiveaninchFacade.php | 21 ----------- src/GiveaninchServiceProvider.php | 60 ------------------------------- src/Within.php | 12 +++++++ tests/ExampleTest.php | 20 ++++++++--- 10 files changed, 118 insertions(+), 98 deletions(-) create mode 100644 .phpunit.result.cache create mode 100644 src/Facades/GiveAnInch.php create mode 100644 src/GiveAnInch.php create mode 100644 src/GiveAnInchServiceProvider.php delete mode 100644 src/Giveaninch.php delete mode 100644 src/GiveaninchFacade.php delete mode 100644 src/GiveaninchServiceProvider.php create mode 100644 src/Within.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..ecabc96 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +C:37:"PHPUnit\Runner\DefaultTestResultCache":596:{a:2:{s:7:"defects";a:2:{s:94:"RicorocksDigitalAgency\GiveAnInch\Tests\ExampleTest::it_calculates_the_distance_between_points";i:4;s:94:"RicorocksDigitalAgency\Giveaninch\Tests\ExampleTest::it_calculates_the_distance_between_points";i:3;}s:5:"times";a:3:{s:94:"RicorocksDigitalAgency\GiveAnInch\Tests\ExampleTest::it_calculates_the_distance_between_points";d:0.032;s:94:"RicorocksDigitalAgency\Giveaninch\Tests\ExampleTest::it_calculates_the_distance_between_points";d:0.041;s:103:"RicorocksDigitalAgency\Giveaninch\Tests\ExampleTest::it_can_say_if_something_is_within_a_certain_radius";d:0.037;}}} \ No newline at end of file diff --git a/composer.json b/composer.json index 9cca78c..42c5418 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": "^7.1", - "illuminate/support": "^6.0" + "illuminate/support": "^6.0|^7.0|^8.0" }, "require-dev": { "orchestra/testbench": "^4.0", @@ -30,7 +30,7 @@ }, "autoload-dev": { "psr-4": { - "RicorocksDigitalAgency\\Giveaninch\\Tests\\": "tests" + "RicorocksDigitalAgency\\Giveaninch\\\\Tests\\": "tests" } }, "scripts": { @@ -44,10 +44,10 @@ "extra": { "laravel": { "providers": [ - "RicorocksDigitalAgency\\Giveaninch\\GiveaninchServiceProvider" + "GiveAnInchServiceProvider" ], "aliases": { - "Giveaninch": "RicorocksDigitalAgency\\Giveaninch\\GiveaninchFacade" + "Giveaninch": "GiveAnInch" } } } diff --git a/src/Facades/GiveAnInch.php b/src/Facades/GiveAnInch.php new file mode 100644 index 0000000..7a7ce55 --- /dev/null +++ b/src/Facades/GiveAnInch.php @@ -0,0 +1,13 @@ +fromLat = $lat; + $this->fromLng = $lng; + + return $this; + } + + public function to($lat, $lng) + { + $this->toLat = $lat; + $this->toLng = $lng; + + return $this; + } + + public function getDistance() + { + $ky = 40000 / 360; + $kx = cos(pi() * $this->fromLat / 180.0) * $ky; + $dy = abs($this->fromLat - $this->toLat) * $ky; + $dx = abs($this->fromLng - $this->toLng) * $kx; + + return sqrt($dx * $dx + $dy * $dy); + } + + public function isWithin(float $radius) + { + return $this->getDistance() < $radius; + } + +} diff --git a/src/GiveAnInchServiceProvider.php b/src/GiveAnInchServiceProvider.php new file mode 100644 index 0000000..620a36f --- /dev/null +++ b/src/GiveAnInchServiceProvider.php @@ -0,0 +1,32 @@ +app->runningInConsole()) { + $this->publishes([ + __DIR__.'/../config/config.php' => config_path('giveaninch.php'), + ], 'config'); + } + } + + /** + * Register the application services. + */ + public function register() + { + $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'giveaninch'); + + $this->app->bind('giveaninch', function () { + return new GiveAnInch; + }); + } +} diff --git a/src/Giveaninch.php b/src/Giveaninch.php deleted file mode 100644 index 2afe803..0000000 --- a/src/Giveaninch.php +++ /dev/null @@ -1,8 +0,0 @@ -loadTranslationsFrom(__DIR__.'/../resources/lang', 'giveaninch'); - // $this->loadViewsFrom(__DIR__.'/../resources/views', 'giveaninch'); - // $this->loadMigrationsFrom(__DIR__.'/../database/migrations'); - // $this->loadRoutesFrom(__DIR__.'/routes.php'); - - if ($this->app->runningInConsole()) { - $this->publishes([ - __DIR__.'/../config/config.php' => config_path('giveaninch.php'), - ], 'config'); - - // Publishing the views. - /*$this->publishes([ - __DIR__.'/../resources/views' => resource_path('views/vendor/giveaninch'), - ], 'views');*/ - - // Publishing assets. - /*$this->publishes([ - __DIR__.'/../resources/assets' => public_path('vendor/giveaninch'), - ], 'assets');*/ - - // Publishing the translation files. - /*$this->publishes([ - __DIR__.'/../resources/lang' => resource_path('lang/vendor/giveaninch'), - ], 'lang');*/ - - // Registering package commands. - // $this->commands([]); - } - } - - /** - * Register the application services. - */ - public function register() - { - // Automatically apply the package configuration - $this->mergeConfigFrom(__DIR__.'/../config/config.php', 'giveaninch'); - - // Register the main class to use with the facade - $this->app->singleton('giveaninch', function () { - return new Giveaninch; - }); - } -} diff --git a/src/Within.php b/src/Within.php new file mode 100644 index 0000000..d7b5b3f --- /dev/null +++ b/src/Within.php @@ -0,0 +1,12 @@ +assertEquals(0, GiveAnInch::from(0, 0)->to(0, 0)->getDistance()); + $this->assertEquals(1571.3484026368, GiveAnInch::from(0, 0)->to(10, 10)->getDistance()); + $this->assertEquals(15530.564562899437, GiveAnInch::from(-5, -50)->to(70, 68.4)->getDistance()); + } + /** @test */ - public function true_is_true() + public function it_can_say_if_something_is_within_a_certain_radius() { - $this->assertTrue(true); + $this->assertTrue(GiveAnInch::from(0, 0)->to(0, 0)->isWithin(5)); + $this->assertFalse(GiveAnInch::from(0, 0)->to(100, 100)->isWithin(5)); } }