/fuel/packages/orm/classes/observer/validation.php

https://bitbucket.org/samhunter3/redzu · PHP · 167 lines · 101 code · 20 blank · 46 comment · 9 complexity · 46a5be88b3754ed27b90fc9fb99390c6 MD5 · raw file

  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Orm;
  13. // Exception to throw when validation failed
  14. class ValidationFailed extends \FuelException {
  15. protected $fieldset;
  16. /**
  17. * Overridden \FuelException construct to add a Fieldset instance into the exception
  18. *
  19. * @param string
  20. * @param int
  21. * @param Exception
  22. * @param Fieldset
  23. */
  24. public function __construct($message = null, $code = 0, \Exception $previous = null, \Fieldset $fieldset = null)
  25. {
  26. parent::__construct($message, $code, $previous);
  27. $this->fieldset = $fieldset;
  28. }
  29. /**
  30. * Gets the Fieldset from this exception
  31. *
  32. * @return Fieldset
  33. */
  34. public function get_fieldset()
  35. {
  36. return $this->fieldset;
  37. }
  38. }
  39. class Observer_Validation extends Observer
  40. {
  41. /**
  42. * Set a Model's properties as fields on a Fieldset, which will be created with the Model's
  43. * classname if none is provided.
  44. *
  45. * @param string
  46. * @param Fieldset|null
  47. * @return Fieldset
  48. */
  49. public static function set_fields($obj, $fieldset = null)
  50. {
  51. static $_generated = array();
  52. $class = is_object($obj) ? get_class($obj) : $obj;
  53. if (is_null($fieldset))
  54. {
  55. $fieldset = \Fieldset::instance($class);
  56. if ( ! $fieldset)
  57. {
  58. $fieldset = \Fieldset::forge($class);
  59. }
  60. }
  61. ! array_key_exists($class, $_generated) and $_generated[$class] = array();
  62. if (in_array($fieldset, $_generated[$class], true))
  63. {
  64. return $fieldset;
  65. }
  66. $_generated[$class][] = $fieldset;
  67. $primary_keys = is_object($obj) ? $obj->primary_key() : $class::primary_key();
  68. $properties = is_object($obj) ? $obj->properties() : $class::properties();
  69. foreach ($properties as $p => $settings)
  70. {
  71. if (in_array($p, $primary_keys))
  72. {
  73. continue;
  74. }
  75. if (isset($settings['form']['options']))
  76. {
  77. foreach ($settings['form']['options'] as $key => $value)
  78. {
  79. $settings['form']['options'][$key] = \Lang::line($value) ?: $value;
  80. }
  81. }
  82. $label = isset($settings['label']) ? $settings['label'] : $p;
  83. $attributes = isset($settings['form']) ? $settings['form'] : array();
  84. $field = $fieldset->add($p, $label, $attributes);
  85. if ( ! empty($settings['validation']))
  86. {
  87. foreach ($settings['validation'] as $rule => $args)
  88. {
  89. if (is_int($rule) and is_string($args))
  90. {
  91. $args = array($args);
  92. }
  93. else
  94. {
  95. array_unshift($args, $rule);
  96. }
  97. call_user_func_array(array($field, 'add_rule'), $args);
  98. }
  99. }
  100. }
  101. return $fieldset;
  102. }
  103. /**
  104. * Execute before saving the Model
  105. *
  106. * @param Model
  107. * @throws ValidationFailed
  108. */
  109. public function before_save(Model $obj)
  110. {
  111. return $this->validate($obj);
  112. }
  113. /**
  114. * Validate the model
  115. *
  116. * @param Model
  117. * @throws ValidationFailed
  118. */
  119. public function validate(Model $obj)
  120. {
  121. $fieldset = static::set_fields($obj);
  122. $val = $fieldset->validation();
  123. // only allow partial validation on updates, specify the fields for updates to allow null
  124. $allow_partial = $obj->is_new() ? false : array();
  125. $input = array();
  126. foreach (array_keys($obj->properties()) as $p)
  127. {
  128. if ( ! in_array($p, $obj->primary_key()) and $obj->is_changed($p))
  129. {
  130. $input[$p] = $obj->{$p};
  131. is_array($allow_partial) and $allow_partial[] = $p;
  132. }
  133. }
  134. if ( ! empty($input) and $val->run($input, $allow_partial, array($obj)) === false)
  135. {
  136. throw new ValidationFailed($val->show_errors(), 0, null, $fieldset);
  137. }
  138. else
  139. {
  140. foreach ($input as $k => $v)
  141. {
  142. $obj->{$k} = $val->validated($k);
  143. }
  144. }
  145. }
  146. }
  147. // End of file validation.php