@ 2015-10-07T05:25:46Z <?php namespace FlatFileParser;
$definitions = [
['name' => 'Serial Number', 'length' => 16, 'type' => 'int'],
['name' => 'Language', 'length' => 3, 'type' => 'string'],
['name' => 'Business Name', 'length' => 32, 'type' => 'string'],
['name' => 'Business Code', 'length' => 8, 'type' => 'string'],
['name' => 'Authorization Code', 'length' => 8, 'type' => 'string'],
['name' => 'Timestamp', 'length' => 20, 'type' => 'timestamp'],
];
$writer = new Writer();
$validator = new Validator();
$parser = new Parser();
if(count($argv) != 2){
$writer->write("ERROR: No flat file provided");
}
try{
$data = new SplFileObject($argv[1]);
if ($data->isFile() && $data->isReadable()) {
$lineNumber = 1;
while (!$file->eof()) {
$line = $file->fgets();
if($validator->isValidLineLength($line)){
$business = convertLineToBusiness($line);
$writer->writeBusiness($business);
}
else{
$writer->write("Line Number: " + $lineNumber + "ERROR: Line to Long");
}
$lineNumber ++;
}
}
else{
$writer->write("ERROR: Flat file not readable");
}
}
catch(Exception $e){
echo "err";
}
class Writer{
public function write($text){
echo $text . "\n";
}
public function writeBusiness($lineNumber, $business){
write("Line Number: " + $lineNumber);
write("Serial Number: " + $business->serialNumber);
write("Language: " + $business->language);
write("Business Name: " + $business->businessName);
write("Business Code: " + $business->businessCode);
write("Authorization Code: " + $business->get(authorizationCode));
write("Timestamp: " + $business->timestamp);
}
}
class Validator{
private $validLineLength;
public function isValidLineLength($line){
return strlen($line) < $this->validLineLength;
}
function Validator(){
$this->validLineLength = 0;
foreach($definitions as $definition){
$this->validLineLength += $definition->length;
}
}
}
class Parser{
public function convertLineToBusiness($line){
$start = 0;
$parms = [];
foreach($definitions as $definition){
array_push($parms, substr($line, $start, $definition->length));
$start += $definition->length;
}
return new Business($parms);
}
}
class Business{
public $serialNumber;
public $language;
public $businessName;
public $businessCode;
private $authorizationCode;
public $timestamp;
/**
* Getter for some private attributes
* @return mixed $attribute
*/
public function __get($attribute){
if (isset($this->{"_".$attribute})){
return $this->{"_".$attribute};
}
else{
return false;
}
}
public function Business($parms){
$this->serialNumber = $parms[1];
$this->language = $parms[2];
$this->businessName = $parms[3];
$this->businessCode = $parms[4];
$this->authorizationCode = $parms[5];
$this->timestamp = $parms[6];
}
}
?>
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.7 ERROR: No flat file provided
Fatal error: Uncaught Error: Class "FlatFileParser\SplFileObject" not found in /in/HGChT:19
Stack trace:
#0 {main}
thrown in /in/HGChT on line 19
Process exited with code 255 . Output for 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 ERROR: No flat file provided
Fatal error: Uncaught Error: Class 'FlatFileParser\SplFileObject' not found in /in/HGChT:19
Stack trace:
#0 {main}
thrown in /in/HGChT on line 19
Process exited with code 255 . Output for 5.4.0 - 5.4.45 , 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 ERROR: No flat file provided
Fatal error: Class 'FlatFileParser\SplFileObject' not found in /in/HGChT on line 19
Process exited with code 255 . preferences:dark mode live preview
278.5 ms | 402 KiB | 377 Q