@@ -45,7 +45,7 @@ $validator->addFieldConstraint(0, new Email()); | |||
$validator->addFieldConstraint(1, new Date()); | |||
// Validate the legend | |||
$validator->setExpectedLegend(array('foo', 'bar', 'bim')); | |||
$validator->setExpectedHeaders(['foo', 'bar', 'bim']); | |||
// An line must contain 3 columns | |||
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) { | |||
@@ -55,10 +55,10 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter | |||
})); | |||
// Initialisation of the parser | |||
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv'); | |||
$parser->setHasLegend(true); | |||
$parser = new CsvParser(); | |||
$parser->setHasHeaders(true); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv')); | |||
if ($validator->isValid() === false) { | |||
foreach ($validator->getErrors() as $error) { | |||
@@ -80,6 +80,39 @@ EOF; | |||
} | |||
``` | |||
Run `example.php` and see results: | |||
``` | |||
<ul> | |||
<li>Line: 1</li> | |||
<li>Column: </li> | |||
<li> | |||
<p>Invalid legend.</p> | |||
</li> | |||
</ul> | |||
<ul> | |||
<li>Line: 4</li> | |||
<li>Column: </li> | |||
<li> | |||
<p>The line must contain 3 columns</p> | |||
</li> | |||
</ul> | |||
<ul> | |||
<li>Line: 2</li> | |||
<li>Column: 1</li> | |||
<li> | |||
<p>This value is not a valid email address.</p> | |||
</li> | |||
</ul> | |||
<ul> | |||
<li>Line: 3</li> | |||
<li>Column: 2</li> | |||
<li> | |||
<p>This value is not a valid date.</p> | |||
</li> | |||
</ul> | |||
``` | |||
Contributors | |||
------------ | |||
@@ -17,6 +17,6 @@ | |||
"require": { | |||
"php": ">=5.6.0", | |||
"symfony/validator": "2.*", | |||
"deblan/csv": "v1.1" | |||
"deblan/csv": "v2.0.*" | |||
} | |||
} |
@@ -20,7 +20,7 @@ $validator->addFieldConstraint(0, new Email()); | |||
$validator->addFieldConstraint(1, new Date()); | |||
// Validate the legend | |||
$validator->setExpectedLegend(array('foo', 'bar', 'bim')); | |||
$validator->setExpectedHeaders(['foo', 'bar', 'bim']); | |||
// An line must contain 3 columns | |||
$validator->addDataConstraint(new Callback(function($data, ExecutionContextInterface $context) { | |||
@@ -30,17 +30,17 @@ $validator->addDataConstraint(new Callback(function($data, ExecutionContextInter | |||
})); | |||
// Initialisation of the parser | |||
$parser = new CsvParser(__DIR__.'/tests/fixtures/example.csv'); | |||
$parser->setHasLegend(true); | |||
$parser = new CsvParser(); | |||
$parser->setHasHeaders(true); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/tests/fixtures/example.csv')); | |||
if ($validator->isValid() === false) { | |||
foreach ($validator->getErrors() as $error) { | |||
$line = $error->getLine(); | |||
$line = $error->getLine(); | |||
$column = $error->getColumn(); | |||
$message = $error->getViolation()->getMessage(); | |||
echo <<<EOF | |||
<ul> | |||
<li>Line: $line</li> |
@@ -49,7 +49,7 @@ class Validator | |||
/** | |||
* @var array | |||
*/ | |||
protected $expectedLegend = []; | |||
protected $expectedHeaders = []; | |||
/** | |||
* Constructor. | |||
@@ -105,9 +105,9 @@ class Validator | |||
* | |||
* @return Validator | |||
*/ | |||
public function setExpectedLegend(array $legend) | |||
public function setExpectedHeaders(array $legend) | |||
{ | |||
$this->expectedLegend = $legend; | |||
$this->expectedHeaders = $legend; | |||
return $this; | |||
} | |||
@@ -121,13 +121,12 @@ class Validator | |||
{ | |||
if ($this->parser !== $parser) { | |||
$this->parser = $parser; | |||
$this->parser->parse(); | |||
$this->errors = []; | |||
} elseif ($this->hasValidate) { | |||
return; | |||
} | |||
$this->validateLegend(); | |||
$this->validateHeaders(); | |||
$this->validateDatas(); | |||
$this->validateFields(); | |||
@@ -137,17 +136,17 @@ class Validator | |||
/** | |||
* Validates the legend. | |||
*/ | |||
protected function validateLegend() | |||
protected function validateHeaders() | |||
{ | |||
if (!$this->parser->getHasLegend()) { | |||
if (!$this->parser->getHasHeaders()) { | |||
return; | |||
} | |||
if (empty($this->expectedLegend)) { | |||
if (empty($this->expectedHeaders)) { | |||
return; | |||
} | |||
if ($this->parser->getLegend() !== $this->expectedLegend) { | |||
if ($this->parser->getHeaders() !== $this->expectedHeaders) { | |||
$this->mergeErrorMessage('Invalid legend.', 1); | |||
} | |||
} | |||
@@ -294,7 +293,7 @@ class Validator | |||
*/ | |||
protected function getTrueLine($line) | |||
{ | |||
if ($this->parser->getHasLegend()) { | |||
if ($this->parser->getHasHeaders()) { | |||
++$line; | |||
} | |||
@@ -0,0 +1,44 @@ | |||
<?php | |||
/** | |||
* class ExampleTest. | |||
* | |||
* @author Simon Vieille <simon@deblan.fr> | |||
*/ | |||
class ExampleTest extends \PHPUnit_Framework_TestCase | |||
{ | |||
public function testExemple() | |||
{ | |||
$content = shell_exec('php -f '.__DIR__.'/../example.php'); | |||
$this->assertEquals('<ul> | |||
<li>Line: 1</li> | |||
<li>Column: </li> | |||
<li> | |||
<p>Invalid legend.</p> | |||
</li> | |||
</ul> | |||
<ul> | |||
<li>Line: 4</li> | |||
<li>Column: </li> | |||
<li> | |||
<p>The line must contain 3 columns</p> | |||
</li> | |||
</ul> | |||
<ul> | |||
<li>Line: 2</li> | |||
<li>Column: 1</li> | |||
<li> | |||
<p>This value is not a valid email address.</p> | |||
</li> | |||
</ul> | |||
<ul> | |||
<li>Line: 3</li> | |||
<li>Column: 2</li> | |||
<li> | |||
<p>This value is not a valid date.</p> | |||
</li> | |||
</ul> | |||
', $content); | |||
} | |||
} |
@@ -16,20 +16,20 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase | |||
$validator->isValid(); | |||
} | |||
public function testExpectedLegend() | |||
public function testExpectedHeaders() | |||
{ | |||
$parser = $this->generateParser('example.csv'); | |||
$parser->setHasLegend(true); | |||
$parser->setHasHeaders(true); | |||
$validator = $this->generateValidator(); | |||
$validator->setExpectedLegend(['foo', 'bar', 'boo']); | |||
$validator->validate($parser); | |||
$validator->setExpectedHeaders(['foo', 'bar', 'boo']); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(true, $validator->isValid()); | |||
$this->assertEquals(0, count($validator->getErrors())); | |||
$validator = $this->generateValidator(); | |||
$validator->setExpectedLegend(['bad', 'legend']); | |||
$validator->validate($parser); | |||
$validator->setExpectedHeaders(['bad', 'legend']); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(false, $validator->isValid()); | |||
$this->assertEquals(1, count($validator->getErrors())); | |||
} | |||
@@ -38,7 +38,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase | |||
{ | |||
$parser = $this->generateParser('example.csv'); | |||
$validator = $this->generateValidator(); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(true, $validator->isValid()); | |||
} | |||
@@ -47,14 +47,14 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase | |||
$parser = $this->generateParser('example.csv'); | |||
$validator = $this->generateValidator(); | |||
$validator->addFieldConstraint(0, new NotBlank()); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(true, $validator->isValid()); | |||
$this->assertEquals(0, count($validator->getErrors())); | |||
$parser = $this->generateParser('example.csv'); | |||
$validator = $this->generateValidator(); | |||
$validator->addFieldConstraint(1, new NotBlank()); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(true, $validator->isValid()); | |||
$this->assertEquals(0, count($validator->getErrors())); | |||
@@ -62,24 +62,24 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase | |||
$validator = $this->generateValidator(); | |||
$validator->addFieldConstraint(0, new NotBlank()); | |||
$validator->addFieldConstraint(1, new NotBlank()); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(true, $validator->isValid()); | |||
$this->assertEquals(0, count($validator->getErrors())); | |||
} | |||
public function testFieldContraintsKo() | |||
{ | |||
$parser = $this->generateParser('example.csv'); | |||
$parser = $this->generateParser(); | |||
$validator = $this->generateValidator(); | |||
$validator->addFieldConstraint(0, new Email()); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(false, $validator->isValid()); | |||
$this->assertEquals(2, count($validator->getErrors())); | |||
$parser = $this->generateParser('example.csv'); | |||
$parser = $this->generateParser(); | |||
$validator = $this->generateValidator(); | |||
$validator->addFieldConstraint(1, new Email()); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(false, $validator->isValid()); | |||
$this->assertEquals(5, count($validator->getErrors())); | |||
@@ -87,14 +87,14 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase | |||
$validator = $this->generateValidator(); | |||
$validator->addFieldConstraint(0, new Email()); | |||
$validator->addFieldConstraint(1, new Email()); | |||
$validator->validate($parser); | |||
$validator->validate($parser->parseFile(__DIR__.'/fixtures/example.csv')); | |||
$this->assertEquals(false, $validator->isValid()); | |||
$this->assertEquals(7, count($validator->getErrors())); | |||
} | |||
protected function generateParser($file) | |||
protected function generateParser() | |||
{ | |||
return new CsvParser(__DIR__.'/fixtures/'.$file); | |||
return new CsvParser(); | |||
} | |||
protected function generateValidator() |