Skip to content

Commit

Permalink
first changes for v5
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Oct 20, 2019
1 parent 64a4b8e commit f6ca83d
Show file tree
Hide file tree
Showing 17 changed files with 556 additions and 964 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
vendor
composer.lock
.php_cs.cache
*.cache

# Eclipse-specific files
/.settings/
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

Previous releases are documented in [github releases](https://github.com/oscarotero/Gettext/releases)

## [5.0.0] - Unreleased

### Change
- Minimum PHP version supported is 7.2

## [4.7.0] - 2019-10-07
### Added
- Support for UnitID in Xliff [#221] [#224] [#225]
Expand Down
14 changes: 2 additions & 12 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,13 @@
"issues": "https://github.com/oscarotero/Gettext/issues"
},
"require": {
"php": ">=5.4.0",
"php": "^7.2",
"gettext/languages": "^2.3"
},
"require-dev": {
"illuminate/view": "*",
"twig/twig": "^1.31|^2.0",
"twig/extensions": "*",
"symfony/yaml": "~2",
"phpunit/phpunit": "^4.8|^5.7|^6.5",
"phpunit/phpunit": "^8.0",
"squizlabs/php_codesniffer": "^3.0"
},
"suggest": {
"illuminate/view": "Is necessary if you want to use the Blade extractor",
"twig/twig": "Is necessary if you want to use the Twig extractor",
"twig/extensions": "Is necessary if you want to use the Twig extractor",
"symfony/yaml": "Is necessary if you want to use the Yaml extractor/generator"
},
"autoload": {
"psr-4": {
"Gettext\\": "src"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit bootstrap="./tests/bootstrap.php">
<phpunit bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="All tests">
<directory>./tests/</directory>
Expand Down
41 changes: 41 additions & 0 deletions src/Comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types = 1);

namespace Gettext;

use JsonSerializable;
use IteratorAggregate;
use ArrayIterator;
use Countable;

/**
* Class to manage the comments of a translation.
*/
class Comments implements JsonSerializable, Countable, IteratorAggregate
{
protected $comments = [];

public function add(string $comment): self
{
if (!in_array($comment, $this->comments)) {
$this->comments[] = $comment;
}

return $this;
}

public function jsonSerialize()
{
return $this->comments;
}

public function getIterator()
{
return new ArrayIterator($this->comments);
}

public function count(): int
{
return count($this->comments);
}
}
41 changes: 41 additions & 0 deletions src/Flags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
declare(strict_types = 1);

namespace Gettext;

use JsonSerializable;
use IteratorAggregate;
use ArrayIterator;
use Countable;

/**
* Class to manage the flags of a translation.
*/
class Flags implements JsonSerializable, Countable, IteratorAggregate
{
protected $flags = [];

public function add(string $flag): self
{
if (!in_array($flag, $this->flags)) {
$this->flags[] = $flag;
}

return $this;
}

public function jsonSerialize()
{
return $this->flags;
}

public function getIterator()
{
return new ArrayIterator($this->flags);
}

public function count(): int
{
return count($this->flags);
}
}
115 changes: 115 additions & 0 deletions src/Headers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
declare(strict_types = 1);

namespace Gettext;

use JsonSerializable;
use IteratorAggregate;
use ArrayIterator;
use Countable;
use InvalidArgumentException;

/**
* Class to manage the headers of translations.
*/
class Headers implements JsonSerializable, Countable, IteratorAggregate
{
const HEADER_LANGUAGE = 'Language';
const HEADER_PLURAL = 'Plural-Forms';
const HEADER_DOMAIN = 'X-Domain';

protected $headers = [];

public function set(string $name, string $value): self
{
$this->headers[$name] = trim($value);

return $this;
}

public function get(string $name): ?string
{
return $this->headers[$name] ?? null;
}

public function delete(string $name): self
{
unset($this->headers[$name]);

return $this;
}

public function clear(): self
{
$this->headers = [];

return $this;
}

public function sort(): self
{
ksort($this->headers);

return $this;
}

public function jsonSerialize()
{
return $this->headers;
}

public function getIterator()
{
return new ArrayIterator($this->headers);
}

public function count(): int
{
return count($this->headers);
}

public function setLanguage(string $language): self
{
return $this->set(self::HEADER_LANGUAGE, $language);
}

public function getLanguage(): ?string
{
return $this->get(self::HEADER_LANGUAGE);
}

public function setDomain(string $domain): self
{
return $this->set(self::HEADER_DOMAIN, $domain);
}

public function getDomain(): ?string
{
return $this->get(self::HEADER_DOMAIN);
}

public function setPluralForm(int $count, string $rule): self
{
if (preg_match('/[a-z]/i', str_replace('n', '', $rule))) {
throw new InvalidArgumentException(sprintf('Invalid Plural form: "%s"', $rule));
}

return $this->set(self::HEADER_PLURAL, sprintf('nplurals=%d; plural=%s;', $count, $rule));
}

/**
* Returns the parsed plural definition.
*
* @param null|array [count, rule]
*/
public function getPluralForm(): ?array
{
$header = $this->get(self::HEADER_PLURAL);

if (!empty($header) && preg_match('/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/', $header, $matches)) {
return [intval($matches[1]), $matches[2]];
}

return null;
}
}
47 changes: 47 additions & 0 deletions src/References.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types = 1);

namespace Gettext;

use JsonSerializable;
use IteratorAggregate;
use ArrayIterator;
use Countable;

/**
* Class to manage the references of a translation.
*/
class References implements JsonSerializable, Countable, IteratorAggregate
{
protected $references = [];

public function add(string $filename, int $line): self
{
$fileReferences = $this->references[$filename] ?? [];

if (!in_array($line, $fileReferences)) {
$fileReferences[] = $line;
}

$this->references[$filename] = $fileReferences;

return $this;
}

public function jsonSerialize()
{
return $this->references;
}

public function getIterator()
{
return new ArrayIterator($this->references);
}

public function count(): int
{
return array_reduce($this->references, function ($carry, $item) {
return $carry + count($item);
}, 0);
}
}
Loading

0 comments on commit f6ca83d

Please sign in to comment.