File indexing completed on 2025-01-19 05:20:54
0001 <?php 0002 /** 0003 * ocs-apiserver 0004 * 0005 * Copyright 2016 by pling GmbH. 0006 * 0007 * This file is part of ocs-apiserver. 0008 * 0009 * This program is free software: you can redistribute it and/or modify 0010 * it under the terms of the GNU Affero General Public License as 0011 * published by the Free Software Foundation, either version 3 of the 0012 * License, or (at your option) any later version. 0013 * 0014 * This program is distributed in the hope that it will be useful, 0015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0017 * GNU Affero General Public License for more details. 0018 * 0019 * You should have received a copy of the GNU Affero General Public License 0020 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0021 **/ 0022 0023 class Local_Validate_NotEmptyXor extends Zend_Validate_Abstract 0024 { 0025 0026 /** 0027 * Validation failure message key for when the value of the parent field is an empty string 0028 */ 0029 const KEY_NOT_FOUND = 'KeyNotFound'; 0030 0031 /** 0032 * Validation failure message key for when the value is an empty string 0033 */ 0034 const KEY_IS_EMPTY = 'KeyIdEmpty'; 0035 0036 /** 0037 * Validation failure message when sibling field is present and all fields has no value 0038 */ 0039 const FIELDS_EMPTY = 'FieldEmpty'; 0040 0041 0042 protected $_messageTemplates = array( 0043 self::KEY_NOT_FOUND => "Key not found in context.", 0044 self::KEY_IS_EMPTY => "Key is empty.", 0045 self::FIELDS_EMPTY => "At least one of the fields must have a value." 0046 ); 0047 0048 /** 0049 * @var string 0050 */ 0051 private $contextKey; 0052 0053 /** 0054 * @param array $contextKey 0055 */ 0056 function __construct($contextKey) 0057 { 0058 if (!is_array($contextKey)) { 0059 $contextKey = array($contextKey); 0060 } 0061 0062 $this->contextKey = $contextKey; 0063 } 0064 0065 0066 /** 0067 * Returns true if and only if $value meets the validation requirements 0068 * 0069 * If $value fails validation, then this method returns false, and 0070 * getMessages() will return an array of messages that explain why the 0071 * validation failed. 0072 * 0073 * @param mixed $value 0074 * @param mixed $context 0075 * @return boolean 0076 */ 0077 public function isValid($value, $context = null) 0078 { 0079 if ((null === $context) || !is_array($context) || !$this->testKeysExistInContext($this->contextKey, $context)) { 0080 $this->_error(self::KEY_NOT_FOUND); 0081 return false; 0082 } 0083 0084 $oneOfSiblingFieldsFilled = false; 0085 foreach ($this->contextKey as $key) { 0086 $siblingField = $context[$key]; 0087 0088 if (false === empty($siblingField)) { 0089 $oneOfSiblingFieldsFilled = ($oneOfSiblingFieldsFilled OR true); 0090 } 0091 } 0092 0093 if (false === $oneOfSiblingFieldsFilled) { 0094 $this->_error(self::FIELDS_EMPTY); 0095 return false; 0096 } 0097 0098 return true; 0099 } 0100 0101 protected function testKeysExistInContext($keys, $context) 0102 { 0103 foreach ($keys as $key) { 0104 if (false === isset($context[$key])) { 0105 return false; 0106 } 0107 } 0108 0109 return true; 0110 } 0111 0112 /** 0113 * @return string 0114 */ 0115 public function getContextKey() 0116 { 0117 return $this->contextKey; 0118 } 0119 0120 /** 0121 * @param string $contextKey 0122 */ 0123 public function setContextKey($contextKey) 0124 { 0125 $this->contextKey = $contextKey; 0126 } 0127 }