Skip to content

Commit

Permalink
Merge pull request #1 from leandrocopam/develop
Browse files Browse the repository at this point in the history
Implementação da codificação de caractere, default utf-8
  • Loading branch information
DRL authored Mar 6, 2018
2 parents 8dcee2c + 816beba commit 6c02a89
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/PHPJasper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

namespace PHPJasper;

/**
* Class PHPJasper
* @package PHPJasper
*/
class PHPJasper
{

Expand Down Expand Up @@ -35,17 +31,28 @@ class PHPJasper
* @var array
*/
protected $formats = ['pdf', 'rtf', 'xls', 'xlsx', 'docx', 'odt', 'ods', 'pptx', 'csv', 'html', 'xhtml', 'xml', 'jrprint'];

protected $lang;

/**
* PHPJasper constructor
*/
public function __construct()
public function __construct($lang = "pt_BR.UTF-8")
{
$this->lang = $lang;
$this->executable = 'jasperstarter';
$this->pathExecutable = __DIR__ . '/../bin/jasperstarter/bin';
$this->windows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? true : false;
}

/**
* @return string
*/
private function checkServer()
{
return $this->command = ($this->windows) ? $this->executable : 'LANG=' . $this->lang . ' ./' . $this->executable;
}

/**
* @param string $input
* @param string $output optional
Expand All @@ -54,13 +61,13 @@ public function __construct()
*/
public function compile(string $input, string $output = '')
{
if (!$input) {
if (!is_file($input)) {
throw new \PHPJasper\Exception\InvalidInputFile();
}

$this->command = $this->windows ? $this->executable : './' . $this->executable;
$this->command = $this->checkServer();
$this->command .= ' compile ';
$this->command .= "\"$input\"";
$this->command .= '"' . realpath($input) . '"';

if (!empty($output)) {
$this->command .= ' -o ' . "\"$output\"";
Expand All @@ -69,7 +76,6 @@ public function compile(string $input, string $output = '')
return $this;
}


/**
* @param string $input
* @param string $output
Expand All @@ -81,12 +87,15 @@ public function compile(string $input, string $output = '')
public function process(string $input, string $output, array $options = [])
{
$options = $this->parseProcessOptions($options);

if (!$input) {
throw new \PHPJasper\Exception\InvalidInputFile();
}

$this->validateFormat($options['format']);

$this->command = $this->windows ? $this->executable : './' . $this->executable;
$this->command = $this->checkServer();

if ($options['locale']) {
$this->command .= " --locale {$options['locale']}";
}
Expand Down Expand Up @@ -127,13 +136,14 @@ public function process(string $input, string $output, array $options = [])
if ($options['resources']) {
$this->command .= " -r {$options['resources']}";
}

$this->command = $this->command . ' 2>&1';
}

return $this;
}

/**
*
* @param array $options
* @return array
*/
Expand Down Expand Up @@ -173,13 +183,13 @@ protected function validateFormat($format)
*/
public function listParameters(string $input)
{
if (!$input) {
if (!is_file($input)) {
throw new \PHPJasper\Exception\InvalidInputFile();
}

$this->command = $this->windows ? $this->executable : './' . $this->executable;
$this->command = $this->checkServer();
$this->command .= ' list_parameters ';
$this->command .= "\"$input\"";
$this->command .= '"'.realpath($input).'"';

return $this;
}
Expand All @@ -202,7 +212,8 @@ public function execute($user = false)
chdir($this->pathExecutable);
exec($this->command, $output, $returnVar);
if ($returnVar !== 0) {
throw new \PHPJasper\Exception\ErrorCommandExecutable();
//throw new \PHPJasper\Exception\ErrorCommandExecutable();
throw new \Exception("{$output[0]}", 1);
}

return $output;
Expand Down Expand Up @@ -238,7 +249,5 @@ protected function validateExecute()
if (!is_dir($this->pathExecutable)) {
throw new \PHPJasper\Exception\InvalidResourceDirectory();
}

}

}
}

0 comments on commit 6c02a89

Please sign in to comment.