<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[
<ac:macro ac:name="unmigrated-inline-wiki-markup"><ac:plain-text-body><![CDATA[
Zend_Filter_StringLength is a filter which allows to pad/extend or truncate a string to a given length. It will also have a view helper which allows to use this filter easily within your view.Zend Framework: Zend_Filter_StringLength Component Proposal
Proposed Component Name
Zend_Filter_StringLength
Developer Notes
http://framework.zend.com/wiki/display/ZFDEV/Zend_Filter_StringLength
Proposers
Thomas Weidner
Zend Liaison
TBD
Revision
1.0 - 12 April 2009: Initial Draft
1.1 - 13 April 2009: added View Helper (wiki revision: 8)Table of Contents
1. Overview
2. References
3. Component Requirements, Constraints, and Acceptance Criteria
4. Dependencies on Other Framework Components
- Zend_Filter
- Zend_View_Helper
5. Theory of Operation
This component allows to pad/extend a string to a given length the same way as str_pad would do. And to truncate a string to a given length as substr allows to.
It will additionally allow to seperate text based on words by using eighter whitespaces, hypens or any other character.
And it adds a view helper which allows to use this filter within your view. The View Helper will also allow to set the filter within a registry using the name 'Zend_Filter_StringLength'.
6. Milestones / Tasks
- Milestone 1: [DONE] Proposal finished
- Milestone 2: Proposal accepted
- Milestone 3: Working implementation
- Milestone 4: Unit tests
- Milestone 5: Documentation
- Milestone 6: Moved to core
7. Class Index
- Zend_Filter_StringLength
- Zend_View_Helper_StringLength
8. Use Cases
| UC-01 |
|---|
Pad a string to 15 chars
| UC-02 |
|---|
Pad a string to 15 chars, using another pad char
| UC-03 |
|---|
Pad a string to 15 chars, using another pad char and another direction
| UC-04 |
|---|
Truncate a string at 6 chars
| UC-05 |
|---|
Truncate a string at 6 chars, using ending chars
| UC-06 |
|---|
Define padding and truncating at the same time
| UC-07 |
|---|
Define truncating based on words
| UC-08 |
|---|
Using the viewhelper
| UC-09 |
|---|
Define truncating based on different trunc-characters
| UC-10 |
|---|
Using the viewhelper with registry
17 Comments
comments.show.hideApr 12, 2009
Ben Scholzen
<p>For padding multibyte strings (e.g. utf-8) this filter should use Zend_Text_MultiByte::strPad() instead of PHP's str_pad() function.</p>
Apr 13, 2009
Thomas Weidner
<p>I never said that it will use internally php's str_pad <ac:emoticon ac:name="wink" /></p>
Apr 13, 2009
Ben Scholzen
<p>So what will it use then?</p>
Apr 13, 2009
Thomas Weidner
<p>Ben:<br />
The point is, that actually there exists no code... only in my brain.<br />
So the details of which class/extension/component to use for this sub-task (there are several sub-tasks which have to be done) will be decided when I evaluate how this filter should work internally (beta-class).</p>
<p>It can use this component or another... I have not decided or fixed any decission for now. This will be done and discussed when this proposal has been approved in general.</p>
Apr 12, 2009
Dolf Schimmel (Freeaqingme)
<p>Looks like a great component!</p>
<p>I do have a first questions though;<br />
1. How will this handle multibyte strings?<br />
2. I would like an option that ensures words themself to not get cut somewhere half way. On IRC you told me to look at UC5, but obviously it isn't in that usecase?<br />
3. Will you add a viewhelper along with this filter? It seems to me that a lot of users want to 'cut strings' when presenting the data, rather than doing it while inserting?</p>
Apr 13, 2009
Thomas Weidner
<p>1.) As normal strings, this component will also handle multibyte strings</p>
<p>2.) You didn't speak of words, but of a way to have not only a single character added when truncating. Truncating words is possible, but will use a seperate constant. I'll add a new usecase. But this possibility is limited to truncate based on whitespaces, and not one word in two seperated because there is no way to define a exact rule where a word has to be seperated</p>
<p>3.) No, because this is a seperate proposal and has nothing to do with this filter. As soon as this filter is available we can simply add a view helper using this filter.</p>
Apr 13, 2009
Matthew Weier O'Phinney
<p>I'd argue that adding a view helper that consumes this filter should be part of this proposal. It's a common use case for the filter.</p>
Apr 13, 2009
Thomas Weidner
<p>Are you sure ?</p>
<p>Because a view helper is another component theoretical independent from this filter (or this component independently from the view helper <ac:emoticon ac:name="smile" /> )</p>
Apr 14, 2009
Kamil Nowakowski
<p>I`d agree with Matthew , It is a popular use case to use it in a view <br />
see <a class="external-link" href="http://smarty.net/manual/en/language.modifier.truncate.php">http://smarty.net/manual/en/language.modifier.truncate.php</a></p>
Apr 14, 2009
Thomas Weidner
<p>Probably... but as this is already part of this proposal since more than a day, what's the reason of this additional comment ?</p>
Apr 14, 2009
Kamil Nowakowski
<p>Sorry for that , I didn`t notice change</p>
Apr 14, 2009
Thomas Weidner
<p>No problem</p>
May 03, 2009
Marc Bennewitz (GIATA mbH)
<p>I think it would be better to split this in two filters:<br />
Zend_Filter_StringPad<br />
Zend_Filter_StringTruncate</p>
<p>than it is selectable if you would only truncate or pad a string:</p>
<p>e.g.:</p>
<ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
// truncate a string if it is longer than length and add fill
$filter = new Zend_Filter_StringTruncate(array('length' => 10, 'fill' => '...', 'type' => Zend_Filter_StringTruncate::PAD_RIGTH));
$filter->filter('This is a test'); // This is...
// pad a string if it is smaller than length
$filter = new Zend_Filter_StringPad(array('length' => 10, 'fill' => '.', 'type' => Zend_Filter_StringPad::PAD_RIGTH));
$filter->filter('This is a test'); // This is a test
// both and pad left
$filter = new Filter();
$filter->addFilter(new Zend_Filter_StringTruncate(array('length' => 10, 'fill' => '...', 'type' => Zend_Filter_StringTruncate::PAD_RIGTH)));
$filter->addFilter(new Zend_Filter_StringPad(array('length' => 20, 'fill' => '.', 'type' => Zend_Filter_StringPad::PAD_LEFT)));
$filter->filter('This is a test'); // ..........This is...
]]></ac:plain-text-body></ac:macro>
May 03, 2009
Thomas Weidner
<p>It is already possible to solve this with the above described API.</p>
<p>You can eighter pad or truncate or do both actions with the same filter.<br />
You can also use two filter instances and pad on both sides with different length as described by your example, even if I would not see a real usecase for such a task.</p>
Jun 08, 2009
Marc Bennewitz (private)
<p>removed</p>
Jul 14, 2009
Matthew Weier O'Phinney
<ac:macro ac:name="note"><ac:parameter ac:name="title">Declined in current state</ac:parameter><ac:rich-text-body>
<p>The Zend Framework team declines acceptance of this proposal in its current state. The component is poorly named, and perhaps performs too many different, discrete tasks. From our review, it:</p>
<ul>
<li>Pads a string (StringPad would be a good name)</li>
<li>Truncates a string (StringTruncate would be a good name)</li>
<li>Truncates based on words (WordTruncate would be a good name)<br />
Our recommendation is that these be three distinct filters. Developers may chain them together to get the combined behavior shown in some of the use cases.</li>
</ul>
<p>Please re-submit the proposal once you have made the above changes.</p></ac:rich-text-body></ac:macro>
Jul 23, 2009
Thomas Weidner
<p>Question:<br />
Does this also mean to add 3 different view helpers ? One for each filter ?</p>