Add toString methods
This commit is contained in:
parent
462d2350f0
commit
b56beba4ce
@ -13,6 +13,10 @@ class FitsHeader
|
|||||||
* @var Keyword[] $keywords
|
* @var Keyword[] $keywords
|
||||||
*/
|
*/
|
||||||
public readonly array $keywords;
|
public readonly array $keywords;
|
||||||
|
/**
|
||||||
|
* @var string[] $blanks
|
||||||
|
*/
|
||||||
|
private array $blanks;
|
||||||
|
|
||||||
public function __construct(string $headerBlock)
|
public function __construct(string $headerBlock)
|
||||||
{
|
{
|
||||||
@ -30,26 +34,42 @@ class FitsHeader
|
|||||||
{
|
{
|
||||||
$records = str_split($this->headerBlock, 80);
|
$records = str_split($this->headerBlock, 80);
|
||||||
|
|
||||||
$records = array_filter(
|
$filtered = array_filter(
|
||||||
$records,
|
$records,
|
||||||
fn (string $r) => trim($r) !== '' && !str_starts_with($r, 'END')
|
fn (string $r) => trim($r) !== '' && !str_starts_with($r, 'END')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$this->blanks = array_diff($records, $filtered);
|
||||||
|
|
||||||
$keywords = [];
|
$keywords = [];
|
||||||
|
|
||||||
foreach ($records as $record) {
|
foreach ($filtered as $record) {
|
||||||
$splitByComment = explode('/', $record);
|
$splitByComment = explode('/', $record);
|
||||||
$comment = isset($splitByComment[1]) ? trim($splitByComment[1]) : null;
|
$comment = isset($splitByComment[1]) ? $splitByComment[1] : null;
|
||||||
$nameValue = explode('=', $splitByComment[0]);
|
[$name, $value] = explode('=', $splitByComment[0]);
|
||||||
|
|
||||||
$keywords[] = new Keyword(
|
$keywords[] = new Keyword(
|
||||||
name : trim($nameValue[0]),
|
name : $name,
|
||||||
value : trim($nameValue[1]),
|
value : $value,
|
||||||
comment : $comment,
|
comment : $comment,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $keywords;
|
return $keywords;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Return the FITS header as a string (byte stream)
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
$blanks = implode('', $this->blanks);
|
||||||
|
$keywordsString = '';
|
||||||
|
|
||||||
|
foreach ($this->keywords as $keyword) {
|
||||||
|
$keywordsString .= $keyword->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $keywordsString . $blanks;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,4 +15,9 @@ readonly class Keyword
|
|||||||
public string $value,
|
public string $value,
|
||||||
public ?string $comment,
|
public ?string $comment,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return "{$this->name}={$this->value}/{$this->comment}";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user