Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Abstract.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Locale.php';
26 
30 #require_once 'Zend/Locale/Math.php';
31 
35 #require_once 'Zend/Locale/Format.php';
36 
46 abstract class Zend_Measure_Abstract
47 {
53  protected $_value;
54 
60  protected $_type;
61 
67  protected $_locale = null;
68 
72  protected $_units = array();
73 
82  public function __construct($value, $type = null, $locale = null)
83  {
84  if (($type !== null) and (Zend_Locale::isLocale($type, null, false))) {
85  $locale = $type;
86  $type = null;
87  }
88 
89  $this->setLocale($locale);
90  if ($type === null) {
91  $type = $this->_units['STANDARD'];
92  }
93 
94  if (isset($this->_units[$type]) === false) {
95  #require_once 'Zend/Measure/Exception.php';
96  throw new Zend_Measure_Exception("Type ($type) is unknown");
97  }
98 
99  $this->setValue($value, $type, $this->_locale);
100  }
101 
107  public function getLocale()
108  {
109  return $this->_locale;
110  }
111 
119  public function setLocale($locale = null, $check = false)
120  {
121  if (empty($locale)) {
122  #require_once 'Zend/Registry.php';
123  if (Zend_Registry::isRegistered('Zend_Locale') === true) {
124  $locale = Zend_Registry::get('Zend_Locale');
125  }
126  }
127 
128  if ($locale === null) {
129  $locale = new Zend_Locale();
130  }
131 
132  if (!Zend_Locale::isLocale($locale, true, false)) {
133  if (!Zend_Locale::isLocale($locale, false, false)) {
134  #require_once 'Zend/Measure/Exception.php';
135  throw new Zend_Measure_Exception("Language (" . (string) $locale . ") is unknown");
136  }
137 
138  $locale = new Zend_Locale($locale);
139  }
140 
141  if (!$check) {
142  $this->_locale = (string) $locale;
143  }
144  return $this;
145  }
146 
155  public function getValue($round = -1, $locale = null)
156  {
157  if ($round < 0) {
158  $return = $this->_value;
159  } else {
160  $return = Zend_Locale_Math::round($this->_value, $round);
161  }
162 
163  if ($locale !== null) {
164  $this->setLocale($locale, true);
165  return Zend_Locale_Format::toNumber($return, array('locale' => $locale));
166  }
167 
168  return $return;
169  }
170 
180  public function setValue($value, $type = null, $locale = null)
181  {
182  if (($type !== null) and (Zend_Locale::isLocale($type, null, false))) {
183  $locale = $type;
184  $type = null;
185  }
186 
187  if ($locale === null) {
188  $locale = $this->_locale;
189  }
190 
191  $this->setLocale($locale, true);
192  if ($type === null) {
193  $type = $this->_units['STANDARD'];
194  }
195 
196  if (empty($this->_units[$type])) {
197  #require_once 'Zend/Measure/Exception.php';
198  throw new Zend_Measure_Exception("Type ($type) is unknown");
199  }
200 
201  try {
202  $value = Zend_Locale_Format::getNumber($value, array('locale' => $locale));
203  } catch(Exception $e) {
204  #require_once 'Zend/Measure/Exception.php';
205  throw new Zend_Measure_Exception($e->getMessage(), $e->getCode(), $e);
206  }
207 
208  $this->_value = $value;
209  $this->setType($type);
210  return $this;
211  }
212 
218  public function getType()
219  {
220  return $this->_type;
221  }
222 
230  public function setType($type)
231  {
232  if (empty($this->_units[$type])) {
233  #require_once 'Zend/Measure/Exception.php';
234  throw new Zend_Measure_Exception("Type ($type) is unknown");
235  }
236 
237  if (empty($this->_type)) {
238  $this->_type = $type;
239  } else {
240  // Convert to standard value
242  if (is_array($this->_units[$this->getType()][0])) {
243  foreach ($this->_units[$this->getType()][0] as $key => $found) {
244  switch ($key) {
245  case "/":
246  if ($found != 0) {
248  }
249  break;
250  case "+":
252  break;
253  case "-":
255  break;
256  default:
258  break;
259  }
260  }
261  } else {
262  $value = call_user_func(Zend_Locale_Math::$mul, $value, $this->_units[$this->getType()][0], 25);
263  }
264 
265  // Convert to expected value
266  if (is_array($this->_units[$type][0])) {
267  foreach (array_reverse($this->_units[$type][0]) as $key => $found) {
268  switch ($key) {
269  case "/":
271  break;
272  case "+":
274  break;
275  case "-":
277  break;
278  default:
279  if ($found != 0) {
281  }
282  break;
283  }
284  }
285  } else {
286  $value = call_user_func(Zend_Locale_Math::$div, $value, $this->_units[$type][0], 25);
287  }
288 
289  $slength = strlen($value);
290  $length = 0;
291  for($i = 1; $i <= $slength; ++$i) {
292  if ($value[$slength - $i] != '0') {
293  $length = 26 - $i;
294  break;
295  }
296  }
297 
298  $this->_value = Zend_Locale_Math::round($value, $length);
299  $this->_type = $type;
300  }
301  return $this;
302  }
303 
310  public function equals($object)
311  {
312  if ((string) $object == $this->toString()) {
313  return true;
314  }
315 
316  return false;
317  }
318 
326  public function toString($round = -1, $locale = null)
327  {
328  if ($locale === null) {
329  $locale = $this->_locale;
330  }
331 
332  return $this->getValue($round, $locale) . ' ' . $this->_units[$this->getType()][1];
333  }
334 
340  public function __toString()
341  {
342  return $this->toString();
343  }
344 
350  public function getConversionList()
351  {
352  return $this->_units;
353  }
354 
363  public function convertTo($type, $round = 2, $locale = null)
364  {
365  $this->setType($type);
366  return $this->toString($round, $locale);
367  }
368 
375  public function add($object)
376  {
377  $object->setType($this->getType());
378  $value = $this->getValue(-1) + $object->getValue(-1);
379 
380  $this->setValue($value, $this->getType(), $this->_locale);
381  return $this;
382  }
383 
390  public function sub($object)
391  {
392  $object->setType($this->getType());
393  $value = $this->getValue(-1) - $object->getValue(-1);
394 
395  $this->setValue($value, $this->getType(), $this->_locale);
396  return $this;
397  }
398 
405  public function compare($object)
406  {
407  $object->setType($this->getType());
408  $value = $this->getValue(-1) - $object->getValue(-1);
409 
410  if ($value < 0) {
411  return -1;
412  } else if ($value > 0) {
413  return 1;
414  }
415 
416  return 0;
417  }
418 }
setLocale($locale=null, $check=false)
Definition: Abstract.php:119
static $sub
Definition: Math.php:41
toString($round=-1, $locale=null)
Definition: Abstract.php:326
static $mul
Definition: Math.php:43
static $add
Definition: Math.php:40
$type
Definition: item.phtml:13
static isRegistered($index)
Definition: Registry.php:178
$value
Definition: gender.phtml:16
static $div
Definition: Math.php:44
static isLocale($locale, $strict=false, $compatible=true)
Definition: Locale.php:1683
getValue($round=-1, $locale=null)
Definition: Abstract.php:155
__construct($value, $type=null, $locale=null)
Definition: Abstract.php:82
convertTo($type, $round=2, $locale=null)
Definition: Abstract.php:363
setValue($value, $type=null, $locale=null)
Definition: Abstract.php:180
static toNumber($value, array $options=array())
Definition: Format.php:300
static round($op1, $precision=0)
Definition: Math.php:65
$i
Definition: gallery.phtml:31
static getNumber($input, array $options=array())
Definition: Format.php:246
static get($index)
Definition: Registry.php:141