IntlCaster.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Caster;
  11. use Symfony\Component\VarDumper\Cloner\Stub;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. * @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
  15. */
  16. class IntlCaster
  17. {
  18. public static function castMessageFormatter(\MessageFormatter $c, array $a, Stub $stub, $isNested)
  19. {
  20. $a += [
  21. Caster::PREFIX_VIRTUAL.'locale' => $c->getLocale(),
  22. Caster::PREFIX_VIRTUAL.'pattern' => $c->getPattern(),
  23. ];
  24. return self::castError($c, $a);
  25. }
  26. public static function castNumberFormatter(\NumberFormatter $c, array $a, Stub $stub, $isNested, $filter = 0)
  27. {
  28. $a += [
  29. Caster::PREFIX_VIRTUAL.'locale' => $c->getLocale(),
  30. Caster::PREFIX_VIRTUAL.'pattern' => $c->getPattern(),
  31. ];
  32. if ($filter & Caster::EXCLUDE_VERBOSE) {
  33. $stub->cut += 3;
  34. return self::castError($c, $a);
  35. }
  36. $a += [
  37. Caster::PREFIX_VIRTUAL.'attributes' => new EnumStub(
  38. [
  39. 'PARSE_INT_ONLY' => $c->getAttribute(\NumberFormatter::PARSE_INT_ONLY),
  40. 'GROUPING_USED' => $c->getAttribute(\NumberFormatter::GROUPING_USED),
  41. 'DECIMAL_ALWAYS_SHOWN' => $c->getAttribute(\NumberFormatter::DECIMAL_ALWAYS_SHOWN),
  42. 'MAX_INTEGER_DIGITS' => $c->getAttribute(\NumberFormatter::MAX_INTEGER_DIGITS),
  43. 'MIN_INTEGER_DIGITS' => $c->getAttribute(\NumberFormatter::MIN_INTEGER_DIGITS),
  44. 'INTEGER_DIGITS' => $c->getAttribute(\NumberFormatter::INTEGER_DIGITS),
  45. 'MAX_FRACTION_DIGITS' => $c->getAttribute(\NumberFormatter::MAX_FRACTION_DIGITS),
  46. 'MIN_FRACTION_DIGITS' => $c->getAttribute(\NumberFormatter::MIN_FRACTION_DIGITS),
  47. 'FRACTION_DIGITS' => $c->getAttribute(\NumberFormatter::FRACTION_DIGITS),
  48. 'MULTIPLIER' => $c->getAttribute(\NumberFormatter::MULTIPLIER),
  49. 'GROUPING_SIZE' => $c->getAttribute(\NumberFormatter::GROUPING_SIZE),
  50. 'ROUNDING_MODE' => $c->getAttribute(\NumberFormatter::ROUNDING_MODE),
  51. 'ROUNDING_INCREMENT' => $c->getAttribute(\NumberFormatter::ROUNDING_INCREMENT),
  52. 'FORMAT_WIDTH' => $c->getAttribute(\NumberFormatter::FORMAT_WIDTH),
  53. 'PADDING_POSITION' => $c->getAttribute(\NumberFormatter::PADDING_POSITION),
  54. 'SECONDARY_GROUPING_SIZE' => $c->getAttribute(\NumberFormatter::SECONDARY_GROUPING_SIZE),
  55. 'SIGNIFICANT_DIGITS_USED' => $c->getAttribute(\NumberFormatter::SIGNIFICANT_DIGITS_USED),
  56. 'MIN_SIGNIFICANT_DIGITS' => $c->getAttribute(\NumberFormatter::MIN_SIGNIFICANT_DIGITS),
  57. 'MAX_SIGNIFICANT_DIGITS' => $c->getAttribute(\NumberFormatter::MAX_SIGNIFICANT_DIGITS),
  58. 'LENIENT_PARSE' => $c->getAttribute(\NumberFormatter::LENIENT_PARSE),
  59. ]
  60. ),
  61. Caster::PREFIX_VIRTUAL.'text_attributes' => new EnumStub(
  62. [
  63. 'POSITIVE_PREFIX' => $c->getTextAttribute(\NumberFormatter::POSITIVE_PREFIX),
  64. 'POSITIVE_SUFFIX' => $c->getTextAttribute(\NumberFormatter::POSITIVE_SUFFIX),
  65. 'NEGATIVE_PREFIX' => $c->getTextAttribute(\NumberFormatter::NEGATIVE_PREFIX),
  66. 'NEGATIVE_SUFFIX' => $c->getTextAttribute(\NumberFormatter::NEGATIVE_SUFFIX),
  67. 'PADDING_CHARACTER' => $c->getTextAttribute(\NumberFormatter::PADDING_CHARACTER),
  68. 'CURRENCY_CODE' => $c->getTextAttribute(\NumberFormatter::CURRENCY_CODE),
  69. 'DEFAULT_RULESET' => $c->getTextAttribute(\NumberFormatter::DEFAULT_RULESET),
  70. 'PUBLIC_RULESETS' => $c->getTextAttribute(\NumberFormatter::PUBLIC_RULESETS),
  71. ]
  72. ),
  73. Caster::PREFIX_VIRTUAL.'symbols' => new EnumStub(
  74. [
  75. 'DECIMAL_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL),
  76. 'GROUPING_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL),
  77. 'PATTERN_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::PATTERN_SEPARATOR_SYMBOL),
  78. 'PERCENT_SYMBOL' => $c->getSymbol(\NumberFormatter::PERCENT_SYMBOL),
  79. 'ZERO_DIGIT_SYMBOL' => $c->getSymbol(\NumberFormatter::ZERO_DIGIT_SYMBOL),
  80. 'DIGIT_SYMBOL' => $c->getSymbol(\NumberFormatter::DIGIT_SYMBOL),
  81. 'MINUS_SIGN_SYMBOL' => $c->getSymbol(\NumberFormatter::MINUS_SIGN_SYMBOL),
  82. 'PLUS_SIGN_SYMBOL' => $c->getSymbol(\NumberFormatter::PLUS_SIGN_SYMBOL),
  83. 'CURRENCY_SYMBOL' => $c->getSymbol(\NumberFormatter::CURRENCY_SYMBOL),
  84. 'INTL_CURRENCY_SYMBOL' => $c->getSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL),
  85. 'MONETARY_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL),
  86. 'EXPONENTIAL_SYMBOL' => $c->getSymbol(\NumberFormatter::EXPONENTIAL_SYMBOL),
  87. 'PERMILL_SYMBOL' => $c->getSymbol(\NumberFormatter::PERMILL_SYMBOL),
  88. 'PAD_ESCAPE_SYMBOL' => $c->getSymbol(\NumberFormatter::PAD_ESCAPE_SYMBOL),
  89. 'INFINITY_SYMBOL' => $c->getSymbol(\NumberFormatter::INFINITY_SYMBOL),
  90. 'NAN_SYMBOL' => $c->getSymbol(\NumberFormatter::NAN_SYMBOL),
  91. 'SIGNIFICANT_DIGIT_SYMBOL' => $c->getSymbol(\NumberFormatter::SIGNIFICANT_DIGIT_SYMBOL),
  92. 'MONETARY_GROUPING_SEPARATOR_SYMBOL' => $c->getSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL),
  93. ]
  94. ),
  95. ];
  96. return self::castError($c, $a);
  97. }
  98. public static function castIntlTimeZone(\IntlTimeZone $c, array $a, Stub $stub, $isNested)
  99. {
  100. $a += [
  101. Caster::PREFIX_VIRTUAL.'display_name' => $c->getDisplayName(),
  102. Caster::PREFIX_VIRTUAL.'id' => $c->getID(),
  103. Caster::PREFIX_VIRTUAL.'raw_offset' => $c->getRawOffset(),
  104. ];
  105. if ($c->useDaylightTime()) {
  106. $a += [
  107. Caster::PREFIX_VIRTUAL.'dst_savings' => $c->getDSTSavings(),
  108. ];
  109. }
  110. return self::castError($c, $a);
  111. }
  112. public static function castIntlCalendar(\IntlCalendar $c, array $a, Stub $stub, $isNested, $filter = 0)
  113. {
  114. $a += [
  115. Caster::PREFIX_VIRTUAL.'type' => $c->getType(),
  116. Caster::PREFIX_VIRTUAL.'first_day_of_week' => $c->getFirstDayOfWeek(),
  117. Caster::PREFIX_VIRTUAL.'minimal_days_in_first_week' => $c->getMinimalDaysInFirstWeek(),
  118. Caster::PREFIX_VIRTUAL.'repeated_wall_time_option' => $c->getRepeatedWallTimeOption(),
  119. Caster::PREFIX_VIRTUAL.'skipped_wall_time_option' => $c->getSkippedWallTimeOption(),
  120. Caster::PREFIX_VIRTUAL.'time' => $c->getTime(),
  121. Caster::PREFIX_VIRTUAL.'in_daylight_time' => $c->inDaylightTime(),
  122. Caster::PREFIX_VIRTUAL.'is_lenient' => $c->isLenient(),
  123. Caster::PREFIX_VIRTUAL.'time_zone' => ($filter & Caster::EXCLUDE_VERBOSE) ? new CutStub($c->getTimeZone()) : $c->getTimeZone(),
  124. ];
  125. return self::castError($c, $a);
  126. }
  127. public static function castIntlDateFormatter(\IntlDateFormatter $c, array $a, Stub $stub, $isNested, $filter = 0)
  128. {
  129. $a += [
  130. Caster::PREFIX_VIRTUAL.'locale' => $c->getLocale(),
  131. Caster::PREFIX_VIRTUAL.'pattern' => $c->getPattern(),
  132. Caster::PREFIX_VIRTUAL.'calendar' => $c->getCalendar(),
  133. Caster::PREFIX_VIRTUAL.'time_zone_id' => $c->getTimeZoneId(),
  134. Caster::PREFIX_VIRTUAL.'time_type' => $c->getTimeType(),
  135. Caster::PREFIX_VIRTUAL.'date_type' => $c->getDateType(),
  136. Caster::PREFIX_VIRTUAL.'calendar_object' => ($filter & Caster::EXCLUDE_VERBOSE) ? new CutStub($c->getCalendarObject()) : $c->getCalendarObject(),
  137. Caster::PREFIX_VIRTUAL.'time_zone' => ($filter & Caster::EXCLUDE_VERBOSE) ? new CutStub($c->getTimeZone()) : $c->getTimeZone(),
  138. ];
  139. return self::castError($c, $a);
  140. }
  141. private static function castError($c, array $a): array
  142. {
  143. if ($errorCode = $c->getErrorCode()) {
  144. $a += [
  145. Caster::PREFIX_VIRTUAL.'error_code' => $errorCode,
  146. Caster::PREFIX_VIRTUAL.'error_message' => $c->getErrorMessage(),
  147. ];
  148. }
  149. return $a;
  150. }
  151. }