[Argil-svn] SF.net SVN: argil: [532] branches/experimental
Status: Alpha
Brought to you by:
tswicegood
|
From: <tsw...@us...> - 2007-05-25 23:35:49
|
Revision: 532
http://argil.svn.sourceforge.net/argil/?rev=532&view=rev
Author: tswicegood
Date: 2007-05-25 16:35:50 -0700 (Fri, 25 May 2007)
Log Message:
-----------
Add in a camel caps to underscore converter
Added Paths:
-----------
branches/experimental/src/Argil/Util/Template/
branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore/
branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore/Exception.php
branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore.php
branches/experimental/tests/Argil/Util/Template/
branches/experimental/tests/Argil/Util/Template/CamelCapsToUnderscoreTest.php
Added: branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore/Exception.php
===================================================================
--- branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore/Exception.php (rev 0)
+++ branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore/Exception.php 2007-05-25 23:35:50 UTC (rev 532)
@@ -0,0 +1,6 @@
+<?php
+
+class Argil_Util_Template_CamelCapsToUnderscore_Exception extends Exception
+{
+
+}
\ No newline at end of file
Added: branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore.php
===================================================================
--- branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore.php (rev 0)
+++ branches/experimental/src/Argil/Util/Template/CamelCapsToUnderscore.php 2007-05-25 23:35:50 UTC (rev 532)
@@ -0,0 +1,21 @@
+<?php
+
+require_once 'Argil/Util/Template/CamelCapsToUnderscore/Exception.php';
+
+class Argil_Util_Template_CamelCapsToUnderscore
+{
+ private $value = 'camel_caps';
+
+ public function __construct($string)
+ {
+ if (!is_string($string) || empty($string)) {
+ throw new Argil_Util_Template_CamelCapsToUnderscore_Exception();
+ }
+ $this->value = strtolower(preg_replace('/(.)([A-Z])/', '$1_$2', $string));
+ }
+
+ public function __toString()
+ {
+ return $this->value;
+ }
+}
\ No newline at end of file
Added: branches/experimental/tests/Argil/Util/Template/CamelCapsToUnderscoreTest.php
===================================================================
--- branches/experimental/tests/Argil/Util/Template/CamelCapsToUnderscoreTest.php (rev 0)
+++ branches/experimental/tests/Argil/Util/Template/CamelCapsToUnderscoreTest.php 2007-05-25 23:35:50 UTC (rev 532)
@@ -0,0 +1,58 @@
+<?php
+
+require_once dirname(__FILE__) . '/../../../config.php';
+require_once 'Argil/Util/Template/CamelCapsToUnderscore.php';
+
+class Argil_Util_Template_CamelCapsToUnderscoreTest extends UnitTestCase
+{
+ public function testConvertsCamelCapsToUnderscoredStrings()
+ {
+ $template = new Argil_Util_Template_CamelCapsToUnderscore('CamelCaps');
+ $this->assertEqual((string)$template, 'camel_caps');
+ unset($template);
+
+ $template = new Argil_Util_Template_CamelCapsToUnderscore('CamelCapsButLonger');
+ $this->assertEqual((string)$template, 'camel_caps_but_longer');
+ }
+
+ public function testRequiresANonEmptyStringAsOnlyParameterInConstruct()
+ {
+ new Argil_Util_Template_CamelCapsToUnderscore('non-empty');
+ $this->pass('exception not thrown');
+
+ try {
+ new Argil_Util_Template_CamelCapsToUnderscore('');
+ $this->fail('exception not caught');
+ } catch(Argil_Util_Template_CamelCapsToUnderscore_Exception $e) {
+ $this->pass('exception caught');
+ }
+
+ try {
+ new Argil_Util_Template_CamelCapsToUnderscore(1);
+ $this->fail('exception not caught');
+ } catch(Argil_Util_Template_CamelCapsToUnderscore_Exception $e) {
+ $this->pass('exception caught');
+ }
+
+ try {
+ new Argil_Util_Template_CamelCapsToUnderscore(array());
+ $this->fail('exception not caught');
+ } catch(Argil_Util_Template_CamelCapsToUnderscore_Exception $e) {
+ $this->pass('exception caught');
+ }
+
+ try {
+ new Argil_Util_Template_CamelCapsToUnderscore(123.321);
+ $this->fail('exception not caught');
+ } catch(Argil_Util_Template_CamelCapsToUnderscore_Exception $e) {
+ $this->pass('exception caught');
+ }
+
+ try {
+ new Argil_Util_Template_CamelCapsToUnderscore($this);
+ $this->fail('exception not caught');
+ } catch(Argil_Util_Template_CamelCapsToUnderscore_Exception $e) {
+ $this->pass('exception caught');
+ }
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|