Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
v6 (2018-02-10)
* Improvements on Headers API to allow better manipulation:
** `Catalog` methods `addHeaders` and `getHeader` removed.
** `Headers` object is the old `Header`.
** A new class `Header` represeting a single header is created.
** `Catalog` method `getHeaders` returns a `Headers` object instead of an array of strings.
** You can retrieve a specific header by its key.

v5.1.1 (2018-02-10)
* Parser::parser() optionally accepts a Catalog interface implementation.
* Header::setHeaders() to allow modifying PO headers.

v5.1 (2018-02-04)
Expand Down
9 changes: 2 additions & 7 deletions src/Catalog/Catalog.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface Catalog
{
public function addEntry(Entry $entry);

public function addHeaders(Header $headers);
public function setHeaders(Headers $headers);

/**
* @param string $msgid
Expand All @@ -15,15 +15,10 @@ public function addHeaders(Header $headers);
public function removeEntry($msgid, $msgctxt = null);

/**
* @return array
* @return Headers
*/
public function getHeaders();

/**
* @return Header
*/
public function getHeader();

/**
* @return Entry[]
*/
Expand Down
14 changes: 3 additions & 11 deletions src/Catalog/CatalogArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class CatalogArray implements Catalog
{
/** @var Header */
/** @var Headers */
protected $headers;

/** @var array */
Expand All @@ -16,7 +16,7 @@ class CatalogArray implements Catalog
public function __construct(array $entries = array())
{
$this->entries = array();
$this->headers = new Header();
$this->headers = new Headers();
foreach ($entries as $entry) {
$this->addEntry($entry);
}
Expand All @@ -37,7 +37,7 @@ public function addEntry(Entry $entry)
/**
* {@inheritdoc}
*/
public function addHeaders(Header $headers)
public function setHeaders(Headers $headers)
{
$this->headers = $headers;
}
Expand All @@ -57,14 +57,6 @@ public function removeEntry($msgid, $msgctxt = null)
* {@inheritdoc}
*/
public function getHeaders()
{
return $this->headers->asArray();
}

/**
* {@inheritdoc}
*/
public function getHeader()
{
return $this->headers;
}
Expand Down
61 changes: 17 additions & 44 deletions src/Catalog/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,20 @@

class Header
{
/** @var array */
protected $headers;
/** @var string */
protected $id;

/** @var int|null */
protected $nPlurals;
/** @var string */
protected $value;

public function __construct(array $headers = array())
{
$this->setHeaders($headers);
}

public function getPluralFormsCount()
/**
* @param string $id
* @param string $value
*/
public function __construct($id, $value)
{
if ($this->nPlurals !== null) {
return $this->nPlurals;
}

$header = $this->getHeaderValue('Plural-Forms');
if ($header === null) {
$this->nPlurals = 0;
return $this->nPlurals;
}

$matches = array();
if (preg_match('/nplurals=([0-9]+)/', $header, $matches) !== 1) {
$this->nPlurals = 0;
return $this->nPlurals;
}

$this->nPlurals = isset($matches[1]) ? (int)$matches[1] : 0;

return $this->nPlurals;
$this->id = $id;
$this->value = $value;
}

public function setHeaders(array $headers)
Expand All @@ -44,27 +26,18 @@ public function setHeaders(array $headers)
}

/**
* @return array
* @return string
*/
public function asArray()
public function getId()
{
return $this->headers;
return $this->id;
}

/**
* @param string $headerName
*
* @return string|null
* @return string
*/
protected function getHeaderValue($headerName)
public function getValue()
{
$header = array_values(array_filter(
$this->headers,
function ($string) use ($headerName) {
return preg_match('/'.$headerName.':(.*)/i', $string) == 1;
}
));

return count($header) ? $header[0] : null;
return $this->value;
}
}
124 changes: 124 additions & 0 deletions src/Catalog/Headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace Sepia\PoParser\Catalog;

class Headers
{
/** @var Header[] */
protected $headers;

/** @var int|null */
protected $nPlurals;

/**
* Headers constructor.
*
* @param Header[] $headers
*/
public function __construct(array $headers = array())
{
foreach ($headers as $header) {
$this->headers[$header->getId()] = $header;
}
}

/**
* @param string $headerKey
*
* @return bool
*/
public function has($headerKey)
{
return isset($this->headers[$headerKey]);
}

/**
* @param string $headerKey
*
* @return Header|null
*/
public function get($headerKey)
{
if (!$this->has($headerKey)) {
return null;
}

return $this->headers[$headerKey];
}

/**
* @return Header[]
*/
public function all()
{
return $this->headers;
}

/**
* @param Header $header
*/
public function set(Header $header)
{
$this->headers[$header->getId()] = $header;
}

/**
* Remove a Header.
*
* @param string $headerKey
*/
public function remove($headerKey)
{
if (!$this->has($headerKey)) {
return;
}

unset($this->headers[$headerKey]);
}

/**
* @return int
*/
public function getPluralFormsCount()
{
if ($this->nPlurals !== null) {
return $this->nPlurals;
}

$key = 'Plural-Forms';
if (!$this->has($key)) {
$this->nPlurals = 0;
return $this->nPlurals;
}

$header = $this->get('Plural-Forms');

$matches = array();
if (preg_match('/nplurals=([0-9]+)/', $header->getValue(), $matches) !== 1) {
$this->nPlurals = 0;
return $this->nPlurals;
}

$this->nPlurals = isset($matches[1]) ? (int)$matches[1] : 0;

return $this->nPlurals;
}

/**
* @param string $headerName
*
* @return string|null
*/
/* protected function getHeaderValue($headerName)
{
$header = array_values(array_filter(
$this->headers,
function ($string) use ($headerName) {
return preg_match('/'.$headerName.':(.*)/i', $string) == 1;
}
));

return count($header) ? $header[0] : null;
}
*/
}
21 changes: 17 additions & 4 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Sepia\PoParser\Catalog\CatalogArray;
use Sepia\PoParser\Catalog\EntryFactory;
use Sepia\PoParser\Catalog\Header;
use Sepia\PoParser\Catalog\HeaderFactory;
use Sepia\PoParser\Catalog\Headers;
use Sepia\PoParser\Exception\ParseException;
use Sepia\PoParser\SourceHandler\FileSystem;
use Sepia\PoParser\SourceHandler\SourceHandler;
Expand Down Expand Up @@ -121,7 +123,7 @@ public function parse(Catalog $catalog = null)
if ($this->shouldCloseEntry($line, $entry)) {
if (!$headersFound && $this->isHeader($entry)) {
$headersFound = true;
$catalog->addHeaders(
$catalog->setHeaders(
$this->parseHeaders($entry['msgstr'])
);
} else {
Expand Down Expand Up @@ -317,13 +319,24 @@ protected function parseComment($line, $entry)
/**
* @param string $msgstr
*
* @return Header
* @return Headers
*/
protected function parseHeaders($msgstr)
{
$headers = array_filter(explode('\\n', $msgstr));
$rawHeaders = array_filter(explode('\\n', $msgstr));
$headers = array();

return new Header($headers);
foreach ($rawHeaders as $rawHeader) {
$tokens = explode(':', $rawHeader, 2);
if (count($tokens) !== 2) {
continue;
}

list($id, $value) = $tokens;
$headers[] = new Header($id, trim($value));
}

return new Headers($headers);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions src/PoCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Sepia\PoParser\Catalog\Catalog;
use Sepia\PoParser\Catalog\Entry;
use Sepia\PoParser\Catalog\Header;
use Sepia\PoParser\Catalog\Headers;

class PoCompiler
{
Expand Down Expand Up @@ -40,11 +40,11 @@ public function compile(Catalog $catalog)
{
$output = '';

if (count($catalog->getHeaders()) > 0) {
if (count($catalog->getHeaders()->all()) > 0) {
$output .= 'msgid ""'.$this->eol();
$output .= 'msgstr ""'.$this->eol();
foreach ($catalog->getHeaders() as $header) {
$output .= $header.$this->eol();
foreach ($catalog->getHeaders()->all() as $header) {
$output .= $header->getId().': '.$header->getValue().$this->eol();
}
$output .= $this->eol();
}
Expand All @@ -69,7 +69,7 @@ public function compile(Catalog $catalog)
$output .= $this->buildContext($entry);
$output .= $this->buildMsgId($entry);
$output .= $this->buildMsgIdPlural($entry);
$output .= $this->buildMsgStr($entry, $catalog->getHeader());
$output .= $this->buildMsgStr($entry, $catalog->getHeaders());


$counter++;
Expand Down Expand Up @@ -183,7 +183,7 @@ protected function buildMsgId(Entry $entry)
return $this->buildProperty('msgid', $entry->getMsgId(), $entry->isObsolete());
}

protected function buildMsgStr(Entry $entry, Header $headers)
protected function buildMsgStr(Entry $entry, Headers $headers)
{
$value = $entry->getMsgStr();
$plurals = $entry->getMsgStrPlurals();
Expand Down
2 changes: 1 addition & 1 deletion tests/UnitTest/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testGetPluralFormsCount()
{
$catalog = $this->parseFile();

$this->assertEquals(3, $catalog->getHeader()->getPluralFormsCount());
$this->assertEquals(3, $catalog->getHeaders()->getPluralFormsCount());
}

/**
Expand Down
Loading