Jelajahi Sumber

Add Label Translator Strategy

Thomas Rabaix 13 tahun lalu
induk
melakukan
425f4bf5b6

+ 26 - 0
Tests/Translator/NativeLabelTranslatorStrategyTest.php

@@ -0,0 +1,26 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+namespace Sonata\AdminBundle\Tests\Translator;
+
+use Sonata\AdminBundle\Translator\NativeLabelTranslatorStrategy;
+
+class NativeTranslatorStrategyTest extends \PHPUnit_Framework_TestCase
+{
+    public function testLabel()
+    {
+        $strategy = new NativeLabelTranslatorStrategy;
+
+        $this->assertEquals('Is Valid', $strategy->getLabel('isValid'));
+        $this->assertEquals('Is Valid', $strategy->getLabel('is_Valid'));
+        $this->assertEquals('Is0 Valid', $strategy->getLabel('is0Valid'));
+        $this->assertEquals('Is Valid Super Cool', $strategy->getLabel('isValid_SuperCool'));
+    }
+}

+ 24 - 0
Tests/Translator/NoopLabelTranslatorStrategyTest.php

@@ -0,0 +1,24 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+namespace Sonata\AdminBundle\Tests\Translator;
+
+use Sonata\AdminBundle\Translator\NoopLabelTranslatorStrategy;
+
+class NoopLabelTranslatorStrategyTest extends \PHPUnit_Framework_TestCase
+{
+    public function testLabel()
+    {
+        $strategy = new NoopLabelTranslatorStrategy;
+
+        $this->assertEquals('isValid', $strategy->getLabel('isValid'));
+        $this->assertEquals('isValid_SuperCool', $strategy->getLabel('isValid_SuperCool'));
+    }
+}

+ 24 - 0
Tests/Translator/UnderscoreLabelTranslatorStrategyTest.php

@@ -0,0 +1,24 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+namespace Sonata\AdminBundle\Tests\Translator;
+
+use Sonata\AdminBundle\Translator\UnderscoreLabelTranslatorStrategy;
+
+class UnderscoreLabelTranslatorStrategyTest extends \PHPUnit_Framework_TestCase
+{
+    public function testLabel()
+    {
+        $strategy = new UnderscoreLabelTranslatorStrategy;
+
+        $this->assertEquals('label_is_valid', $strategy->getLabel('isValid'));
+        $this->assertEquals('label_is0_valid', $strategy->getLabel('is0Valid'));
+    }
+}

+ 21 - 0
Translator/LabelTranslatorStrategyInterface.php

@@ -0,0 +1,21 @@
+<?php
+
+/*
+ * This file is part of sonata-project.
+ *
+ * (c) 2010 Thomas Rabaix
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Translator;
+
+interface LabelTranslatorStrategyInterface
+{
+    /**
+     * @abstract
+     * @param $label
+     */
+    function getLabel($label);
+}

+ 27 - 0
Translator/NativeLabelTranslatorStrategy.php

@@ -0,0 +1,27 @@
+<?php
+
+/*
+ * This file is part of sonata-project.
+ *
+ * (c) 2010 Thomas Rabaix
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Translator;
+
+class NativeLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
+{
+    /**
+     * @param string $label
+     * @return string
+     */
+    public function getLabel($label)
+    {
+        $label = str_replace('_', ' ', $label);
+        $label = strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label));
+
+        return ucwords(str_replace('_', ' ', $label));
+    }
+}

+ 24 - 0
Translator/NoopLabelTranslatorStrategy.php

@@ -0,0 +1,24 @@
+<?php
+
+/*
+ * This file is part of sonata-project.
+ *
+ * (c) 2010 Thomas Rabaix
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Translator;
+
+class NoopLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
+{
+    /**
+     * @param string $label
+     * @return string
+     */
+    public function getLabel($label)
+    {
+        return $label;
+    }
+}

+ 24 - 0
Translator/UnderscoreLabelTranslatorStrategy.php

@@ -0,0 +1,24 @@
+<?php
+
+/*
+ * This file is part of sonata-project.
+ *
+ * (c) 2010 Thomas Rabaix
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\AdminBundle\Translator;
+
+class UnderscoreLabelTranslatorStrategy implements LabelTranslatorStrategyInterface
+{
+    /**
+     * @param string $label
+     * @return string
+     */
+    public function getLabel($label)
+    {
+        return sprintf('label_%s', strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $label)));
+    }
+}