PayU integration by Krzysztof Gzocha
  • Namespace
  • Class

Namespaces

  • Team3
    • PayU
      • Annotation
      • Communication
        • CurlRequestBuilder
        • HttpStatusParser
        • Notification
        • Process
          • NotificationProcess
          • ResponseDeserializer
        • Request
          • Model
        • Response
          • Model
        • Sender
      • Configuration
        • Credentials
      • Order
        • Autocomplete
          • Strategy
        • Model
          • Buyer
          • Money
          • Products
          • ShippingMethods
          • Traits
        • Transformer
          • UserOrder
            • Strategy
              • Product
              • ShippingMethod
      • PropertyExtractor
        • Reader
      • Serializer
      • SignatureCalculator
        • Encoder
          • Algorithms
          • Strategy
        • ParametersSorter
        • Validator
      • ValidatorBuilder

Classes

  • Team3\PayU\ValidatorBuilder\ValidatorBuilder

Interfaces

  • Team3\PayU\ValidatorBuilder\ValidatorBuilderInterface
  1 <?php
  2 /**
  3  * @author Krzysztof Gzocha <krzysztof.gzocha@xsolve.pl>
  4  */
  5 namespace Team3\PayU\Order\Model\Money;
  6 
  7 use Symfony\Component\Validator\Constraints as Assert;
  8 
  9 /**
 10  * All money information will be stored and manipulated in this class.
 11  * 
 12  * Class Money
 13  * @package Team3\PayU\Order\Model\Money
 14  */
 15 class Money implements MoneyInterface
 16 {
 17     /**
 18      * @var double
 19      * @Assert\GreaterThan(0)
 20      */
 21     protected $value;
 22 
 23     /**
 24      * @var string
 25      * @Assert\Length(min="3", max="3")
 26      */
 27     protected $currency;
 28 
 29     /**
 30      * @var int
 31      * @Assert\GreaterThan(0)
 32      */
 33     protected $precision;
 34 
 35     /**
 36      * @param double $value
 37      * @param string $currency
 38      * @param int    $precision
 39      *
 40      * @throws WrongMoneyValueException
 41      */
 42     public function __construct($value, $currency = null, $precision = 2)
 43     {
 44         $this->checkValueValidity($value);
 45 
 46         $this->value = $value;
 47         $this->currency = $currency;
 48         $this->precision = $precision;
 49     }
 50 
 51     /**
 52      * @return string
 53      */
 54     public function __toString()
 55     {
 56         if (null === $this->currency) {
 57             return (string) round($this->value, $this->precision);
 58         }
 59 
 60         return sprintf(
 61             '%s %s',
 62             round($this->value, $this->precision),
 63             $this->currency
 64         );
 65     }
 66 
 67     /**
 68      * @inheritdoc
 69      */
 70     public function getValue()
 71     {
 72         return (double) $this->value;
 73     }
 74 
 75     /**
 76      * When precision is set to 2 this method will transforms 12.34 into 1234.
 77      * @param int $precision
 78      *
 79      * @return int
 80      */
 81     public function getValueWithoutSeparation($precision = 2)
 82     {
 83         return (int) sprintf('%d', ($this->getValue() * (double) pow(10, $precision)));
 84     }
 85 
 86     /**
 87      * @param MoneyInterface $money
 88      *
 89      * @return MoneyInterface
 90      */
 91     public function add(MoneyInterface $money)
 92     {
 93         return new self(
 94             (double) ($this->getValue() + $money->getValue()),
 95             $this->currency,
 96             $this->precision
 97         );
 98     }
 99 
100     /**
101      * @param double $multiplier
102      *
103      * @return MoneyInterface
104      */
105     public function multiply($multiplier)
106     {
107         return new self(
108             (double) ($this->getValue() * $multiplier),
109             $this->currency,
110             $this->precision
111         );
112     }
113 
114     /**
115      * @param double $value
116      *
117      * @throws WrongMoneyValueException
118      */
119     protected function checkValueValidity($value)
120     {
121         if (!is_numeric($value)) {
122             throw new WrongMoneyValueException(sprintf(
123                 'Value passed to %s should be numeric, but is %s',
124                 get_class($this),
125                 gettype($value)
126             ));
127         }
128     }
129 }
130 
PayU integration by Krzysztof Gzocha API documentation generated by ApiGen