/lib/dmMediaItemValidator.class.php

https://github.com/stephaneerard/dmImagesGridPlugin · PHP · 222 lines · 172 code · 32 blank · 18 comment · 27 complexity · 77dd56a3af803b487eae2c04f20ba4b9 MD5 · raw file

  1. <?php
  2. /**
  3. * Description of dmMediaItemValidator
  4. *
  5. * @author TheCelavi
  6. */
  7. class dmMediaItemValidator extends sfValidatorBase {
  8. protected $val = '';
  9. protected $image_resize_method = array(
  10. 'fit'=>'Fit',
  11. 'scale'=>'Scale',
  12. 'inflate'=>'Inflate',
  13. 'top'=>'Top',
  14. 'right'=>'Right',
  15. 'bottom'=>'Bottom',
  16. 'left'=>'Left',
  17. 'center'=>'Center'
  18. );
  19. protected function configure($options = array(), $messages = array()) {
  20. /* OPTIONS */
  21. // Media
  22. $this->addOption('media_id');
  23. $this->addOption('model', 'DmMedia');
  24. $this->addOption('query', null);
  25. $this->addOption('column', null);
  26. // Media config
  27. $this->addOption('media_config_required', false);
  28. // Title
  29. $this->addOption('title_min_length', false);
  30. $this->addOption('title_max_length', false);
  31. $this->addOption('title_required', false);
  32. // Link
  33. $this->addOption('item_link_required', false);
  34. // Link config
  35. $this->addOption('item_link_config_required', false);
  36. /* MESSAGES */
  37. $this->addMessage('empty', 'List of images can not be empty');
  38. // Media
  39. $this->addMessage('media_invalid', 'Error in image file, it does not exist.');
  40. // Media config
  41. $this->addMessage('media_config_required', 'Image configuration is required.');
  42. $this->addMessage('media_config_invalid', 'Image configuration content is not in valid YAML format.');
  43. // Title
  44. $this->addMessage('title_min_length', '"%value%" is too short for the title (%title_min_length% characters min).');
  45. $this->addMessage('title_max_length', '"%value%" is too long for the title (%title_max_length% characters max).');
  46. $this->addMessage('title_required', 'Title is required.');
  47. // Link
  48. $this->addMessage('link_invalid', '"%value%" is not valid URL resource.');
  49. $this->addMessage('link_required', 'Link is required.');
  50. // Link config
  51. $this->addMessage('link_config_required', 'Link configuration is required.');
  52. $this->addMessage('link_config_invalid', 'Link configuration content is not in valid YAML format.');
  53. }
  54. protected function doClean($mediaItems) {
  55. try {
  56. $mediaItems = get_object_vars(json_decode($mediaItems));
  57. } catch (Exception $e) {
  58. throw new sfValidatorError($this, 'empty');
  59. }
  60. if (sizeof($mediaItems['media_id'])==0){
  61. throw new sfValidatorError($this, 'empty');
  62. }
  63. $cleanedMediaItems = array();
  64. $hasErrors = false;
  65. for ($i=0; $i<$c = sizeof($mediaItems['media_id']); $i++) {
  66. $mediaItem = array();
  67. $mediaItem['errors'] = array();
  68. try {
  69. $mediaItem['media_id'] = $this->validateMediaId($mediaItems['media_id'][$i]);
  70. } catch (sfValidatorError $e) {
  71. $mediaItem['media_id'] = $mediaItems['media_id'][$i];
  72. $mediaItem['errors'][] = array(
  73. "field"=>'media_id',
  74. "message"=>$e->getMessage()
  75. );
  76. $hasErrors = true;
  77. }
  78. try {
  79. $mediaItem['media_config'] = $this->validateMediaConfig($mediaItems['media_config'][$i]);
  80. } catch (sfValidatorError $e) {
  81. $mediaItem['media_config'] = $mediaItems['media_config'][$i];
  82. $mediaItem['errors'][] = array(
  83. "field"=>'media_config',
  84. "message"=>$e->getMessage()
  85. );
  86. $hasErrors = true;
  87. }
  88. try {
  89. $mediaItem['media_title'] = $this->validateMediaTitle($mediaItems['media_title'][$i]);
  90. } catch (sfValidatorError $e) {
  91. $mediaItem['media_title'] = $mediaItems['media_title'][$i];
  92. $mediaItem['errors'][] = array(
  93. "field"=>'media_title',
  94. "message"=>$e->getMessage()
  95. );
  96. $hasErrors = true;
  97. }
  98. try {
  99. $mediaItem['link'] = $this->validateLink($mediaItems['media_link'][$i]);
  100. } catch (sfValidatorError $e) {
  101. $mediaItem['link'] = $mediaItems['media_link'][$i];
  102. $mediaItem['errors'][] = array(
  103. "field"=>'media_link',
  104. "message"=>$e->getMessage()
  105. );
  106. $hasErrors = true;
  107. }
  108. try {
  109. $mediaItem['link_config'] = $this->validateMediaConfig($mediaItems['link_config'][$i]);
  110. } catch (sfValidatorError $e) {
  111. $mediaItem['link_config'] = $mediaItems['link_config'][$i];
  112. $mediaItem['errors'][] = array(
  113. "field"=>'link_config',
  114. "message"=>$e->getMessage()
  115. );
  116. $hasErrors = true;
  117. }
  118. $mediaItem['media_position'] = $mediaItems['media_position'][$i];
  119. $cleanedMediaItems[] = $mediaItem;
  120. }
  121. $this->val = json_encode($cleanedMediaItems);
  122. if ($hasErrors) throw new sfValidatorError($this, 'invalid', array('value'=> $this->val));
  123. else return $this->val;
  124. }
  125. // Hack for form
  126. public function getVal() {
  127. return $this->val;
  128. }
  129. protected function validateMediaId($value) {
  130. $query = $this->getOption('query');
  131. if ($query)
  132. $query = clone $query;
  133. else
  134. $query = Doctrine_Core::getTable($this->getOption('model'))->createQuery();
  135. $query->andWhere(sprintf('%s.%s = ?', $query->getRootAlias(), 'id'), $value);
  136. if (!$query->count())
  137. throw new sfValidatorError($this, 'media_invalid', array('value' => $value));
  138. return $value;
  139. }
  140. protected function validateMediaConfig($value) {
  141. $clean = (string) $value;
  142. $length = function_exists('mb_strlen') ? mb_strlen($clean, $this->getCharset()) : strlen($clean);
  143. if ($this->hasOption('media_config_required') && $this->getOption('media_config_required') && $length == 0) {
  144. throw new sfValidatorError($this, 'media_config_required');
  145. }
  146. if ($length > 0) {
  147. try {
  148. sfYaml::load($clean);
  149. } catch (InvalidArgumentException $e) {
  150. throw new sfValidatorError($this, 'media_config_invalid');
  151. }
  152. }
  153. return $clean;
  154. }
  155. protected function validateMediaTitle($value) {
  156. $clean = (string) $value;
  157. $length = function_exists('mb_strlen') ? mb_strlen($clean, $this->getCharset()) : strlen($clean);
  158. if ($this->hasOption('title_required') && $this->getOption('title_required') && $length == 0) {
  159. throw new sfValidatorError($this, 'title_required');
  160. }
  161. if ($this->hasOption('title_max_length') && $this->getOption('title_max_length') && $length > $this->getOption('title_max_length')) {
  162. throw new sfValidatorError($this, 'title_max_length', array('value' => $value, 'title_max_length' => $this->getOption('title_max_length')));
  163. }
  164. if ($this->hasOption('title_min_length') && $this->getOption('title_min_length') && $length < $this->getOption('title_min_length')) {
  165. throw new sfValidatorError($this, 'title_min_length', array('value' => $value, 'title_min_length' => $this->getOption('title_min_length')));
  166. }
  167. return $clean;
  168. }
  169. protected function validateLink($value) {
  170. $linkValidator = new dmValidatorLinkUrl(array('required' => ($this->hasOption('link_required') && $this->getOption('link_required'))));
  171. $linkValidator->setMessage('invalid', $this->getMessage('link_invalid'));
  172. $linkValidator->setMessage('required', $this->getMessage('link_required'));
  173. return $linkValidator->clean($value);
  174. }
  175. protected function validateLinkConfig($value) {
  176. $clean = (string) $value;
  177. $length = function_exists('mb_strlen') ? mb_strlen($clean, $this->getCharset()) : strlen($clean);
  178. if ($this->hasOption('link_config_required') && $this->getOption('link_config_required') && $length == 0) {
  179. throw new sfValidatorError($this, 'link_config_required');
  180. }
  181. if ($length > 0) {
  182. try {
  183. sfYaml::load($clean);
  184. } catch (InvalidArgumentException $e) {
  185. throw new sfValidatorError($this, 'link_config_invalid');
  186. }
  187. }
  188. return $clean;
  189. }
  190. }