Source for file DataContainer.abstract.php

Documentation is available at DataContainer.abstract.php

  1. <?php
  2.  
  3. require_once(HARMONI."utilities/DataContainer.interface.php");
  4. require_once(HARMONI."utilities/FieldSetValidator/FieldSet.class.php");
  5. require_once(HARMONI."utilities/FieldSetValidator/RuleSet.class.php");
  6.  
  7. /**
  8. * The DataContainer class encapsulates a FieldSet, and a RuleSet and sets up field-setting restrictions.
  9. *
  10. * The class is abstract and allows children to set up a list of fields (keys) that can be set/accessed, and rules to be associated with them.
  11. *
  12. * @package harmoni.utilities
  13. *
  14. * @copyright Copyright &copy; 2005, Middlebury College
  15. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  16. *
  17. * @version $Id: DataContainer.abstract.php,v 1.8 2007/09/05 19:55:22 adamfranco Exp $
  18. *
  19. * @abstract
  20. */
  21. class DataContainer
  22. extends DataContainerInterface
  23. {
  24. /**
  25. * The FieldSet where we will store our data.
  26. * @access private
  27. * @var object FieldSet $_fieldSet
  28. ***/
  29. var $_fieldSet;
  30. /**
  31. * The RuleSet that will define both rules and what keys are allowed.
  32. * @access private
  33. * @var object RuleSet $_ruleSet
  34. ***/
  35. var $_ruleSet;
  36. /**
  37. * The init function -- sets up the private variables.
  38. * @access protected
  39. * @return void
  40. ***/
  41. function init() {
  42. $this->_fieldSet = new FieldSet;
  43. $this->_ruleSet = new RuleSet;
  44. }
  45. /**
  46. * The add method adds a new field with associated rule and optional error if validation fails.
  47. * @param string $field The key of the field to add.
  48. * @param ref object ValidatorRule $rule The validator rule to apply to the value set to this key.
  49. * @param optional object Error $error The error to throw if validation of this key fails.
  50. * @access protected
  51. * @see FieldSetValidator
  52. * @return void
  53. ***/
  54. function add( $field, $rule, $message = null, $type = '' ) {
  55. // add the $field to the ruleset with the rule & error
  56. if ($message == null) {
  57. // add the default error for a DataContainer
  58. $message = "";
  59. $message .= "The field '$field' in the DataContainer '".get_class($this);
  60. $message .= "' could not be validated using the rule: ";
  61. $message .= get_class($rule);
  62. $message .= ".";
  63. $type = "System";
  64. }
  65. $this->_ruleSet->addRule( $field, $rule, $message, $type );
  66. // done;
  67. }
  68. /**
  69. * The get method returns the value stored in the FieldSet for $field.
  70. * @param string $field The field to get.
  71. * @access public
  72. * @return mixed The value of $field.
  73. ***/
  74. function get( $field ) {
  75. // check if this is a valid key
  76. if (!in_array($field,$this->_ruleSet->getKeys())) {
  77. throwError(new Error(get_class($this)." - can not get the value for key '$field' because it is not a valid key!","DataContainer",true));
  78. return false;
  79. }
  80. return $this->_fieldSet->get( $field );
  81. }
  82. /**
  83. * The set method sets the value for a field while checking constrictions.
  84. * @param string $field The field to set.
  85. * @param mixed $val The value to set $field to.
  86. * @access public
  87. * @return boolean True if setting $field succeeds.
  88. ***/
  89. function set( $field, $val ) {
  90. // first check if this is a valid field.
  91. if (!in_array($field,$this->_ruleSet->getKeys())) {
  92. // no good
  93. printDebugBacktrace();
  94. throwError( new Error(get_class($this)." - can not set key '$field' because it is not a valid key!","DataContainer",true));
  95. return false;
  96. }
  97. if ($this->_ruleSet->validate($field, $val)) {
  98. $this->_fieldSet->set($field, $val);
  99. return true;
  100. }
  101. return false;
  102. }
  103. /**
  104. * Goes through all the keys and makes sure they comply to the rules specified for them.
  105. * @access public
  106. * @return boolean TRUE if all are OK, FALSE otherwise.
  107. ***/
  108. function checkAll() {
  109. $toCheck = $this->_ruleSet->getKeys();
  110. foreach ($toCheck as $key) {
  111. if (!$this->_ruleSet->validate($key,$this->_fieldSet->get($key)))
  112. return false;
  113. }
  114. return true;
  115. }
  116. }
  117.  
  118. ?>

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