[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Copyright 2006, 2007, 2008, 2009 Eric D. Hough (http://ehough.com)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of TubePress (http://tubepress.org)
|
---|
| 6 | *
|
---|
| 7 | * TubePress is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * TubePress is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | *
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | function_exists('tubepress_load_classes')
|
---|
| 23 | || require(dirname(__FILE__) . '/../../../../tubepress_classloader.php');
|
---|
| 24 | tubepress_load_classes(array('org_tubepress_options_validation_InputValidationService',
|
---|
| 25 | 'org_tubepress_options_category_Display',
|
---|
| 26 | 'org_tubepress_message_MessageService'));
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * Default implementation of org_tubepress_options_validation_InputValidationService
|
---|
| 30 | */
|
---|
| 31 | class org_tubepress_options_validation_SimpleInputValidationService implements org_tubepress_options_validation_InputValidationService
|
---|
| 32 | {
|
---|
| 33 | private $_messageService;
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * @see org_tubepress_options_validation_InputValidationService::validate($optionName, $candidate)
|
---|
| 37 | */
|
---|
| 38 | public function validate($optionName, $candidate)
|
---|
| 39 | {
|
---|
| 40 | switch ($optionName) {
|
---|
| 41 |
|
---|
| 42 | case org_tubepress_options_category_Display::THUMB_HEIGHT:
|
---|
| 43 | $this->
|
---|
| 44 | _integerValidation(org_tubepress_options_category_Display::THUMB_HEIGHT,
|
---|
| 45 | $candidate, 1, 90);
|
---|
| 46 | break;
|
---|
| 47 |
|
---|
| 48 | case org_tubepress_options_category_Display::THUMB_WIDTH:
|
---|
| 49 | $this->
|
---|
| 50 | _integerValidation(org_tubepress_options_category_Display::THUMB_WIDTH,
|
---|
| 51 | $candidate, 1, 120);
|
---|
| 52 | break;
|
---|
| 53 |
|
---|
| 54 | case org_tubepress_options_category_Display::RESULTS_PER_PAGE:
|
---|
| 55 | $this->
|
---|
| 56 | _integerValidation(org_tubepress_options_category_Display::RESULTS_PER_PAGE,
|
---|
| 57 | $candidate, 1, 50);
|
---|
| 58 | break;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | /**
|
---|
| 63 | * Validates text values
|
---|
| 64 | *
|
---|
| 65 | * @param string $name The name of the option being updated
|
---|
| 66 | * @param unknown_type $candidate The new value for this option
|
---|
| 67 | *
|
---|
| 68 | * @return void
|
---|
| 69 | */
|
---|
| 70 | private function _textValidation($name, $candidate)
|
---|
| 71 | {
|
---|
| 72 | if (!is_string($candidate)) {
|
---|
| 73 | throw new Exception(sprintf($this->_messageService->_("validation-text"),
|
---|
| 74 | $name, $candidate));
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | /**
|
---|
| 79 | * Validates integral values
|
---|
| 80 | *
|
---|
| 81 | * @param string $name The name of the option being updated
|
---|
| 82 | * @param unknown_type $candidate The new value for this option
|
---|
| 83 | * @param int $min The minimum (inclusive) value this option
|
---|
| 84 | * can take
|
---|
| 85 | * @param int $max The maximum (inclusive) value this option
|
---|
| 86 | * can take
|
---|
| 87 | *
|
---|
| 88 | * @return void
|
---|
| 89 | */
|
---|
| 90 | private function _integerValidation($name, $candidate, $min, $max)
|
---|
| 91 | {
|
---|
| 92 |
|
---|
| 93 | if (intval($candidate) == 0) {
|
---|
| 94 | throw new Exception(sprintf($this->_messageService->_("validation-int-type"),
|
---|
| 95 | $name, $candidate));
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | if ($candidate < $min || $candidate > $max) {
|
---|
| 99 | throw new Exception(sprintf($this->_messageService->_("validation-int-range"),
|
---|
| 100 | $name, $min, $max, $candidate));
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * @see org_tubepress_options_validation_InputValidationService::setMessageService($messageService)
|
---|
| 106 | */
|
---|
| 107 | public function setMessageService(org_tubepress_message_MessageService $messageService)
|
---|
| 108 | {
|
---|
| 109 | $this->_messageService = $messageService;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|