Skip to end of metadata
Go to start of metadata

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: 7)

Table of Contents

1. Overview

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.

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

  • This component will pad a string to a given length
  • This component will allow to pad/extend a string on both sides
  • This component will allow to use any character to pad the string
  • This component will truncate a string at a given length
  • This component will only work on strings

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

9. Class Skeletons

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Apr 12, 2009

    For padding multibyte strings (e.g. utf-8) this filter should use Zend_Text_MultiByte::strPad() instead of PHP's str_pad() function.

    1. Apr 13, 2009

      I never said that it will use internally php's str_pad

      1. Apr 13, 2009

        So what will it use then?

        1. Apr 13, 2009

          Ben:
          The point is, that actually there exists no code... only in my brain.
          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).

          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.

  2. Apr 12, 2009

    Looks like a great component!

    I do have a first questions though;
    1. How will this handle multibyte strings?
    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?
    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?

    1. Apr 13, 2009

      1.) As normal strings, this component will also handle multibyte strings

      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

      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.

      1. Apr 13, 2009

        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.

        1. Apr 13, 2009

          Are you sure ?

          Because a view helper is another component theoretical independent from this filter (or this component independently from the view helper )

          1. Apr 14, 2009

            I`d agree with Matthew , It is a popular use case to use it in a view
            see http://smarty.net/manual/en/language.modifier.truncate.php

            1. Apr 14, 2009

              Probably... but as this is already part of this proposal since more than a day, what's the reason of this additional comment ?

              1. Apr 14, 2009

                Sorry for that , I didn`t notice change

                1. Apr 14, 2009

                  No problem

  3. May 03, 2009

    I think it would be better to split this in two filters:
    Zend_Filter_StringPad
    Zend_Filter_StringTruncate

    than it is selectable if you would only truncate or pad a string:

    e.g.:

    1. May 03, 2009

      It is already possible to solve this with the above described API.

      You can eighter pad or truncate or do both actions with the same filter.
      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.

      1. Jun 08, 2009

        removed

  4. Jul 14, 2009

    Declined in current state
    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:
    • Pads a string (StringPad would be a good name)
    • Truncates a string (StringTruncate would be a good name)
    • Truncates based on words (WordTruncate would be a good name)
      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.

    Please re-submit the proposal once you have made the above changes.

    1. Jul 23, 2009

      Question:
      Does this also mean to add 3 different view helpers ? One for each filter ?