array('Host' => $host)); } function order($hosts) { $detcameras = array_map('camera', $hosts); eval($GLOBALS['sort'][0]); return array_map(function($c) { return $c['monitor']['Host']; }, $detcameras); } function ep($ip) { return 'http://'.$ip.':80/onvif/device_service'; } $failures = 0; function check($name, $got, $want) { global $failures; if ($got === $want) { echo "ok - $name\n"; } else { echo "FAIL - $name\n got ".json_encode($got)."\n want ".json_encode($want)."\n"; $failures++; } } $ips = array('192.168.1.100', '192.168.1.9', '192.168.1.20', '10.0.0.5', '192.168.1.3'); check('addresses are ordered by their numbers', order(array_map('ep', $ips)), array_map('ep', array('10.0.0.5', '192.168.1.3', '192.168.1.9', '192.168.1.20', '192.168.1.100'))); check('host names ignore case', order(array('http://Cam2.local/x', 'http://cam1.local/x', 'http://CAM10.local/x')), array('http://cam1.local/x', 'http://Cam2.local/x', 'http://CAM10.local/x')); check('a host that cannot be read sorts first and the list keeps its other entries', order(array('http://10.0.0.2/x', 'not a url ::', 'http://10.0.0.1/x')), array('not a url ::', 'http://10.0.0.1/x', 'http://10.0.0.2/x')); check('no cameras', order(array()), array()); exit($failures ? 1 : 0);