Skip to content

Commit

Permalink
Added utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
natenolting committed May 31, 2017
1 parent a81de3c commit d65ac6c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Bandolier/Type/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public static function contains($haystack, $needle, $caseSensitive = true)
return true;
}
}

return false;
}

Expand Down
37 changes: 37 additions & 0 deletions src/Bandolier/Type/Utility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Utility
*
* Created 5/31/17 4:33 PM
* Utility class
*
* @author Nate Nolting <naten@paulbunyan.net>
* @package Pbc\Bandolier\Type
*/

namespace Pbc\Bandolier\Type;


class Utility
{

/**
* Grab IP from host
* http://www.php.net/manual/en/function.gethostbyname.php#78965
* @param $address
* @return mixed
*/
public static function lookUpHostIp($address)
{

$ipAddress = null;
$records = dns_get_record(trim($address));
foreach ($records as $record) {
if ($record['type'] == 'A') {
$ipAddress = $record['ip'];
}
}
return $ipAddress;
}
}

11 changes: 6 additions & 5 deletions src/Bandolier/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ function env($key, $default = null)
if ($value === false) {
return value($default);
}

$conditions = [
['return' => true, 'checks' => 'true', '(true)', '"true"'],
['return' => false, 'checks' => 'false', '(false)', '"false"'],
['return' => null, 'checks' => 'null', '(null)', '"null"'],
['return' => '', 'checks' => 'empty', '(empty)', '"empty"'],
['return' => true, 'checks' => ['true', '(true)', '"true"']],
['return' => false, 'checks' => ['false', '(false)', '"false"']],
['return' => null, 'checks' => ['null', '(null)', '"null"']],
['return' => '', 'checks' => ['empty', '(empty)', '"empty"']],
];
for ($i=0, $iCount=count($conditions); $i < $iCount; $i++) {
if (Strings::contains($value, $conditions[$i]['checks'], false)) {
if (in_array($value, $conditions[$i]['checks'])) {
return $conditions[$i]['return'];
}
}
Expand Down
63 changes: 63 additions & 0 deletions tests/Type/Utility/LookUpHostIpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Pbc\Bandolier\Type;

/**
* UtilityTest
*
* Created 5/31/17 4:42 PM
* Tests for the utility class
*
* @author Nate Nolting <naten@paulbunyan.net>
* @package ${NAMESPACE}
*/


/**
* Class UtilityTest
* @package Pbc\Bandolier\Type
*/
class LookUpHostIpTest extends \PHPUnit_Framework_TestCase
{

/** @var \Faker\Factory */
protected static $faker;

/**
*
*/
protected function setUp()
{
parent::setUp();
self::$faker = \Faker\Factory::create();
}

/**
*
*/
protected function tearDown()
{
parent::tearDown();
}

/**
* Test getting an ip from a domain name
*/
public function testLookUpHostIpReturnsAnIpAddress()
{
$address = "paulbunyan.net";
$lookUp = Utility::lookUpHostIp($address);
$this->assertNotFalse(filter_var($lookUp, FILTER_VALIDATE_IP), "$lookUp is an ip address");
}

/**
* check that a fake domain name will not return an ip
*/
public function testLookUpHostIpReturnsNullIfNoneFound()
{
$address = self::$faker->sha256.".com";
$lookUp = Utility::lookUpHostIp($address);
$this->assertNull($lookUp);

}
}

0 comments on commit d65ac6c

Please sign in to comment.