Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,39 @@

class Query implements \Iterator
{
protected $session;
protected $id;
protected $cache;
protected $pos;
protected Session $session;
protected string $id;
protected ?array $cache = null;
protected int $pos = 0;

/**
* Query constructor.
*
* @param Session $session
* @param string $query
*/
public function __construct($session, $query)
public function __construct(Session $session, string $query)
{
$this->session = $session;
$this->id = $this->exec(chr(0), $query);
}

public function bind($name, $value, $type = "")
public function bind(string $name, string $value, string $type = ""): void
{
$this->exec(chr(3), $this->id.chr(0).$name.chr(0).$value.chr(0).$type);
}

public function context($value, $type = "")
public function context(string $value, string $type = ""): void
{
$this->exec(chr(14), $this->id.chr(0).$value.chr(0).$type);
}

public function execute()
public function execute(): string
{
return $this->exec(chr(5), $this->id);
}

public function more()
public function more(): bool
{
if ($this->cache === null) {
$this->pos = 0;
Expand All @@ -63,29 +63,29 @@ public function more()
return false;
}

public function next()
public function next(): void
{
if ($this->more()) {
return $this->cache[$this->pos++];
$this->pos++;
}
}

public function info()
public function info(): string
{
return $this->exec(chr(6), $this->id);
}

public function options()
public function options(): string
{
return $this->exec(chr(7), $this->id);
}

public function close()
public function close(): void
{
$this->exec(chr(2), $this->id);
}

public function exec($cmd, $arg)
public function exec(string $cmd, string $arg): string
{
$this->session->send($cmd.$arg);
$s = $this->session->receive();
Expand All @@ -95,22 +95,22 @@ public function exec($cmd, $arg)
return $s;
}

public function current()
public function current(): mixed
{
return $this->cache[$this->pos];
}

public function key()
public function key(): mixed
{
return $this->pos;
}

public function valid()
public function valid(): bool
{
return $this->more();
}

public function rewind()
public function rewind(): void
{
}
}
49 changes: 26 additions & 23 deletions src/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
class Session
{
// instance variables.
protected $socket;
protected $info;
protected $buffer;
protected $bpos;
protected $bsize;
protected \Socket|false $socket;
protected ?string $info = null;
protected string $buffer = '';
protected int $bpos = 0;
protected int $bsize = 0;

public function __construct($hostname, $port, $user, $password)
public function __construct(string $hostname, int $port, string $user, string $password)
{
// create server connection
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
Expand Down Expand Up @@ -54,7 +54,7 @@ public function __construct($hostname, $port, $user, $password)
* @param string $command
* @return string
*/
public function execute($command)
public function execute(string $command): string
{
// send command to server
socket_write($this->socket, $command.chr(0));
Expand All @@ -63,7 +63,7 @@ public function execute($command)
$result = $this->receive();
$this->info = $this->readString();
if ($this->ok() != true) {
throw new BaseXException($this->info);
throw new BaseXException($this->info ?? 'Unknown error');
}
return $result;
}
Expand All @@ -74,7 +74,7 @@ public function execute($command)
* @param string $xquery
* @return Query
*/
public function query($xquery)
public function query(string $xquery): Query
{
return new Query($this, $xquery);
}
Expand All @@ -85,7 +85,7 @@ public function query($xquery)
* @param string $name name of the new database
* @param string $input XML to insert
*/
public function create($name, $input)
public function create(string $name, string $input): void
{
$this->sendCmd(8, $name, $input);
}
Expand All @@ -96,7 +96,7 @@ public function create($name, $input)
* @param string $path filesystem-like path
* @param string $input XML to insert
*/
public function add($path, $input)
public function add(string $path, string $input): void
{
$this->sendCmd(9, $path, $input);
}
Expand All @@ -107,12 +107,12 @@ public function add($path, $input)
* @param string $path filesystem-like path
* @param string $input XML to insert
*/
public function replace($path, $input)
public function replace(string $path, string $input): void
{
$this->sendCmd(12, $path, $input);
}

public function store($path, $input)
public function store(string $path, string $input): void
{
$this->sendCmd(13, $path, $input);
}
Expand All @@ -122,21 +122,21 @@ public function store($path, $input)
*
* @return string|null
*/
public function info()
public function info(): ?string
{
return $this->info;
}

/**
* Close the connection.
*/
public function close()
public function close(): void
{
socket_write($this->socket, "exit".chr(0));
socket_close($this->socket);
}

private function init()
private function init(): void
{
$this->bpos = 0;
$this->bsize = 0;
Expand All @@ -146,7 +146,7 @@ private function init()
* @internal
* @return string
*/
public function readString()
public function readString(): string
{
$com = "";
while (($d = $this->read()) != chr(0)) {
Expand All @@ -155,25 +155,28 @@ public function readString()
return $com;
}

private function read()
private function read(): string
{
if ($this->bpos == $this->bsize) {
$this->bsize = socket_recv($this->socket, $this->buffer, 4096, 0);
if ($this->bsize === false) {
throw new BaseXException("Socket read error: " . socket_last_error($this->socket));
}
$this->bpos = 0;
}
return $this->buffer[$this->bpos++];
}

private function sendCmd($code, $arg, $input)
private function sendCmd(int $code, string $arg, string $input): void
{
socket_write($this->socket, chr($code).$arg.chr(0).$input.chr(0));
$this->info = $this->receive();
if ($this->ok() != true) {
throw new BaseXException($this->info);
throw new BaseXException($this->info ?? 'Unknown error');
}
}

public function send($str)
public function send(string $str): void
{
socket_write($this->socket, $str.chr(0));
}
Expand All @@ -184,7 +187,7 @@ public function send($str)
* @internal not idempotent, not intended for use by client code
* @return bool
*/
public function ok()
public function ok(): bool
{
return $this->read() == chr(0);
}
Expand All @@ -193,7 +196,7 @@ public function ok()
* @internal
* @return string
*/
public function receive()
public function receive(): string
{
$this->init();
return $this->readString();
Expand Down