Source for file LengthSC.class.php

Documentation is available at LengthSC.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."GUIManager/StyleComponent.class.php");
  4.  
  5. /**
  6. * The LengthSC represents CSS relative and absolute length values. The allowed
  7. * formats are:
  8. * <ul style="font-family: monospace;">
  9. * <li> Percentage - "12%" </li>
  10. * <li> Ems - "12em" </li>
  11. * <li> X-Height - "25ex" </li>
  12. * <li> Pixels - "120px" </li>
  13. * <li> Inches - "2.3in" </li>
  14. * <li> Centimeters - "23cm" </li>
  15. * <li> Millimeters - "1232mm" </li>
  16. * <li> Points - "22pt" </li>
  17. * <li> Picas - "54pc" </li>
  18. * </ul>
  19. * <br /><br />
  20. * The <code>StyleComponent</code> (SC) is the most basic of the three building pieces
  21. * of CSS styles. It combines a CSS property value with a ValidatorRule to ensure that
  22. * the value follows a certain format.<br /><br />
  23. *
  24. * @package harmoni.gui.scs
  25. *
  26. * @copyright Copyright &copy; 2005, Middlebury College
  27. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  28. *
  29. * @version $Id: LengthSC.class.php,v 1.11 2007/09/04 20:25:22 adamfranco Exp $
  30. */
  31. class LengthSC extends StyleComponent {
  32.  
  33. /**
  34. * The constructor.
  35. * @param string value The value to assign to this SC.
  36. * @access public
  37. ***/
  38. function LengthSC($value) {
  39. $errDescription = "Could not validate the length StyleComponent value \"%s\". ";
  40. $errDescription .= "Allowed units are: %, in, cm, mm, em, ex, pt, pc, px.";
  41. $rule = CSSLengthValidatorRule::getRule();
  42.  
  43. $displayName = "Length";
  44. $description = "Specifies the length (width, size, etc) in percentages (%),
  45. inches (in), centimeters (cm), millimeters (mm), ems (em), X-height (ex),
  46. points (pt), picas (pc), or pixels (px).";
  47. $this->StyleComponent($value, $rule, null, null, $errDescription, $displayName, $description);
  48. }
  49. }
  50.  
  51. class CSSLengthValidatorRule extends RegexValidatorRule {
  52.  
  53. function CSSLengthValidatorRule(){
  54. $this->_regex= "^-?[0-9]+(\.[0-9]+)?(%|in|cm|mm|em|ex|pt|pc|px)$";
  55. }
  56. /**
  57. * This is a static method to return an already-created instance of a validator
  58. * rule. There are at most about a hundred unique rule objects in use durring
  59. * any given execution cycle, but rule objects are instantiated hundreds of
  60. * thousands of times.
  61. *
  62. * This method follows a modified Singleton pattern
  63. *
  64. * @return object ValidatorRule
  65. * @access public
  66. * @static
  67. * @since 3/28/05
  68. */
  69. function getRule () {
  70. // Because there is no way in PHP to get the class name of the descendent
  71. // class on which this method is called, this method must be implemented
  72. // in each descendent class.
  73.  
  74. if (!is_array($GLOBALS['validator_rules']))
  75. $GLOBALS['validator_rules'] = array();
  76. $class = __CLASS__;
  77. if (!isset($GLOBALS['validator_rules'][$class]))
  78. $GLOBALS['validator_rules'][$class] = new $class;
  79. return $GLOBALS['validator_rules'][$class];
  80. }
  81. }
  82.  
  83.  
  84.  
  85.  
  86.  
  87. class CSSLengthValidatorRuleWithOptions extends RegexValidatorRule {
  88.  
  89. //@todo not tested
  90. /**
  91. * Note: this class takes a parameter and may have several instantiations.
  92. *
  93. * This is a static method to return an already-created instance of a validator
  94. * rule. There are at most about a hundred unique rule objects in use during
  95. * any given execution cycle, but rule objects are instantiated hundreds of
  96. * thousands of times.
  97. *
  98. * This method follows a modified Singleton pattern
  99. *
  100. * @return object ValidatorRule
  101. * @access public
  102. * @static
  103. * @since 3/28/05
  104. */
  105. function getRule ($options) {
  106. // Because there is no way in PHP to get the class name of the descendent
  107. // class on which this method is called, this method must be implemented
  108. // in each descendent class.
  109. if(count($options)==0){
  110. $regex = "^-?[0-9]+(\.[0-9]+)?(%|in|cm|mm|em|ex|pt|pc|px)$";
  111. }else{
  112. $regex = "^(".$options[0];
  113. for($i = 1; $i < count($options); $i++){
  114. $regex .= "|".$options[$i];
  115. }
  116. $regex .= "|-?[0-9]+(\.[0-9]+)?(%|in|cm|mm|em|ex|pt|pc|px))$";
  117. }
  118.  
  119. if (!isset($GLOBALS['validator_rules']) || !is_array($GLOBALS['validator_rules']))
  120. $GLOBALS['validator_rules'] = array();
  121.  
  122. $class = __CLASS__;
  123. $ruleKey = $class."(".strtolower($regex).")";
  124. if (!isset($GLOBALS['validator_rules'][$ruleKey])){
  125. eval('$newRule = new '.$class.'($regex);');
  126. $GLOBALS['validator_rules'][$ruleKey] =$newRule;
  127. }
  128. return $GLOBALS['validator_rules'][$ruleKey];
  129. }
  130. }
  131. ?>

Documentation generated on Wed, 19 Sep 2007 10:24:53 -0400 by phpDocumentor 1.3.0RC3