Permalink
Browse files

removed function `craue_substr` in favor of Twig's `slice` filter

  • Loading branch information...
craue committed Feb 17, 2012
1 parent 6f83737 commit 8559e7b3903b04368d444372aa963921a4554f5f
View
@@ -40,10 +40,7 @@ children) has any errors.
## StringHelperExtension
-Provides the `craue_substr` function wrapping PHP's `substr` function. (As of Twig 1.6, there's a `slice` filter you
-should use instead.)
-
-Also provides the `craue_trailingDot` filter for ensuring that a text ends with a dot.
+Provides the `craue_trailingDot` filter for ensuring that a text ends with a dot.
This comes in handy when using error messages (e.g. for validation) of vendor bundles (which are written like sentences
but are missing the trailing dots) together with your own ones (which should include the trailing dot).
@@ -133,11 +130,6 @@ Or by using the `deps` file:
## StringHelperExtension
- {{ craue_substr('bla', 2) }} will print "a"<br />
- {{ craue_substr('bla', 0, 1) }} will print "b"<br />
- {{ craue_substr('bla', 1, 1) }} will print "l"<br />
- {{ craue_substr('bla', 1, 2) }} will print "la"
-
{{ aString | craue_trailingDot }}<br />
{{ 'This text should end with a dot' | craue_trailingDot }}<br />
{{ 'This text should end with exactly one dot.' | craue_trailingDot }}
@@ -248,7 +240,6 @@ can do it intentionally.
### StringHelperExtension
; app/config/parameters.ini
- craue_twig_extensions.stringHelper.substrAlias="substr"
craue_twig_extensions.stringHelper.trailingDotAlias="trailingDot"
### FormatDateTimeExtension
@@ -11,15 +11,13 @@
<parameters>
<parameter key="craue_twig_extensions.stringHelper.class">Craue\TwigExtensionsBundle\Twig\Extension\StringHelperExtension</parameter>
<parameter key="craue_twig_extensions.stringHelper.trailingDotAlias" />
- <parameter key="craue_twig_extensions.stringHelper.substrAlias" />
</parameters>
<services>
<service id="twig.extension.craue_stringHelper" class="%craue_twig_extensions.stringHelper.class%">
<tag name="twig.extension" />
<call method="setAliases">
<argument>%craue_twig_extensions.stringHelper.trailingDotAlias%</argument>
- <argument>%craue_twig_extensions.stringHelper.substrAlias%</argument>
</call>
</service>
</services>
@@ -1,5 +0,0 @@
-{%- if length | default(null) is not null -%}
- {{- craue_substr(value, start, length) -}}
-{%- else -%}
- {{- craue_substr(value, start) -}}
-{%- endif -%}
@@ -34,43 +34,4 @@ public function testAddTrailingDot() {
}
}
- public function testSubstr() {
- $cases = array(
- array(
- 'value' => 'bla',
- 'start' => 2,
- 'length' => null,
- 'result' => 'a',
- ),
- array(
- 'value' => 'bla',
- 'start' => 0,
- 'length' => 1,
- 'result' => 'b',
- ),
- array(
- 'value' => 'bla',
- 'start' => 1,
- 'length' => 1,
- 'result' => 'l',
- ),
- array(
- 'value' => 'bla',
- 'start' => 1,
- 'length' => 2,
- 'result' => 'la',
- ),
- );
-
- foreach ($cases as $index => $case) {
- $this->assertSame($case['result'],
- $this->getTwig()->render('IntegrationTestBundle:StringHelper:substr.html.twig', array(
- 'value' => $case['value'],
- 'start' => $case['start'],
- 'length' => $case['length'],
- )),
- 'test case with index '.$index);
- }
- }
-
}
@@ -21,22 +21,13 @@ class StringHelperExtension extends \Twig_Extension {
*/
protected $trailingDotAlias = null;
- /**
- * @var string
- */
- protected $substrAlias = null;
-
/**
* @param string $trailingDotAlias Alias for the trailingDot filter.
- * @param string $substrAlias Alias for the substr filter.
*/
- public function setAliases($trailingDotAlias = null, $substrAlias = null) {
+ public function setAliases($trailingDotAlias = null) {
if (!empty($trailingDotAlias)) {
$this->trailingDotAlias = $trailingDotAlias;
}
- if (!empty($substrAlias)) {
- $this->substrAlias = $substrAlias;
- }
}
/**
@@ -61,21 +52,6 @@ public function getFilters() {
return $filters;
}
- /**
- * {@inheritDoc}
- */
- public function getFunctions() {
- $functions = array();
-
- $substrFunction = new \Twig_Function_Function('substr');
- $functions['craue_substr'] = $substrFunction;
- if (!empty($this->substrAlias)) {
- $functions[$this->substrAlias] = $substrFunction;
- }
-
- return $functions;
- }
-
/**
* This will NOT remove any trailing dots, i.e. won't change "There must be something more...".
* @param string $value Text possibly containing a trailing dot.

0 comments on commit 8559e7b

Please sign in to comment.