Reading out first data

This commit is contained in:
Jeroen De Meerleer 2019-07-15 14:43:23 +02:00
parent 2791a1051f
commit d370aad190
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
1 changed files with 138 additions and 1 deletions

View File

@ -13,21 +13,158 @@
namespace JeroenED\Libpairtwo\Readers;
use DateTime;
use JeroenED\Libpairtwo\Interfaces\ReaderInterface;
use JeroenED\Libpairtwo\Tournament;
/**
* Class Swar4
* @package JeroenED\Libpairtwo\Readers
*/
class Swar4 implements ReaderInterface
{
/** @var Tournament */
private $tournament;
/** @var bool|int|DateTime|string[] */
private $binaryData;
/** @var string */
private $release;
/**
* @param string $filename
* @return ReaderInterface
*/
public function read(string $filename): ReaderInterface
{
// TODO: Implement read() method.
$swshandle = fopen($filename, 'rb');
$this->setRelease($this->readData('String', $swshandle));
$this->setTournament(new Tournament());
fclose($swshandle);
return $this;
}
/**
* @return Tournament
*/
public function getTournament(): Tournament
{
return $this->tournament;
}
/**
* @param Tournament $tournament
*/
public function setTournament(Tournament $tournament): void
{
$this->tournament = $tournament;
}
/**
* @param string $type
* @param $handle
* @return array|bool|false|float|int|string
*/
private function readData(string $type, $handle, $default = null)
{
switch ($type) {
case 'String':
$length = $this->readData('Int', $handle);
$data = fread($handle, $length);
if ($data == '') {
return (is_null($default)) ? '' : $default;
}
return iconv('windows-1252', 'utf-8', $data);
break;
case 'Hex':
case 'Int':
case 'Bool':
case 'Date':
$data = fread($handle, 4);
$hex = implode(unpack("H*", $data));
$hex = array_reverse(str_split($hex, 2));
foreach ($hex as $key => $item) {
if ($item == "00") {
$hex[$key] = "";
} else {
break;
}
}
$hex = implode($hex);
$hex = ($hex == "") ? "00" : $hex;
if ($type == 'Hex') {
if ($hex == '00') {
return (is_null($default)) ? '00' : $default;
}
return $hex;
} elseif ($type == 'Int') {
if ($hex == '00') {
return (is_null($default)) ? 0 : $default;
}
return hexdec($hex);
} elseif ($type == 'Date') {
if ($hex == '00') {
return (is_null($default)) ? $this->convertUIntToTimestamp(0) : $default;
}
return $this->convertUIntToTimestamp(hexdec($hex));
} elseif ($type == 'Bool') {
return ($hex == "01") ? true : false;
}
break;
default:
throw new \InvalidArgumentException("Datatype not known");
break;
}
return false;
}
/**
* @return string
*/
public function getRelease(): string
{
return $this->release;
}
/**
* @param string $release
*/
public function setRelease(string $release): void
{
$this->release = $release;
}
/**
* Returns binary data that was read out the pairtwo file but was not needed immediately
*
* @param string $Key
* @return bool|DateTime|int|string
*/
public function getBinaryData(string $Key)
{
return $this->BinaryData[$Key];
}
/**
* Sets binary data that is read out the pairtwo file but is not needed immediately
*
* @param string $Key
* @param bool|int|DateTime|string $Value
* @return Pairtwo6
*/
public function setBinaryData(string $Key, $Value): Pairtwo6
{
$this->BinaryData[$Key] = $Value;
return $this;
}
}