File indexing completed on 2024-05-12 06:02:11

0001 <?php
0002 /**
0003  *  ocs-webserver
0004  *
0005  *  Copyright 2016 by pling GmbH.
0006  *
0007  *    This file is part of ocs-webserver.
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_NotEmptyInContext 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 parent field is present and the current field has no value
0038      */
0039     const PARENT_NOT_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::PARENT_NOT_EMPTY => "Field must have a value when parent field is filled out."
0046     );
0047 
0048     /**
0049      * @var string
0050      */
0051     private $contextKey;
0052 
0053     function __construct($contextKey)
0054     {
0055         $this->contextKey = $contextKey;
0056     }
0057 
0058 
0059     /**
0060      * Returns true if and only if $value meets the validation requirements
0061      *
0062      * If $value fails validation, then this method returns false, and
0063      * getMessages() will return an array of messages that explain why the
0064      * validation failed.
0065      *
0066      * @param mixed $value
0067      * @param mixed $context
0068      * @return boolean
0069      */
0070     public function isValid($value, $context = null)
0071     {
0072         if ((null === $context) || !is_array($context) || !array_key_exists($this->contextKey, $context)) {
0073             $this->_error(self::KEY_NOT_FOUND);
0074             return false;
0075         }
0076 
0077         $parentField = $context[$this->contextKey];
0078 
0079         if (false === empty($parentField) AND empty($value)) {
0080             $this->_error(self::PARENT_NOT_EMPTY);
0081             return false;
0082         }
0083 
0084         return true;
0085     }
0086 
0087     /**
0088      * @return string
0089      */
0090     public function getContextKey()
0091     {
0092         return $this->contextKey;
0093     }
0094 
0095     /**
0096      * @param string $contextKey
0097      */
0098     public function setContextKey($contextKey)
0099     {
0100         $this->contextKey = $contextKey;
0101     }
0102 
0103 }