Skip to end of metadata
Go to start of metadata

<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 Framework: Zend_View_Helper_Currency Component Proposal

Proposed Component Name Zend_View_Helper_Currency
Developer Notes http://framework.zend.com/wiki/display/ZFDEV/Zend_View_Helper_Currency
Proposers Thomas Weidner
Zend Liaison TBD
Revision 1.0 - 7 April 2009: Initial Draft (wiki revision: 6)

Table of Contents

1. Overview

Zend_View_Helper_Currency is a view helper to use Zend_Currency within a view script.

2. References

3. Component Requirements, Constraints, and Acceptance Criteria

  • This component will display currencies within your view
  • It supports having the used currency within the registry
  • It supports using the default currency which is detected by the users browser locale when possible

4. Dependencies on Other Framework Components

  • Zend_Currency
  • Zend_Locale

5. Theory of Operation

Zend_View_Helper_Currency allows to use defined currencies within your view. It allows to use a registry key (Zend_Currency) which can be set application wide. When giving a numeric input (integer or float) the displayed value will always be a currency notation according to the used settings

6. Milestones / Tasks

  • Milestone 1: [DONE] Proposal finished
  • Milestone 2: [DONE] Proposal accepted
  • Milestone 3: [DONE] Working implementation
  • Milestone 4: [DONE] Unit tests
  • Milestone 5: [DONE] Documentation
  • Milestone 6: [DONE] Moved to core

7. Class Index

  • Zend_View_Helper_Currency

8. Use Cases

UC-01

Set the currency to use within the registry

Now you can use the view helper without any initiation or parameter

UC-02

Create the view helper manually

UC-03

Initiate the view on the fly

PS: Not sure if the first line is correct, but there is a way to call view helper function from within the view

9. Class Skeletons

]]></ac:plain-text-body></ac:macro>

]]></ac:plain-text-body></ac:macro>

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

    <p>+1</p>

    1. Jan 26, 2009

      <p>LOL... you should wait for me to write anything before voting for this proposal <ac:emoticon ac:name="wink" /></p>

  2. Jan 26, 2009

    <p>You can't possibly write anything wrong! <ac:emoticon ac:name="wink" /><br />
    I assumed this view helper will transform floats into a formatted string, correct?</p>

  3. Mar 18, 2009

    <p>This really would be helpful. Localizing a currency in the view seems like the most logical place to do it. You pass through the raw currency amounts from the model/controller and the view handles adding the currency symbol, setting the precision, etc.</p>

    <p>Thomas, is this what you had in mind for this proposal?</p>

  4. Apr 07, 2009

    <p>Looks good. Will the helper round amounts to the nearest penny/cent etc., or truncate? E.g. would:</p>

    <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
    $this->currency->setCurrency('en_US');
    echo $this->currency(10.89999);
    ]]></ac:plain-text-body></ac:macro>

    <p>show $10.89 or $10.90? Perhaps this could be optional as well?</p>

    <p>Edit: Actually I guess the helper will do whatever Zend_Currency already does, so ignore that question!</p>

    1. Apr 07, 2009

      <p>You should take a look at Zend_Currency...<br />
      This is only a view helper which makes use of Zend_Currency.</p>

      <p>All possibilities of Zend_Currency can also be used within this view helper.</p>

      <p>For clearification:<br />
      Currencies define a default precission which is mainly 2 but some define 3 or 4.<br />
      You can still override this setting by giving your own options within Zend_Currency.</p>

  5. Apr 08, 2009

    <p>+1 for this. Looks really good.</p>

    <p>Maby one small addition would be very usefull. The official way of writing currencies in euro is € 12,00. In your use case you set 12,00 EUR. It would be very handy if it support both writing methods. </p>

    1. Apr 08, 2009

      <p>As already said:<br />
      Please take a look at Zend_Currency. All what was said until now is already supported and also documented since several releases. <ac:emoticon ac:name="wink" /></p>

      <p>This proposal describes just a view helper which adds the possibility to use Zend_Currency within the view in a simple manner.</p>

  6. Apr 08, 2009

    <p>First, what about applications that require multiple currencies to be displayed in one view? There is no example on how that would be used.</p>

    <p>Second, i don't like the global state via Zend_Registry and a magic key Zend_Currency. Its in no way obvious or something that the Zend_Currency key holds the default currency.</p>

    <p>Finally, this view helper would be obsolete if a Zend had a money value object, that has a __toString() method and an attached currency. This would be way more simple to use.</p>

    1. Apr 08, 2009

      <blockquote>
      <p>First, what about applications that require multiple currencies to be displayed in one view? There is no example on how that would be used.</p></blockquote>
      <p>Create a currency object and attach the wished object/currency to your call of the view helper.</p>
      <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
      $curr1 = new Zend_Currency('EUR');
      $curr2 = new Zend_Currency('USD');
      //
      $this->currency(1234,$curr1);
      $this->currency(1234,$curr2);
      ]]></ac:plain-text-body></ac:macro>
      <p>I thought that this is simple enough visible through the API so it has not to be mentioned extra.</p>
      <blockquote>
      <p>Second, i don't like the global state via Zend_Registry and a magic key Zend_Currency. Its in no way obvious or something that the Zend_Currency key holds the default currency.</p></blockquote>
      <p> Even if you don't like the idea, this is common through many components within the framework. And this does not mean that you have to use this approach. It's just a simplification for the user when he has only one default currency for his shop for example.</p>
      <blockquote>
      <p>Finally, this view helper would be obsolete if a Zend had a money value object, that has a __toString() method and an attached currency. This would be way more simple to use.</p></blockquote>
      <p> Would / Could / Should...</p>

      <p>Fact is that there is actually no simple way to handle currenies within the view.<br class="atl-forced-newline" /></p>

      <p>And as already mentioned in past, would a Money object bring several other problems with it like conversion, precision, calculation and so on. Do you propose to negotate this proposal because we should erase Zend_Currency and make a Zend_Money instead ?<br class="atl-forced-newline" /></p>

      <p>My opinion is that having 30% improvement now is better than having 70% improvement in one year and nothing in the meantime.<br class="atl-forced-newline" /> <br class="atl-forced-newline" /></p>

      1. Apr 08, 2009

        <p>Well, if you have to attach currency objects as second parameter to the view helper for this use, case then the view helper is quite useless imho. if you call:</p>

        <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
        $curr = new Zend_Currency('USD');
        echo $this->currency(1234,$curr);
        ]]></ac:plain-text-body></ac:macro>

        <p>or</p>

        <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
        $curr = new Zend_Currency('USD');
        echo $curr->toCurrency(1234);
        ]]></ac:plain-text-body></ac:macro>

        <p>makes no difference for me. Also in both cases you instantiate an object in the view for it, which is a bad practice imho. Therefore in a multi currency enviroment, this helper wouldn't help much.</p>

        <p>In the single currency case, you could call this two liners also or grab the currency from the registry. This is not really overhead compared to adding a view helper. Its also not 30% improvement, its one line improvement, which is nothing for me. Think of the costs of learning about the view helper.</p>

        <p>To the point 30% / 70%. Imho adding more and more features for a component that has serious implementation drawbacks (being currency is not tied with money), should not be driven for more and more features that lead to a deadlock of the component being tied in so much into the whole code-base that you cannot replace it, although you really want to. So yes i say this proposal should be erased, a component should be developed that combines currency and money in a coherent way, then the need for yet another view helper goes away.</p>

        <p>To conversion, precision, calculation and such: Don't you deal with that in toCurrency already? Thats what the function is doing i thought. Although that is what object oriented type system is for, the money class is supposed to aid you in that stuff. Imho its not an argument to say there is a problem with these issues, so we don't implement it and all the users that really need this functionality have to build it for themselves.</p>

        1. Apr 08, 2009

          <p>btw, an additional use of Zend_Currency would be:</p>

          <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
          $view->assign('euro', new Zend_Currency('EUR'));
          $view->assign('usd', new Zend_Currency('USD'));
          ]]></ac:plain-text-body></ac:macro>

          <p>--view:</p>

          <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
          <?php echo $this->euro->toCurrency(1234); ?>
          <?php echo $this->usd->toCurrency(1234); ?>
          ]]></ac:plain-text-body></ac:macro>

          <p>isn't that several times more explicit than the view helper?</p>

          1. Aug 07, 2009

            <p>As far as I know according to your idea each currency must have a view variable for each currency you will use, imho, that's not so flexible because you'll have to edit the view script (and the controller) each time you need a different currency.</p>

            <p>Problems could arise when you register every currency variable to the view script because you will have to check in the view script for the existence of that currency in the script variable-space.</p>

            <p>Once I wrote a currency helper for an application that uses an instance of Zend_Currency stored in a Registry (Zend_Registry) for converting numbers to currencies. In this mean I can override the instance of Zend_Currency in the registry with the one chosen by the user:</p>

            <p>Application-wide bootstrap:</p>

            <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
            <?php

            // Hard coded
            Zend_Registry::set('currency', new Zend_Currency('EUR'));

            // Using Zend_Config
            $config = Zend_Config_Ini('path/to/config.ini');
            Zend_Registry::set('currency', $config->currency);
            ]]></ac:plain-text-body></ac:macro>
            <p>User preferences:</p>
            <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
            <?php

            $userPreferences = $user->getPreferences();
            Zend_Registry::set('currency', $userPreferences->getCurrency());
            ]]></ac:plain-text-body></ac:macro>
            <p>And I could use the helper simply calling the Currency and passing the number to the direct method:</p>
            <ac:macro ac:name="code"><ac:plain-text-body><![CDATA[
            <p><?php echo $this->currency(1234) ?></p>
            ]]></ac:plain-text-body></ac:macro>
            <p><br class="atl-forced-newline" /></p>

            <p>I liked the idea of injecting a Zend_Currency object into the currency call.</p>

            <p>Btw, I'm happy to see that this proposal has been accepted in the Standard Incubator <ac:emoticon ac:name="laugh" /></p>

            1. Aug 08, 2009

              <p>But this approach would still not help with the problem mentioned before.</p>

              <p>You can only access one application wide currency. When you have more than one then you would have to set them within your view script.</p>

              <p>As you see in use case 1 we integrate a application wide currency.</p>

        2. Apr 08, 2009

          <blockquote>
          <p>Also in both cases you instantiate an object in the view for it, which is a bad practice imho.</p></blockquote>
          <p>You know that it's not necessary to initiate a object within the view. It was just an example to let you see how this can work. The first two lines could be seen as within the bootstrap. You can also just give 'USD' as second parameter to the view helper or any other option Zend_Currency also uses. Why should this be a bad practice ?</p>

          <p>You could also save your wished currencies into the registry and access them from there.</p>
          <blockquote>
          <p>Therefore in a multi currency enviroment, this helper wouldn't help much.</p></blockquote>
          <p>You don't need to use it. You can still initiate multiple instances from within your view to get what you want to have differently. This is just a way to simplify things.</p>
          <blockquote>
          <p>Its also not 30% improvement, its one line improvement, which is nothing for me. Think of the costs of learning about the view helper.</p></blockquote>
          <p>Just because you don't know the benefit of View Helpers does not mean that others, who already use this way, should not have the benefit of having a currency view helper.</p>

          <p> I think your general problem is that you don't like the way how Zend_Currency works, and you think there is no benefit from using it. You would like to have a java style money class which is able to convert based on any bank exchange service.</p>

          <p>There were proposals in past which wanted to add such behaviour, but as long as there is no exchange service available there is no reason for me to add such functionallity to Zend_Currency.</p>

          <p>But again... this is another work and has nothing to do with this proposal. We should only discuss the pro and con of the here written proposal to come to an conclusion if it is worth to be added or not.</p>

          <p>So actually we have +5 and -1. <ac:emoticon ac:name="smile" /></p>

  7. Jul 14, 2009

    <ac:macro ac:name="note"><ac:parameter ac:name="title">Zend Framework Approval</ac:parameter><ac:rich-text-body>
    <p>This proposal is accepted for immediate development in the Standard Incubator, as-is.</p></ac:rich-text-body></ac:macro>

  8. Aug 04, 2009

    <p>When we can see a first version in the incubator ?</p>