-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Stanojevic
committed
Jan 10, 2020
1 parent
c942e9c
commit 509931a
Showing
6 changed files
with
106 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,59 @@ | ||
<?php | ||
|
||
namespace Ivanstan\Tle; | ||
namespace Ivanstan\Tle\Service; | ||
|
||
use Ivanstan\Tle\Exception\InvalidTleException; | ||
use Ivanstan\Tle\Model\Tle; | ||
|
||
class Validator | ||
{ | ||
private Tle $tle; | ||
private string $line1 = ''; | ||
private string $line2 = ''; | ||
|
||
/** | ||
* @param Tle $tle | ||
* | ||
* @return bool | ||
* @throws InvalidTleException | ||
*/ | ||
public function validate(Tle $tle): bool | ||
{ | ||
// check line number | ||
// check checksum matches calculated checksum | ||
$this->tle = $tle; | ||
$this->line1 = $tle->getLine1(); | ||
$this->line2 = $tle->getLine2(); | ||
|
||
$this->validLineNumber(); | ||
$this->validChecksum(); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @throws InvalidTleException | ||
*/ | ||
private function validLineNumber(): void | ||
{ | ||
if ($this->line1[0] !== (string)Tle::LINE1) { | ||
throw new InvalidTleException(\sprintf('Line 1 number expected to be %s but %s found', Tle::LINE1, $this->line1[0])); | ||
} | ||
|
||
if ($this->line2[0] !== (string)Tle::LINE2) { | ||
throw new InvalidTleException(\sprintf('Line 2 number expected to be %s but %s found', Tle::LINE2, $this->line2[0])); | ||
} | ||
} | ||
|
||
/** | ||
* @throws InvalidTleException | ||
*/ | ||
private function validChecksum(): void | ||
{ | ||
if ($this->tle->getChecksum(Tle::LINE1) !== ChecksumCalculator::calculate($this->tle->getLine1())) { | ||
throw new InvalidTleException('Calculated line 1 checksum does not match actual checksum'); | ||
} | ||
|
||
if ($this->tle->getChecksum(Tle::LINE2) !== ChecksumCalculator::calculate($this->tle->getLine2())) { | ||
throw new InvalidTleException('Calculated line 2 checksum does not match actual checksum'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
use Ivanstan\Tle\Helper\SampleTleHelper; | ||
use Ivanstan\Tle\Model\Tle; | ||
use Ivanstan\Tle\Service\Validator; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ValidatorTest extends TestCase | ||
{ | ||
private Validator $validator; | ||
private Tle $tle; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->tle = SampleTleHelper::create(); | ||
$this->validator = new Validator(); | ||
} | ||
|
||
public function testValidateLineNumber(): void | ||
{ | ||
$tle = SampleTleHelper::create(); | ||
$line = SampleTleHelper::$line1; | ||
$line[0] = '0'; | ||
$tle->setLine1($line); | ||
|
||
$this->expectExceptionMessage('Line 1 number expected to be 1 but 0 found'); | ||
$this->validator->validate($tle); | ||
|
||
$tle = SampleTleHelper::create(); | ||
$line = SampleTleHelper::$line2; | ||
$line[0] = '0'; | ||
$tle->setLine2($line); | ||
$this->expectExceptionMessage('Line 2 number expected to be 2 but 0 found'); | ||
$this->validator->validate($tle); | ||
} | ||
|
||
public function testValidateLineChecksum(): void | ||
{ | ||
$line = SampleTleHelper::$line1; | ||
$line[68] = '0'; | ||
$this->expectExceptionMessage('Calculated line 1 checksum does not match actual checksum'); | ||
$this->validator->validate(new Tle($line, SampleTleHelper::$line2)); | ||
|
||
$line = SampleTleHelper::$line2; | ||
$line[68] = '0'; | ||
$this->expectExceptionMessage('Calculated line 2 checksum does not match actual checksum'); | ||
$this->validator->validate(new Tle(SampleTleHelper::$line1, $line)); | ||
} | ||
} |