I18nUsageValidator.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. require_once __DIR__ . '/I18nValidatorInterface.php';
  3. class I18nUsageValidator implements I18nValidatorInterface {
  4. private $code;
  5. private $reference;
  6. private $totalEntries = 0;
  7. private $failedEntries = 0;
  8. private $result = '';
  9. public function __construct($reference, $code) {
  10. $this->code = $code;
  11. $this->reference = $reference;
  12. }
  13. public function displayReport() {
  14. return sprintf('%5.1f%% of translation keys are unused.', $this->failedEntries / $this->totalEntries * 100) . PHP_EOL;
  15. }
  16. public function displayResult() {
  17. return $this->result;
  18. }
  19. public function validate() {
  20. foreach ($this->reference as $file => $data) {
  21. foreach ($data as $key => $value) {
  22. $this->totalEntries++;
  23. if (preg_match('/\._$/', $key) && in_array(preg_replace('/\._$/', '', $key), $this->code)) {
  24. continue;
  25. }
  26. if (!in_array($key, $this->code)) {
  27. $this->result .= sprintf('Unused key %s - %s', $key, $value) . PHP_EOL;
  28. $this->failedEntries++;
  29. continue;
  30. }
  31. }
  32. }
  33. return 0 === $this->failedEntries;
  34. }
  35. }