sfI18nYamlValidateExtractor.class.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * @package symfony
  11. * @subpackage i18n
  12. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  13. * @version SVN: $Id: sfI18nYamlValidateExtractor.class.php 9128 2008-05-21 00:58:19Z Carl.Vondrick $
  14. */
  15. class sfI18nYamlValidateExtractor extends sfI18nYamlExtractor
  16. {
  17. /**
  18. * Extract i18n strings for the given content.
  19. *
  20. * @param string $content The content
  21. *
  22. * @return array An array of i18n strings
  23. */
  24. public function extract($content)
  25. {
  26. $strings = array();
  27. $config = sfYaml::load($content);
  28. // New validate.yml format
  29. // fields
  30. if (isset($config['fields']))
  31. {
  32. foreach ($config['fields'] as $field => $validation)
  33. {
  34. foreach ($validation as $type => $parameters)
  35. {
  36. if (!is_array($parameters))
  37. {
  38. continue;
  39. }
  40. foreach ($parameters as $key => $value)
  41. {
  42. if (preg_match('/(msg|error)$/', $key))
  43. {
  44. $strings[] = $value;
  45. }
  46. }
  47. }
  48. }
  49. }
  50. // validators
  51. if (isset($config['validators']))
  52. {
  53. foreach (array_keys($config['validators']) as $name)
  54. {
  55. if (!isset($config['validators'][$name]['param']))
  56. {
  57. continue;
  58. }
  59. foreach ($config['validators'][$name]['param'] as $key => $value)
  60. {
  61. if (preg_match('/(msg|error)$/', $key))
  62. {
  63. $strings[] = $value;
  64. }
  65. }
  66. }
  67. }
  68. // Old validate.yml format
  69. // required messages
  70. if (isset($config['names']))
  71. {
  72. foreach ($config['names'] as $key => $value)
  73. {
  74. if (isset($value['required_msg']))
  75. {
  76. $strings[] = $value['required_msg'];
  77. }
  78. }
  79. }
  80. // validators
  81. foreach ($config as $key => $value)
  82. {
  83. if (isset($value['param']) && isset($value['class']))
  84. {
  85. foreach ($value['param'] as $key => $value)
  86. {
  87. if (preg_match('/(msg|error)$/', $key))
  88. {
  89. $strings[] = $value;
  90. }
  91. }
  92. }
  93. }
  94. return $strings;
  95. }
  96. }