Page MenuHomeDevCentral

D512.diff
No OneTemporary

D512.diff

diff --git a/app/Console/Commands/AccountInfo.php b/app/Console/Commands/AccountInfo.php
--- a/app/Console/Commands/AccountInfo.php
+++ b/app/Console/Commands/AccountInfo.php
@@ -2,8 +2,10 @@
namespace AuthGrove\Console\Commands;
-use Illuminate\Console\Command;
use AuthGrove\Console\Services\AccountHelpers as AccountHelpers;
+use AuthGrove\Internationalization\UserAttribute;
+
+use Illuminate\Console\Command;
class AccountInfo extends Command {
@@ -43,10 +45,11 @@
* @param string $value The value for this attribute
*/
protected function printInformationAttribute ($attribute, $value) {
- $line = \Keruald\mb_str_pad(
- AccountHelpers::localizeUserAttribute($attribute),
- self::ATTRIBUTE_LEN
- );
+ $line = (new UserAttribute($attribute))
+ ->localize()
+ ->replaceSpacesByNonBreakableSpaces()
+ ->pad(self::ATTRIBUTE_LEN)
+ ->get();
$line .= $value;
$this->info($line);
}
diff --git a/app/Console/Services/AccountHelpers.php b/app/Console/Services/AccountHelpers.php
--- a/app/Console/Services/AccountHelpers.php
+++ b/app/Console/Services/AccountHelpers.php
@@ -30,17 +30,4 @@
return null;
}
- public static function getNonBreakableSpace() {
- return mb_convert_encoding(' ', 'UTF-8', 'HTML-ENTITIES');
- }
-
- public static function localizeUserAttribute ($attribute, $useNoBreakableSpace = true) {
- $localizedKey = 'panel.user-attributes.' . $attribute;
- $localizedString = trans($localizedKey);
- if ($useNoBreakableSpace) {
- return str_replace(' ', self::getNonBreakableSpace(), $localizedString);
- }
- return $localizedString;
- }
-
}
diff --git a/app/Internationalization/LocalizableMessage.php b/app/Internationalization/LocalizableMessage.php
new file mode 100644
--- /dev/null
+++ b/app/Internationalization/LocalizableMessage.php
@@ -0,0 +1,103 @@
+<?php
+
+namespace AuthGrove\Internationalization;
+
+class LocalizableMessage {
+
+ ///
+ /// Properties
+ ///
+
+ /**
+ * The prefix to use to get l10n
+ *
+ * @var string
+ */
+ protected $prefix = '';
+
+ /**
+ * The message to localize
+ *
+ * @var string
+ */
+ protected $message;
+
+ /**
+ * The current representation of the message string
+ *
+ * @var string
+ */
+ protected $string;
+
+ ///
+ /// Constructor
+ ///
+
+ /**
+ * Represents a new instance of the LocalizableMessage class
+ */
+ public function __construct ($message) {
+ $this->message = $message;
+ $this->string = $message; // By default, it contains unlocalized text.
+ }
+
+ ///
+ /// L10n methods
+ ///
+
+ /**
+ * @return LocalizableMessage
+ */
+ public function localize () {
+ $this->string = trans($this->prefix . $this->message);
+ return $this;
+ }
+
+ ///
+ /// Transformation methods
+ ///
+
+ /**
+ * Replaces spaces by no breaking spaces
+ * @return LocalizableMessage
+ */
+ public function replaceSpacesByNonBreakableSpaces () {
+ $nbsp = self::getNonBreakableSpace();
+ $this->string = str_replace(' ', $nbsp, $this->string);
+ return $this;
+ }
+
+ /**
+ * Pads the message
+ *
+ * @param int $len The length of the taget string
+ */
+ public function pad ($len) {
+ $this->string = \Keruald\mb_str_pad($this->string, $len);
+ return $this;
+ }
+
+ ///
+ /// Output methods
+ ///
+
+ /**
+ * Gets a string representation of the message
+ * @var string
+ */
+ public function get () {
+ return $this->string;
+ }
+
+ ///
+ /// Static helpers methods
+ ///
+
+ /**
+ * Gets non breakable space
+ */
+ public static function getNonBreakableSpace() {
+ return mb_convert_encoding('&nbsp;', 'UTF-8', 'HTML-ENTITIES');
+ }
+
+}
diff --git a/app/Internationalization/UserAttribute.php b/app/Internationalization/UserAttribute.php
new file mode 100644
--- /dev/null
+++ b/app/Internationalization/UserAttribute.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace AuthGrove\Internationalization;
+
+class UserAttribute extends LocalizableMessage {
+
+ function __construct ($attribute) {
+ $this->prefix = "panel.user-attributes.";
+ parent::__construct($attribute);
+ }
+
+}
diff --git a/tests/Internationalization/LocalizableMessageTest.php b/tests/Internationalization/LocalizableMessageTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Internationalization/LocalizableMessageTest.php
@@ -0,0 +1,49 @@
+<?php
+
+use AuthGrove\Internationalization\LocalizableMessage;
+
+class LocalizableMessageTest extends TestCase {
+
+ /**
+ * @var AuthGrove\Internationalization\LocalizableMessage
+ */
+ protected $message;
+
+ public function setUp () {
+ $this->message = new LocalizableMessage('app.title');
+
+ parent::setUp();
+ }
+
+ public function testDryGet () {
+ $this->assertSame('app.title', $this->message->get());
+ }
+
+ public function testLocalize () {
+ $message = $this->message->localize()->get();
+ $this->assertSame('Auth Grove', $message);
+ }
+
+ public function testPad () {
+ $message = $this->message
+ ->pad(12)
+ ->get();
+ $this->assertSame('app.title ', $message);
+ }
+
+ public function testReplaceSpacesByNonBreakableSpaces () {
+ $message = $this->message
+ ->localize()
+ ->replaceSpacesByNonBreakableSpaces()
+ ->get();
+ $this->assertSame('Auth Grove', $message);
+ }
+
+ public function testGetNonBreakableSpace () {
+ $this->assertSame(
+ ' ',
+ LocalizableMessage::getNonBreakableSpace()
+ );
+ }
+
+}
diff --git a/tests/Internationalization/UserAttributeTest.php b/tests/Internationalization/UserAttributeTest.php
new file mode 100644
--- /dev/null
+++ b/tests/Internationalization/UserAttributeTest.php
@@ -0,0 +1,12 @@
+<?php
+
+use AuthGrove\Internationalization\UserAttribute;
+
+class UserAttributeTest extends TestCase {
+
+ public function testMessage () {
+ $attribute = new UserAttribute("id");
+ $this->assertSame("User ID", $attribute->localize()->get());
+ }
+
+}

File Metadata

Mime Type
text/plain
Expires
Sat, Nov 16, 20:22 (21 h, 24 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2248051
Default Alt Text
D512.diff (6 KB)

Event Timeline