Skip to content

Commit

Permalink
格式化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
itbdw committed Nov 30, 2020
1 parent 9b641e4 commit 2c94cb9
Show file tree
Hide file tree
Showing 4 changed files with 35,918 additions and 8 deletions.
22 changes: 22 additions & 0 deletions bin/update-region-code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php
/**
* Created by PhpStorm.
* User: zhao.binyan
* Date: 2019/7/25
* Time: 下午2:24
*/


/**
* 示例
* `php ~/bin/update-region-code.php`
*
*/

date_default_timezone_set("PRC");

require dirname(__DIR__) . '/src/IpLocation.php';

$region_code_html = \itbdw\Ip\IpLocation::root('/tmp/mca_region_code.html');

// todo 解析 html 变成格式化的数据
56 changes: 54 additions & 2 deletions src/IpLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@
use itbdw\Ip\IpParser\QQwry;
use itbdw\Ip\IpParser\IpV6wry;

/**
*
*/
define("IP_DATABASE_ROOT_DIR", dirname(__DIR__));

/**
* Class IpLocation
* @package itbdw\Ip
*/
class IpLocation {
/**
* @var
*/
private static $ipV4Path;
/**
* @var
*/
private static $ipV6Path;

/**
Expand Down Expand Up @@ -60,28 +75,65 @@ public static function getLocation($ip, $ipV4Path='', $ipV6Path='') {
return StringParser::parse($location);
}

/**
* @param $path
*/
public static function setIpV4Path($path)
{
self::$ipV4Path = $path;
}

/**
* @param $path
*/
public static function setIpV6Path($path)
{
self::$ipV6Path = $path;
}

/**
* @return string
*/
private static function getIpV4Path() {
return self::$ipV4Path ? : __DIR__ . '/libs/qqwry.dat';
return self::$ipV4Path ? : self::src('/libs/qqwry.dat');
}

/**
* @return string
*/
private static function getIpV6Path() {
return self::$ipV6Path ? : __DIR__ . '/libs/ipv6wry.db';
return self::$ipV6Path ? : self::src('/libs/ipv6wry.db');
}

/**
* @param $ip
* @return bool
*/
private static function isIpV4($ip) {
return false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}

/**
* @param $ip
* @return bool
*/
private static function isIpV6($ip) {
return false !== filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
}

/**
* @param $filename
* @return string
*/
public static function src($filename) {
return self::root('/src'.$filename);
}

/**
* @param $filename
* @return string
*/
public static function root($filename) {
return IP_DATABASE_ROOT_DIR . $filename;
}
}
15 changes: 9 additions & 6 deletions tests/ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
*/

//you do not need to do this if use composer!
require dirname(__DIR__) . '/src/ipparser/IpParserInterface.php';

require dirname(__DIR__) . '/src/IpLocation.php';
require dirname(__DIR__) . '/src/ipparser/QQwry.php';
require dirname(__DIR__) . '/src/ipparser/IpV6wry.php';
require dirname(__DIR__) . '/src/StringParser.php';
//require dirname(__DIR__) . '/src/ipparser/IpParserInterface.php';
//
//require dirname(__DIR__) . '/src/IpLocation.php';
//require dirname(__DIR__) . '/src/ipparser/QQwry.php';
//require dirname(__DIR__) . '/src/ipparser/IpV6wry.php';
//require dirname(__DIR__) . '/src/StringParser.php';

//需要 composer install 或者去掉上面注释,并注释这一行
include dirname(__DIR__) .'/vendor/autoload.php';

$input = getopt("i:", ['ip:']);

Expand Down
Loading

0 comments on commit 2c94cb9

Please sign in to comment.