Source for file StyleComponent.class.php

Documentation is available at StyleComponent.class.php

  1. <?php
  2.  
  3. require_once(HARMONI."GUIManager/StyleComponent.interface.php");
  4.  
  5. /**
  6. * This <code>StyleComponent</code> generic class is the base for all the other
  7. * <code>StyleComponents</code>. It is up to the user to provide all the
  8. * necessary data (display name, description, the ValidatorRule, the value of the SC, etc.)
  9. *
  10. * The <code>StyleComponent</code> (SC) is the most basic of the three building pieces
  11. * of CSS styles. It combines a CSS property value with a ValidatorRule to ensure that
  12. * the value follows a certain format.<br /><br />
  13. * The other two CSS styles building pieces are <code>StyleProperties</code> and
  14. * <code>StyleCollections</code>. To clarify the relationship between these three
  15. * building pieces, consider the following example:
  16. * <pre>
  17. * div {
  18. * margin: 20px;
  19. * border: 1px solid #000;
  20. * }
  21. * </pre>
  22. * <code>div</code> is a <code>StyleCollection</code> consisting of 2
  23. * <code>StyleProperties</code>: <code>margin</code> and <code>border</code>. Each
  24. * of the latter consists of one or more <code>StyleComponents</code>. In
  25. * specific, <code>margin</code> consists of one <code>StyleComponent</code>
  26. * with the value <code>20px</code>, and <code>border</code> has three
  27. * <code>StyleComponents</code> with values <code>1px</code>, <code>solid</code>,
  28. * and <code>#000</code> correspondingly.
  29. *
  30. * @package harmoni.gui
  31. *
  32. * @copyright Copyright &copy; 2005, Middlebury College
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  34. *
  35. * @version $Id: StyleComponent.class.php,v 1.14 2007/09/04 20:25:21 adamfranco Exp $
  36. ***/
  37.  
  38. class StyleComponent extends StyleComponentInterface {
  39.  
  40. /**
  41. * The display name of this SC.
  42. * @var string _displayName
  43. * @access private
  44. */
  45. var $_displayName;
  46. /**
  47. * The description of this SC.
  48. * @var string _description
  49. * @access private
  50. */
  51. var $_description;
  52. /**
  53. * This is the ValidatorRule of this SC.
  54. * @var object _rule
  55. * @access private
  56. */
  57. var $_rule;
  58. /**
  59. * The description of the Error that will be thrown whenever validation fails.
  60. * @var object _error
  61. * @access private
  62. */
  63. var $_errorDescription;
  64. /**
  65. * An array of strings that will store the list of options (may or may not be
  66. * set in the constructor)
  67. * @var array _options
  68. * @access private
  69. */
  70. var $_options;
  71. /**
  72. * If TRUE, then the value of this SC will be restricted to the list of options.
  73. * @var boolean _limitedToOptions
  74. * @access private
  75. */
  76. var $_limitedToOptions;
  77. /**
  78. * The constructor.
  79. * @param string value The value to assign to this SC.
  80. * @param ref object rule The ValidatorRule that will be used to validate the
  81. * values of this SC. If <code>NULL</code>, no validator rule will be used.
  82. * @param array options An array of strings that represents the allowed values
  83. * (i.e. the list of options) of this SC. If this argument is not null, hasOptions()
  84. * will return <code>true</code> and getOptions() will return an iterator of
  85. * the options. In addition, if <code>limitedToOptions</code> is set to <code>TRUE</code>,
  86. * then a new ChoiceValidatorRule will be created with the given options.
  87. * If this argument is <code>null</code>, then hasOptions() will
  88. * return <code>false</code>.
  89. * @param mixed limitedToOptions This is either a boolean or null. If TRUE,
  90. * a new ChoiceValidatorRule will be created for the given list of options.
  91. * If <code>limitedToOptions</code> is not set, then the value of the argument is irrelevant.
  92. * FALSE and <code>null</code> will result the same behavior but it is recommended
  93. * that <code>FALSE</code> is used whenever <code>options</code> is set, and <code>null</code>
  94. * if not.
  95. * @param ref mixed This is one of the following two: 1) The ValidatorRule
  96. * that will be used to validate the values of this SC, or 2) An array of strings
  97. * that represents the allowed values (i.e. the list of options) of this SC. Pass the
  98. * array whenever you want hasOptions() and getOptions to function accordingly.
  99. * @param ref object error This is the Error to throw when validation fails.
  100. * @param string displayName The display name of the SC.
  101. * @param string description The description of the SC.
  102. * @access public
  103. ***/
  104. function StyleComponent($value, $rule, $options, $limitedToOptions, $errorDescription, $displayName, $description) {
  105. if (isset($rule)&&!is_null($rule)){
  106. $this->_rule =$rule;
  107. }else{
  108. //always true regex rule
  109. $this->_rule = RegexValidatorRule::getRule(".*");
  110. }
  111. if(func_num_args()<7){
  112. throwError(new Error("Too few parameters for StyleComponent", "GUIManager", true));
  113. }
  114. $this->_displayName = $displayName;
  115. $this->_description = $description;
  116. $this->_errorDescription = $errorDescription;
  117. $this->_limitedToOptions = false;
  118. $this->_options = array();
  119. if (isset($options) && is_array($options)) {
  120. // the SC will have a list of options
  121. $this->_options = $options;
  122. if ($limitedToOptions) {
  123. // create the appropriate ChoiceValidatorRule with the given options
  124. $this->_limitedToOptions = true;
  125. //$choiceRule = ChoiceValidatorRule::getRule($options);
  126. //$this->_rule = AndValidatorRule::getRule($this->_rule, $choiceRule);
  127. }
  128. //else
  129. // $this->_rule = OrValidatorRule::getRule($this->_rule, ChoiceValidatorRule::getRule($options));
  130. }
  131. // validate the value
  132. if (!$this->_rule->check($value)){
  133. throwError(new Error($this->_errorDescription, "GUIManager", true));
  134. }
  135. $this->_value = $value;
  136. }
  137. /**
  138. * Sets the id
  139. *
  140. * @param object HarmoniId $id
  141. * @return void
  142. * @access public
  143. * @since 4/26/06
  144. */
  145. function setId ($id) {
  146. if (!is_object($id))
  147. throwError(new Error("GUIMANAGER", "STRING ID PASSED"));
  148. $this->_id =$id;
  149. }
  150. /**
  151. * Answers the id
  152. *
  153. * @return object HarmoniId
  154. * @access public
  155. * @since 4/26/06
  156. */
  157. function getId () {
  158. if (isset($this->_id)){
  159. return $this->_id;
  160. }else{
  161. $im = Services::getService("Id");
  162. $this->_id = $im->createId();
  163. return $this->_id;
  164. }
  165. }
  166.  
  167. /**
  168. * Returns the display name of this SC.
  169. * @access public
  170. * @return string The display name of this SC.
  171. ***/
  172. function getDisplayName() {
  173. return $this->_displayName;
  174. }
  175. /**
  176. * Returns the description of this StlyeProperty.
  177. * @access public
  178. * @return string The description of this StlyeProperty.
  179. ***/
  180. function getDescription() {
  181. return $this->_description;
  182. }
  183. /**
  184. * Get the value of this SC.
  185. * @access public
  186. * @return string The value of this SC.
  187. ***/
  188. function getValue() {
  189. return $this->_value;
  190. }
  191. /**
  192. * Get the error description of this SC.
  193. * @access public
  194. * @return string The error description of this SC.
  195. ***/
  196. function getErrorDescription() {
  197. return $this->_errorDescription;
  198. }
  199. /**
  200. * Get the rule of this SC.
  201. * @access public
  202. * @return object ValidatorRule The rule of this SC.
  203. ***/
  204. function getRule() {
  205. return $this->_rule;
  206. }
  207.  
  208. /**
  209. * Sets the value of this SC and validates it using the attached <code>ValidatorRule</code>.
  210. * @access public
  211. * @param string value The new value.
  212. ***/
  213. function setValue($value) {
  214. // validate the value
  215. if (!$this->_rule->check($value))
  216. throwError(new Error($this->_errorDescription, "GUIManager", false));
  217. $this->_value = $value;
  218. }
  219. /**
  220. * Determines whether this SC has a list of options. If there is a list of
  221. * options, then the ValidatorRule of this SC would be a ChoiceValidatorRule.
  222. * If not, the ValidatorRule could be any ValidatorRule.
  223. * @access public
  224. * @return boolean True if the SC has a list of options. FALSE if
  225. * the SC can take any value.
  226. ***/
  227. function hasOptions() {
  228. return count($this->_options) > 0;
  229. }
  230.  
  231. /**
  232. * This function will return <code>TRUE</code> if the value of this SC is
  233. * restricted only to the list of options. It will return <code>FALSE</code>,
  234. * if not.
  235. * @access public
  236. * @return boolean <code>TRUE</code> if the value of this SC is
  237. * restricted only to the list of options. <code>FALSE</code>,
  238. * if not.
  239. ***/
  240. function isLimitedToOptions() {
  241. return $this->_limitedToOptions;
  242. }
  243. /**
  244. * Returns the list of options (list of allowed values) of this SC.
  245. * @access public
  246. * @return array An array containing the list of options
  247. * (list of allowed values) of this SC.
  248. ***/
  249. function getOptions() {
  250. return $this->_options;
  251. }
  252. }
  253. ?>

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