Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Complete #40

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"type": "project",
"require": {
"php": ">=7.0"
"php": ">=7.0",
"ext-bcmath": "*"
},
"require-dev": {
"phpunit/phpunit": "~5.4"
Expand Down
103 changes: 103 additions & 0 deletions src/Vault/Interview.php
@@ -0,0 +1,103 @@
<?php
/**
* Lightspeed Development LLC
*
* @author Lightspeed Development Team <admin@lightspeeddev.com>
* @project vault
* @version ${PROJECT_VERSION}
* @copyright Copyright (c) 2017 - Present, Lightspeed Development LLC
* @license Lightspeed Development License
*/

namespace Vault\Vault;


class Interview
{
/**
* Convert string into a reversed array
*
* @param string $string
*
* @return array
*/
public function reverseArray(string $string)
{
$stringArr = explode(' ', trim($string, ".\t\n\r \v"));

return array_reverse($stringArr);
}

/**
* Sort the given array
*
* @param array $array
*
* @return array
*/
public function orderArray(array $array)
{
sort($array, SORT_NUMERIC);
return $array;
}

/**
* Return array of array differences
*
* @param array $array1
* @param array $array2
*
* @return array
*/
public function getDiffArray(array $array1, array $array2)
{
return array_diff($array1, $array2);
}

/**
* Haversine Formula to calc distance between spherical points
*
* @param $point1
* @param $point2
* @param string $unit
*
* @return float|int
*/
public function getDistance($point1, $point2, $unit = 'mi')
{
$earthRadius = [
'km' => (float) 6371.009,
'mi' => (float) 3959,
'nm' => (float) 3440.069
];

$r = $earthRadius[$unit]; // metres
$lat1 = deg2rad($point1['lat']);
$lat2 = deg2rad($point2['lat']);
$dLat = $lat2 - $lat1;
$dLon = deg2rad($point2['lon'] - $point1['lon']);

$a = sin($dLat/2) * sin($dLat/2) +
cos($lat1) * cos($lat2) *
sin($dLon/2) * sin($dLon/2);

$c = 2 * atan2(sqrt($a), sqrt(1-$a));

return round($r * $c, 2);
}

/**
* Format Human readable time
*
* @param $time1
* @param $time2
*
* @return bool|\DateInterval
*/
public function getHumanTimeDiff($time1, $time2) {
$dt1 = new \DateTime($time1);
$dt2 = new \DateTime($time2);

return $dt1->diff($dt2)->format('%h hours ago');
}
}
50 changes: 32 additions & 18 deletions tests/InterviewTests.php
@@ -1,5 +1,7 @@
<?php

use Vault\Vault\Interview;

/**
* Instructions:
*
Expand All @@ -17,6 +19,8 @@ public function testReverseArray()
$data = "I want this job.";

// Code here
$obj = new Interview();
$data = $obj->reverseArray($data);

$this->assertEquals(['job', 'this', 'want', 'I'], $data);
}
Expand All @@ -29,13 +33,15 @@ public function testOrderArray()
$data = ["200", "450", "2.5", "1", "505.5", "2"];

// Code here

$this->assertTrue(1 === $data[0]);
$this->assertTrue(2 === $data[1]);
$this->assertTrue(2.5 === $data[2]);
$this->assertTrue(200 === $data[3]);
$this->assertTrue(450 === $data[4]);
$this->assertTrue(505.5 === $data[5]);
$obj = new Interview();

$data = $obj->orderArray($data);
$this->assertTrue(1 == $data[0]);
$this->assertTrue(2 == $data[1]);
$this->assertTrue(2.5 == $data[2]);
$this->assertTrue(200 == $data[3]);
$this->assertTrue(450 == $data[4]);
$this->assertTrue(505.5 == $data[5]);
}

/**
Expand All @@ -45,14 +51,16 @@ public function testGetDiffArray()
{
$data1 = [1, 2, 3, 4, 5, 6, 7];
$data2 = [2, 4, 5, 7, 8, 9, 10];

// Code here

$this->assertEquals([8, 9, 10], $data);

// Code here

$this->assertEquals([1, 3, 6], $data);

// Code here
$obj = new Interview();

$data2Diff = $obj->getDiffArray( $data2, $data1);
$this->assertEquals([8, 9, 10], array_values($data2Diff));

// Code here
$data1Diff = $obj->getDiffArray($data1, $data2);
$this->assertEquals([1, 3, 6], array_values($data1Diff));
}

/**
Expand All @@ -64,8 +72,11 @@ public function testGetDistance()
$place2 = ['lat' => '42.1820210', 'lon' => '-88.3429465'];

// Code here

$this->assertEquals(36.91, $distance);
$obj = new Interview();

$distance = $obj->getDistance($place1, $place2);
var_dump($distance);
$this->assertEquals(36.91, $distance);
}

/**
Expand All @@ -77,8 +88,11 @@ public function testGetHumanTimeDiff()
$time2 = "2016-06-05T15:00:00";

// Code here
$obj = new Interview();

$this->assertEquals("3 hours ago", $timeDiff);
$timeDiff = $obj->getHumanTimeDiff($time1, $time2);
var_dump($timeDiff);
$this->assertEquals("3 hours ago", $timeDiff);
}

}