1- <?php
2-
1+ <?php declare (strict_types=1 );
32/**
43 * This file is part of escpos-php: PHP receipt printer library for use with
54 * ESC/POS-compatible thermal and impact printers.
@@ -24,17 +23,19 @@ class NetworkPrintConnector extends FilePrintConnector
2423 * Construct a new NetworkPrintConnector
2524 *
2625 * @param string $ip IP address or hostname to use.
27- * @param string $port The port number to connect on.
28- * @param string $timeout The connection timeout, in seconds.
26+ * @param int $port The port number to connect on.
27+ * @param int $timeout The connection timeout, in seconds.
2928 * @throws Exception Where the socket cannot be opened.
3029 */
31- public function __construct (string $ ip , int $ port = 9100 , bool $ timeout = false )
30+ public function __construct (string $ ip , int $ port = 9100 , int $ timeout = - 1 )
3231 {
33- // Default to 60 if default_socket_timeout isn't defined in the ini
34- $ defaultSocketTimeout = ini_get ("default_socket_timeout " ) ?: 60 ;
35- $ timeout = $ timeout ?: $ defaultSocketTimeout ;
36-
37- $ this -> fp = @fsockopen ($ ip , $ port , $ errno , $ errstr , $ timeout );
32+ // Note: Once the minimum PHP version is PHP 7.0 or higher, we can type $timeout as '?int' to make it optional
33+ // instead of using -1.
34+ if ($ timeout == -1 ) {
35+ $ this -> fp = @fsockopen ($ ip , $ port , $ errno , $ errstr );
36+ } else {
37+ $ this -> fp = @fsockopen ($ ip , $ port , $ errno , $ errstr , (float )$ timeout );
38+ }
3839 if ($ this -> fp === false ) {
3940 throw new Exception ("Cannot initialise NetworkPrintConnector: " . $ errstr );
4041 }
0 commit comments