public function testParse() { $rp = new RowParams(); $rp->addColumn('c1', new Column(0, 2, new FInt()))->addColumn('c2', new Column(2, 3, new FString())); $content = '23abc iii'; $rv = new RowValues($rp, $content); $this->assertEquals($content, $rv->getContent()); $expected = array('c1' => 23, 'c2' => 'abc'); $this->assertEquals($expected, $rv->parse()); }
/** * Obtem o conteudo do arquivo em uma array de array mapeado com os valores convertidos. * * @param string $file_name * * @return array * * @throws \Exception */ public function exec($file_name) { try { $this->file = new File($file_name); $this->validate(); $result = array(); $this->file->load(); foreach ($this->file->getRows() as $i => $v) { $ln = 1 + $i; $key = $this->getRowsKeyLength() ? substr($v, 0, $this->getRowsKeyLength()) : ''; $rv = new RowValues($this->getParams($key), $v); $result[] = $rv->parse(); } } catch (\DomainException $e) { throw new \DomainException($e->getMessage() . " linha: {$ln}"); } catch (\Exception $e) { throw new \Exception($e->getMessage() . " linha: {$ln}"); } return $result; }
require_once "vendor/autoload.php"; // exempo de uso da maneira manual. $result = array(); $rp = new RowParams(); $rp->addColumn('type', new Column(0, 1, new FString(true))); $rp->addColumn('id', new Column(1, 2, new FInt(true))); $rp->addColumn('c1', new Column(2, 3, new FDouble())); $rp->addColumn('c2', new Column(5, 10, new FNumberString())); $rp->addColumn('c3', new Column(15, 5, new FString())); $rp->addColumn('c4', new Column(20, 10, new FEmail())); $rp->addColumn('c5', new Column(30, 19, new FDateTime())); $file_name = 'teste.txt'; $f = new File($file_name); $f->load(); foreach ($f->getRows() as $v) { $rv = new RowValues($rp, $v); $result[] = $rv->parse(); } //var_export($result); echo PHP_EOL; echo PHP_EOL; // exempo de uso da maneira facil (recomendado). $ff = FileFacade::create(1)->addColumn('C', 'type', 0, 1, Types::STRING, true)->addColumn('C', 'id', 1, 2, Types::INT, true)->addColumn('C', 'c1', 2, 3, Types::DOUBLE)->addColumn('C', 'c2', 5, 10, Types::NUMBER_STRING)->addColumn('C', 'c3', 15, 5, Types::STRING)->addColumn('C', 'c4', 20, 10, Types::EMAIL)->addColumn('C', 'c5', 30, 19, Types::DATE_TIME, array('format' => 'Y-m-d H:i:s', 'required' => true))->addColumn('D', 'type', 0, 1, Types::STRING, true)->addColumn('D', 'id', 1, 2, Types::INT, true)->addColumn('D', 'c1', 2, 3, Types::DOUBLE)->addColumn('D', 'c2', 5, 10, Types::NUMBER_STRING)->addColumn('D', 'c3', 15, 5, Types::STRING)->addColumn('D', 'c4', 20, 10, Types::EMAIL)->addColumn('D', 'c5', 30, 19, Types::DATE_TIME, array('format' => 'Y-m-d H:i:s', 'required' => true))->addColumn('', 'type', 0, 1, Types::STRING, true)->addColumn('', 'id', 1, 2, Types::INT, true)->addColumn('', 'c1', 2, 3, Types::DOUBLE)->addColumn('', 'c2', 5, 10, Types::NUMBER_STRING)->addColumn('', 'c3', 15, 5, Types::STRING)->addColumn('', 'c4', 20, 10, Types::EMAIL)->addColumn('', 'c5', 30, 19, Types::DATE_TIME, array('format' => 'Y-m-d H:i:s', 'required' => true)); //$result = $ff->exec($file_name); //var_export($result); /* $tipOpe = substr($reg, 1, 1); $tipins = (int)(trim(substr($reg, 2, 2))); $insmun = (int)(substr($reg, 4, 15)); $cgcm_id = (int)(substr($reg, 19, 10)); $nomraz = utf8_encode(strtoupper(strtr(trim(substr($reg, 29, 60)), "'", "`")));