-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
1 parent
64a4b8e
commit f6ca83d
Showing
17 changed files
with
556 additions
and
964 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
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/ | ||
|
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 |
---|---|---|
@@ -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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
Oops, something went wrong.