[icinga-checkins] icinga.org: icingaweb2/feature/translation-catalog-object-13012: Catalog: Implement methods

git at icinga.org git at icinga.org
Thu Nov 24 11:26:46 CET 2016


Module: icingaweb2
Branch: feature/translation-catalog-object-13012
Commit: 89fae418d9166eb1469b2de3a63d773f37b96c0d
URL:    https://git.icinga.org/?p=icingaweb2.git;a=commit;h=89fae418d9166eb1469b2de3a63d773f37b96c0d

Author: Jennifer Mourek <jennifer.mourek at netways.de>
Date:   Thu Nov 24 11:23:17 2016 +0100

Catalog: Implement methods

refs #13012

---

 .../library/Translation/Catalog/Catalog.php        |   45 +++++++++++++++++---
 1 file changed, 39 insertions(+), 6 deletions(-)

diff --git a/modules/translation/library/Translation/Catalog/Catalog.php b/modules/translation/library/Translation/Catalog/Catalog.php
index a5e04f0..8dcf44d 100644
--- a/modules/translation/library/Translation/Catalog/Catalog.php
+++ b/modules/translation/library/Translation/Catalog/Catalog.php
@@ -3,9 +3,12 @@
 
 namespace Icinga\Module\Translation\Catalog;
 
+
 use ArrayIterator;
 use DateTime;
+use Exception;
 use IteratorAggregate;
+use Icinga\Exception\IcingaException;
 
 
 /**
@@ -25,6 +28,13 @@ class Catalog implements IteratorAggregate
     public $header;
 
     /**
+     * Entries for this Catalog
+     *
+     * @var array
+     */
+    public $entries;
+
+    /**
      * Create a new Catalog
      *
      * @param   CatalogHeader   $header
@@ -32,7 +42,8 @@ class Catalog implements IteratorAggregate
      */
     public function __construct(CatalogHeader $header, array $entries)
     {
-
+        $this->header = $header;
+        $this->entries = $entries;
     }
 
     /**
@@ -44,7 +55,20 @@ class Catalog implements IteratorAggregate
      */
     public static function fromArray(array $rawEntries)
     {
-
+        $header = null;
+        $entries = array();
+        foreach ($rawEntries as $rawEntry) {
+            if (isset($rawEntry['msgid']) && empty($rawEntry['msgid'])) {
+                $header = CatalogHeader::fromString($rawEntry['msgstr'][0]);
+                if (isset($rawEntry['translator_comments'])) {
+                    $header->setCopyrightInformation($rawEntry['translator_comments']);
+                }
+            } elseif (isset($rawEntry['msgid'])) {
+                $entries[] = CatalogEntry::fromArray($rawEntry);
+            }
+        }
+
+        return new Catalog($header, $entries);
     }
 
     /**
@@ -66,7 +90,7 @@ class Catalog implements IteratorAggregate
      */
     public function getIterator()
     {
-
+        return new ArrayIterator($this->entries);
     }
 
     /**
@@ -127,7 +151,7 @@ class Catalog implements IteratorAggregate
      */
     public function creationDate()
     {
-
+        return date_create_from_format(CatalogHeader::DATETIME_FORMAT, $this->getHeader('POT-Creation-Date'));
     }
 
     /**
@@ -137,7 +161,7 @@ class Catalog implements IteratorAggregate
      */
     public function revisionDate()
     {
-
+        return date_create_from_format(CatalogHeader::DATETIME_FORMAT, $this->getHeader('PO-Revision-Date'));
     }
 
     /**
@@ -147,7 +171,12 @@ class Catalog implements IteratorAggregate
      */
     public function render()
     {
+        $renderedCatalog = $this->header->render();
+        foreach ($this->entries as $entry) {
+            $renderedCatalog .= "\n\n" . $entry->render();
+        }
 
+        return $renderedCatalog;
     }
 
     /**
@@ -155,6 +184,10 @@ class Catalog implements IteratorAggregate
      */
     public function __toString()
     {
-
+        try {
+            return $this->render();
+        } catch (Exception $e) {
+            return 'Failed to render Catalog: ' . IcingaException::describe($e);
+        }
     }
 }



More information about the icinga-checkins mailing list