Pelzini

This is the code documentation for the Pelzini project

source of /processor/virtual_enumerations_transformer.php

Contains the VirtualEnumerationsTransformer class
  1. <?php
  2. /*
  3. Copyright 2008 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21.  
  22. /**
  23.  * Contains the {@link VirtualEnumerationsTransformer} class
  24.  *
  25.  * @package Transformers
  26.  * @author Josh
  27.  * @since 0.3
  28.  **/
  29.  
  30. /**
  31.  * This transformer converts constants that start with the same name into an enumeration of that name.
  32.  *
  33.  * So if a file has the following constants:
  34.  * APP_VERSION
  35.  * ITEM_TYPE_APPLE
  36.  * ITEM_TYPE_ORANGE
  37.  *
  38.  * The APPLE and ORANGE constants will become a part of the virtual enumeration ITEM_TYPE.
  39.  **/
  40. class VirtualEnumerationsTransformer extends Transformer {
  41. public function __construct()
  42. {
  43.  
  44. }
  45.  
  46.  
  47. /**
  48.   * Transforms the data model before outputting.
  49.   *
  50.   * This transformer converts constants that start with the same name into an enumeration of that name.
  51.   *
  52.   * @param array $parser_model The data model to transform
  53.   * @return array The new data model, or null if there was an error
  54.   **/
  55. public function transform(&$parser_model)
  56. {
  57. foreach ($parser_model as $item) {
  58. if ($item instanceof ParserFile) {
  59. $this->processConstants ($item);
  60. }
  61. }
  62.  
  63. return $parser_model;
  64. }
  65.  
  66.  
  67. /**
  68.   * Processes constants for a specified file
  69.   **/
  70. private function processConstants(ParserFile $file)
  71. {
  72. usort($file->constants, 'constant_name_sorter');
  73.  
  74. reset($file->constants);
  75. list ($last_const_id, $last_const) = each($file->constants);
  76.  
  77. $enum = null;
  78. while (list ($constant_id, $constant) = each($file->constants)) {
  79. if ($enum) {
  80. // If the constant starts with the same stuff as the current enum, add it
  81. if (strncmp($enum->name, $constant->name, strlen($enum->name)) == 0) {
  82. $enum->constants[] = $constant;
  83. unset ($file->constants[$constant_id]);
  84.  
  85. } else {
  86. // It doesn't match, so do a check that the constand begins with mostly
  87. // the same stuff - it can have the last two chars wrong.
  88. // If it is close, the enum is shrunk to what matches
  89. $num = $this->numSimilarChars($enum->name, $constant->name);
  90. if ($num >= 3 and $num >= (strlen($enum->name) - 2)) {
  91. if ($constant->name[$num - 1] == '_') $num--;
  92. $enum->name = substr($constant->name, 0, $num);
  93. $enum->constants[] = $constant;
  94. unset ($file->constants[$constant_id]);
  95.  
  96. // Of course if there is no match, we cannot force a match.
  97. } else {
  98. $enum = null;
  99. }
  100. }
  101.  
  102. } else {
  103. // There is no current enum, so check if one can be created by looking at the
  104. // last constant and the current constant
  105. $num = $this->numSimilarChars($last_const->name, $constant->name);
  106. if ($num >= 3) {
  107. if ($constant->name[$num - 1] == '_') $num--;
  108. $enum = $this->createVirtualEnum (substr($constant->name, 0, $num));
  109. $file->enumerations[] = $enum;
  110. $enum->constants[] = $last_const;
  111. $enum->constants[] = $constant;
  112. unset ($file->constants[$last_const_id]);
  113. unset ($file->constants[$constant_id]);
  114. }
  115. }
  116.  
  117. $last_const = $constant;
  118. $last_const_id = $constant_id;
  119. }
  120. }
  121.  
  122.  
  123. /**
  124.   * Creates an enum
  125.   **/
  126. private function createVirtualEnum($name)
  127. {
  128. $enum = new ParserEnumeration();
  129. $enum->name = $name;
  130. $enum->virtual = 1;
  131. return $enum;
  132. }
  133.  
  134.  
  135. /**
  136.   * Returns the number of chars in $a that are the same as the chars in $b
  137.   **/
  138. private function numSimilarChars($a, $b)
  139. {
  140. $i = 0;
  141.  
  142. while ($a[$i] == $b[$i]) $i++;
  143.  
  144. return $i;
  145. }
  146.  
  147.  
  148. }
  149.  
  150.  
  151. function constant_name_sorter($a, $b)
  152. {
  153. return strcasecmp($a->name, $b->name);
  154. }
  155.  
  156.  
  157. ?>
  158.