Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Commit

Permalink
Changed behaviour (dont throw exception - instead, use the default lo…
Browse files Browse the repository at this point in the history
…cale)
  • Loading branch information
stevenobird authored and devheidelpay committed Aug 2, 2017
1 parent d61ff75 commit 1b29038
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
36 changes: 22 additions & 14 deletions lib/CustomerMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,20 @@
*/
class CustomerMessage
{
/** @var string the default locale to be used */
const DEFAULT_LOCALE = 'en_US';

/** @var FileSystem A helper class for file handling. */
private $_filesystem = null;
private $fileSystem = null;

/** @var string The locale (IETF tag is recommended) to be used by the library. */
private $_locale;
private $locale;

/** @var string The path of the locale file. */
private $_path;
private $path;

/** @var array Contains all customer messages. */
private $_messages;
private $messages;

/**
* The CustomerMessage constructor, which accepts the locale and
Expand All @@ -49,13 +52,18 @@ class CustomerMessage
*/
public function __construct($locale = 'en_US', $path = null)
{
$this->_locale = $locale;
$this->locale = $locale;

if ($path !== null && is_string($path)) {
$this->path = $path;
}

// if the locale file does not exist, we better throw an exception
// if the locale file does not exist, we set the default locale
if (! file_exists($this->getLocalePath())) {
$this->locale = self::DEFAULT_LOCALE;
}

// if the locale file still does not exist, we better throw an exception
// instead of just let PHP error_log something.
if (! file_exists($this->getLocalePath())) {
throw new MissingLocaleFileException(
Expand All @@ -75,7 +83,7 @@ public function __construct($locale = 'en_US', $path = null)
*/
public function getLocale()
{
return $this->_locale;
return $this->locale;
}

/**
Expand All @@ -95,7 +103,7 @@ public function getLocalePath()
*/
public function getPath()
{
return $this->_path ?: __DIR__ . '/locales';
return $this->path ?: __DIR__ . '/locales';
}

/**
Expand All @@ -111,8 +119,8 @@ public function getMessage($messagecode)
$messagecode = 'HPError-' . $messagecode;
}

return isset($this->_messages[$messagecode])
? $this->_messages[$messagecode]
return isset($this->messages[$messagecode])
? $this->messages[$messagecode]
: $this->getDefaultMessage($messagecode);
}

Expand All @@ -126,8 +134,8 @@ public function getMessage($messagecode)
*/
public function getDefaultMessage($messagecode = '000.000.000')
{
return isset($this->_messages['Default'])
? $this->_messages['Default']
return isset($this->messages['Default'])
? $this->messages['Default']
: "An unspecific error occured. HPErrorcode: {$messagecode}";
}

Expand All @@ -139,7 +147,7 @@ public function getDefaultMessage($messagecode = '000.000.000')
private function setContent()
{
// open the fs to retrieve file information.
$this->_filesystem = new FileSystem($this->getLocalePath());
$this->_messages = $this->_filesystem->getCsvContent();
$this->fileSystem = new FileSystem($this->getLocalePath());
$this->messages = $this->fileSystem->getCsvContent();
}
}
3 changes: 2 additions & 1 deletion tests/unit/customerMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ public function throwMissingLocaleFileException()
{
$this->expectException(MissingLocaleFileException::class);

$message = new CustomerMessage('ab_CD');
$message = new CustomerMessage('ab_CD', 'invalid/path');
$this->assertEquals('en_US', $message->getLocale());
echo $message->getMessage('HPError-100.100.101');
}
}

0 comments on commit 1b29038

Please sign in to comment.