Skip to content

Commit

Permalink
add validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Stanojevic committed Jan 10, 2020
1 parent c942e9c commit 509931a
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 176 deletions.
6 changes: 3 additions & 3 deletions src/Field/NameField.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

trait NameField
{
private string $name;
private ?string $name;

public function getName(): string
public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): void
public function setName(?string $name): void
{
$this->name = $name;
}
Expand Down
14 changes: 1 addition & 13 deletions src/Model/Tle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,17 @@
use Ivanstan\Tle\Field\InclinationField;
use Ivanstan\Tle\Field\NameField;
use Ivanstan\Tle\Field\TleField;
use Ivanstan\Tle\Service\ChecksumCalculator;

class Tle
{
public const LINE1 = 1;
public const LINE2 = 2;

public const SATELLITE_UNCLASSIFIED = 'U';
public const SATELLITE_CLASSIFIED = 'C';
public const SATELLITE_SECRET = 'S';

use NameField;
use TleField;
use InclinationField;

public function __construct(string $line1, string $line2, string $name = null)
public function __construct(string $line1, string $line2, ?string $name = null)
{
$this->line1 = $line1;
$this->line2 = $line2;
Expand Down Expand Up @@ -150,13 +145,6 @@ public function getChecksum(int $lineNumber): int
return (int)trim(substr($line, 68));
}

public function calculateChecksum(int $lineNumber): int
{
$line = $this->getLineByNumber($lineNumber);

return ChecksumCalculator::calculate($line);
}

private function formatYear(int $twoDigitYear): int
{
if ($twoDigitYear < 57) {
Expand Down
133 changes: 1 addition & 132 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
*/
class Parser
{
private $dateTimeZone;

/**
* First Line String.
*
Expand All @@ -23,34 +21,6 @@ class Parser
*/
public $secondLine;

/**
* Parsed checksum of the first line.
*
* @var int
*/
public $firstLineChecksum;

/**
* Calculated checksum value of the first line.
*
* @var int
*/
public $firstLineCalculatedChecksum;

/**
* Parsed checksum of the second line.
*
* @var int
*/
public $secondLineChecksum;

/**
* Calculated checksum value of the second line.
*
* @var int
*/
public $secondLineCalculatedChecksum;

/**
* Twenty-four character name (to be consistent with the name length in the NORAD Satellite Catalog).
*
Expand All @@ -65,20 +35,6 @@ class Parser
*/
public $satelliteNumber;

/**
* Satellite Classification. Either SATELLITE_UNCLASSIFIED, SATELLITE_CLASSIFIED or SATELLITE_SECRET.
*
* @var string
*/
public $classification;

/**
* Satellite Launch Year.
*
* @var int
*/
public $internationalDesignatorLaunchYear;

/**
* Number of launch in Launch Year
*
Expand All @@ -94,48 +50,6 @@ class Parser
*/
public $internationalDesignatorLaunchPiece;

/**
* A src Epoch indicates the UTC time when the src's indicated orbit elements were true.
*
* @var string
*/
public $epoch;

/**
* Year of the epoch when src is taken.
*
* @var int
*/
public $epochYear;

/**
* Day of the year when src was taken.
*
* @var string
*/
public $epochDay;

/**
* Decimal (fraction) days.
*
* @var float
*/
public $epochFraction;

/**
* Calculated Linux Timestamp when src is taken.
*
* @var int
*/
public $epochUnixTimestamp;

/**
* Elapsed time in seconds from src Epoch.
*
* @var mixed
*/
public $deltaSec;

/**
* Half of the Mean Motion First Time Derivative, measured in orbits per day per day (orbits/day^2).
*
Expand Down Expand Up @@ -223,61 +137,16 @@ class Parser
*/
public function __construct($tleString, $dateTime = null)
{
$this->dateTimeZone = new \DateTimeZone('UTC');
$this->currentDateTime = ($dateTime == null) ? getdate() : $dateTime;

$lines = explode("\n", $tleString);

switch (count($lines)) {
case 2:

break;
case 3:
$this->name = substr($lines[0], 0, 24);
unset($lines[0]);
break;
default:
throw new \Exception('Invalid two element set');
}

$this->firstLine = trim(reset($lines));
$this->secondLine = trim(end($lines));

$this->firstLineChecksum = (int)trim(substr($this->firstLine, 68));
$this->firstLineCalculatedChecksum = (int)$this->calculateChecksum($this->firstLine);

if ($this->firstLineChecksum != $this->firstLineCalculatedChecksum) {
throw new \Exception('src First Line ChecksumCalculatorTest.php fail');
}

$this->secondLineChecksum = (int)trim(substr($this->secondLine, 68));
$this->secondLineCalculatedChecksum = (int)$this->calculateChecksum($this->secondLine);

if ($this->secondLineChecksum != $this->secondLineCalculatedChecksum) {
throw new \Exception('src Second Line ChecksumCalculatorTest.php fail');
}

$this->satelliteNumber = (int)substr($this->firstLine, 2, 6);
$this->classification = substr($this->firstLine, 7, 1);
$this->internationalDesignatorLaunchYear = (int)trim(substr($this->firstLine, 9, 2));

if ($this->internationalDesignatorLaunchYear) {
$dateTime = \DateTime::createFromFormat('y', $this->internationalDesignatorLaunchYear);
$dateTime->setTimezone($this->dateTimeZone);
$this->internationalDesignatorLaunchYear = (int)$dateTime->format('Y');
}

$this->internationalDesignatorLaunchNumber = (int)trim(substr($this->firstLine, 12, 2));
$this->internationalDesignatorLaunchPiece = trim(substr($this->firstLine, 14, 2));
$this->epochYear = trim(substr($this->firstLine, 18, 2));
$dateTime = \DateTime::createFromFormat('y', $this->epochYear);
$dateTime->setTimezone($this->dateTimeZone);
$this->epochYear = (int)$dateTime->format('Y');
$this->epoch = trim(substr($this->firstLine, 18, 14));
$epoch = explode('.', trim(substr($this->firstLine, 20, 12)));
$this->epochDay = (int)$epoch[0];
$this->epochFraction = '0.' . $epoch[1];
$this->epochUnixTimestamp = $this->getEpochUnixTimestamp();

$this->meanMotionFirstDerivative = trim(substr($this->firstLine, 33, 10));
$this->meanMotionSecondDerivative = trim(substr($this->firstLine, 44, 8));
$this->bStarDragTerm = trim(substr($this->firstLine, 53, 8));
Expand Down
49 changes: 46 additions & 3 deletions src/Service/Validator.php
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');
}
}
}
31 changes: 6 additions & 25 deletions tests/TleModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Tests;

use Ivanstan\Tle\Helper\SampleTleHelper;
use Ivanstan\Tle\Model\Tle;
use PHPUnit\Framework\TestCase;

final class TleModelTest extends TestCase
Expand All @@ -18,30 +17,6 @@ public function testParse(): void
'Failed asserting TLE returned correct date'
);

static::assertEquals(
0,
$tle->getChecksum(Tle::LINE1),
'Failed asserting TLE checksum for line1 is correct'
);

static::assertEquals(
4,
$tle->getChecksum(Tle::LINE2),
'Failed asserting TLE checksum for line2 is correct'
);

static::assertEquals(
0,
$tle->calculateChecksum(Tle::LINE1),
'Failed asserting TLE calculated checksum for line1 is correct'
);

static::assertEquals(
4,
$tle->calculateChecksum(Tle::LINE2),
'Failed asserting TLE calculated checksum for line2 is correct'
);

static::assertEquals(
43550,
$tle->getId(),
Expand All @@ -53,5 +28,11 @@ public function testParse(): void
$tle->getClassification(),
'Failed asserting that TLE classification is correct'
);

static::assertEquals(
'1998-067NY',
$tle->getName(),
'Assert TLE name is correct'
);
}
}
49 changes: 49 additions & 0 deletions tests/ValidatorTest.php
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));
}
}

0 comments on commit 509931a

Please sign in to comment.