Brak opisu

IntlCaster.php 8.7KB

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