Skip to content

Commit

Permalink
Refactoring to use dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoltan committed Feb 4, 2019
1 parent e1fc6f4 commit 479a4b1
Show file tree
Hide file tree
Showing 22 changed files with 503 additions and 516 deletions.
33 changes: 24 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,42 @@ composer update colourbox-account/i18n

## Usage

### Initialize
### Create instance

```php
CBX\i18n::setAPIURL("https://test-tb.cbx.xyz");
CBX\i18n::setLanguage('en_GB');
CBX\i18n::setDomain('i18n-develop-test');
$api = new CBX\API("https://test-tb.cbx.xyz");
$config = new CBX\Config("en_GB", "i18n-develop-test");
$collections = new CBX\Collections($config);
$i18n = new i18nClass($collections);
```

### Create instance with factory

```php
$i18n = CBX\i18n::create("en_GB", "i18n-develop-test", "https://test-tb.cbx.xyz");
```

### Getting simple translation

```php
// 'contact/phone' -> '+45 55 55 45'
echo CBX\i18n::_('contact/phone');
echo $i18n->_('contact/phone');
// output: +45 55 55 45
```

### Getting simple translation with placeholders

```php
// 'global/newPrice' -> 'New price: $price$'
echo CBX\i18n::_('newPrice', [ 'price' => '10€' ]);
echo $i18n->_('newPrice', [ 'price' => '10€' ]);
// output: New price: 10€
```

### Use in HTML

```html
<!-- 'global/helloWorld' -> 'Hello World!!!' -->
<p><?=CBX\i18n::_htmlEscaped('helloWorld');?></p>
<p><?=$i18n->_htmlEscaped('helloWorld');?></p>
<!-- output: <p>Hello World!!!</p> -->
```

Expand All @@ -56,10 +63,16 @@ echo CBX\i18n::_('newPrice', [ 'price' => '10€' ]);
# Test

Run
Offline test

```
test/test.sh
test/online-test.sh
```

Online test

```
test/offline-test.sh
```


Expand All @@ -69,5 +82,7 @@ PHP: ^5.3.0 || ^7.0

PHP Curl extension

PHP Memcache extension

> This package requires no other composer packages
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"minimum-stability": "stable",
"require": {
"php": "^5.3.0 || ^7.0",
"ext-curl": "*"
"ext-curl": "*",
"ext-memcached": "*"
},
"autoload": {
"psr-0": {
Expand Down
40 changes: 14 additions & 26 deletions src/CBX/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,28 @@ class API
{
private $url = null;

public function setURL($url) {
if (Validate::url($url)) {
public function __construct($url) {
if ($url) {
$this->url = $url;
return $this->url;
} else {
trigger_error("I18NClass API Error. API url '{$url}' is not valid.");
}
return false;
}

public function getURL() {
return $this->url;
}

public function isValid() {
return Validate::url($this->url);
}

public function getConfigURL($language, $domain) {
if (Validate::language($language) && Validate::domain($domain) && $this->isValid()) {
return "{$this->getURL()}/translation/config/{$language}/{$domain}";
}
return false;
}

public function fetchConfig($language, $domain) {
$url = $this->getConfigURL($language, $domain);
if (Validate::url($url)) {
return $this->fetch($url);
}
return false;
return $this->fetch("{$this->getURL()}/translation/config/{$language}/{$domain}");
}

public function fetchCollection($url) {
if (Validate::url($url)) {
return $this->fetch($url);
}
return false;
return $this->fetch($url);
}

public function fetch($url) {
if (Validate::url($url)) {
private function fetch($url) {
if ($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
Expand All @@ -57,8 +39,14 @@ public function fetch($url) {
$json = json_decode(trim($result), true);
if (json_last_error() === 0) {
return $json;
} else {
trigger_error("I18NClass API Error. (JSON) ".json_last_error_msg());
}
} else {
trigger_error("I18NClass API Error. (CURL) ".curl_error($ch));
}
} else {
trigger_error("I18NClass API Error. Url '{$url}' is not valid. ");
}
return false;
}
Expand Down
47 changes: 47 additions & 0 deletions src/CBX/APIOffline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace CBX;

class APIOffline
{
private $dir = null;

public function __construct($dir) {
if (file_exists($dir)) {
$this->dir = $dir;
} else {
trigger_error("I18NClass API Error. API directory '{$dir}' is not exists.");
}
}

public function getURL() {
return $this->dir;
}

public function fetchConfig($language, $domain) {
return $this->fetch("{$this->dir}/{$language}_{$domain}.json");
}

public function fetchCollection($file) {
return $this->fetch($this->dir.'/'.$file);
}

private function fetch($path) {
if (file_exists($path)) {
$content = file_get_contents($path);
if ($content) {
$json = json_decode(trim($content), true);
if (json_last_error() === 0) {
return $json;
} else {
trigger_error("I18NClass API Error. (JSON) ".json_last_error_msg());
}
} else {
trigger_error("I18NClass API Error. File '{$path}' is not exists.");
}
} else {
trigger_error("I18NClass API Error. File '{$path}' is not exists.");
}
return false;
}
}
110 changes: 0 additions & 110 deletions src/CBX/Collection.php

This file was deleted.

66 changes: 44 additions & 22 deletions src/CBX/Collections.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,59 @@

class Collections
{
private $i18n;
private $data;
private $config;
private $collections;

public function __construct($I18NObject) {
$this->i18n = $I18NObject;
$this->data = [];
public function __construct(Config $config) {
$this->config = $config;
$this->collections = [];
}

/** Nr. of collections */
public function length() {
return count($this->data);
public function getAPIURL() {
return $this->config->getAPI()->getURL();
}

/** Add collection */
public function add($index, $url = false) {
$fullIndex = Index::toCollectionIndex($index, $this->i18n->getLanguage(), $this->i18n->getDomain());
if (!$this->is($fullIndex)) {
return $this->data[$fullIndex] = new Collection($this->i18n, $fullIndex, $url);
}
return $this->get($fullIndex);
public function getLanguage() {
return $this->config->getLanguage();
}

public function getDomain() {
return $this->config->getDomain();
}

/** Check for collection */
public function is($index) {
return isset($this->data[Index::toCollectionIndex($index, $this->i18n->getLanguage(), $this->i18n->getDomain())]);
public function getTranslation($collection, $index) {
$collectionData = $this->getCollectionByName($collection);
if ($collectionData && isset($collectionData[$index])) {
return $collectionData[$index]['text'];
}
return false;
}

/** Get collection */
public function get($index) {
$fullIndex = Index::toCollectionIndex($index, $this->i18n->getLanguage(), $this->i18n->getDomain());
return $this->is($fullIndex) ? $this->data[$fullIndex] : false;
private function getCollectionByName($collection) {
if (isset($this->collections[$collection])) {
return $this->collections[$collection];
} else {
return $this->addCollection($collection);
}
}

private function addCollection($collection) {
if (isset($this->collections[$collection])) {
return $this->collections[$collection];
}
if (Validate::collection($collection)) {
$collectionUrl = $this->config->getCollectionUrl($collection);
if ($collectionUrl) {
if ($collectionData = $this->config->getAPI()->fetchCollection($collectionUrl)) {
$this->collections[$collection] = $collectionData;
return $this->collections[$collection];
}
} else {
trigger_error("I18NClass Collections Error. Collection URL not found in config file for '{$collection}'.");
}
} else {
trigger_error("I18NClass Collections Error. Collection '{$collection}' is not valid.");
}
return false;
}
}
Loading

0 comments on commit 479a4b1

Please sign in to comment.