PHP code example of gpslab / interval
1. Go to this page and download the library: Download gpslab/interval library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gpslab / interval example snippets
$interval = NumberInterval::halfClosed(1, 5);
echo $interval->start(); // 1
echo $interval->end(); // 5
// convert to string
$string = (string)$interval;
echo $string; // [1, 5)
// restore from string
$new_interval = NumberInterval::fromString($string);
$interval == $new_interval; // true
$interval->equal($new_interval); // true
$interval = NumberInterval::halfClosed(1, 5);
// created a new interval instance
$new_interval = $interval->withStart(new NumberIntervalPoint(2));
$interval->start() != $new_interval->start(); // true
// from CIDR
$network = IPv4Network::fromCIDR('192.168.0.0', 16);
echo $network->start(); // 192.168.0.0
echo $network->end(); // 192.168.255.255
$network->contains('192.168.13.74'); // true
// from ip mask
$new_network = IPv4Network::fromMask('192.168.0.0', '255.255.0.0');
$network->equal($new_network); // true
function isLocalNetworkHost($host)
{
// is IPv6
if (strpos($host, ':') !== false) {
// RFC 4193
return IPv6Network::fromString('fc00::/7')->contains($host);
}
// RFC 1918
return
IPv4Network::fromString('10/8')->contains($host) ||
IPv4Network::fromString('172.16/12')->contains($host) ||
IPv4Network::fromString('192.168/16')->contains($host)
;
}
$interval = NumberInterval::closed(1, 5);
$points_in_interval = [];
foreach ($interval->iterate() as $point) {
$points_in_interval[] = $point;
}
$points_in_interval == [1, 2, 3, 4, 5]; // true
$step = 2;
$interval = NumberInterval::open(0, 10);
$points_in_interval = [];
foreach ($interval->iterate($step) as $point) {
$points_in_interval[] = $point;
}
$points_in_interval == [2, 4, 6, 8]; // true
$expected = [
'10.0.1.2',
'10.0.1.4',
'10.0.1.6',
'10.0.1.8'
];
$step = 2;
$interval = IPv4Interval::open('10.0.1.0', '10.0.1.10');
$points_in_interval = [];
foreach ($interval->iterate($step) as $point) {
$points_in_interval[] = $point;
}
$points_in_interval == $expected; // true
$expected = [
'2017-03-03',
'2017-03-05',
'2017-03-07',
'2017-03-09',
];
$step = new \DateInterval('P2D');
$interval = DateInterval::open(new \DateTime('2017-03-01'), new \DateTime('2017-03-11'));
$points_in_interval = [];
foreach ($interval->iterate($step) as $point) {
$points_in_interval[] = $point->format('Y-m-d');
}
$points_in_interval == $expected; // true
Type::addType('NumberInterval', 'GpsLab\Component\Interval\Persistence\Doctrine\DBAL\Types\NumberIntervalType');
$em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('NumberInterval', 'NumberInterval');