'integer' * 'type'=>'string' * 'type'=>'email' * * 'minvalue'=>30 * 'maxvalue'=>100 */ abstract class BaseIniFile { /** * Copy of definitions as array like: * array( * 'field1'=>array('type'=>'integer', 'minvalue'=>10) , * 'field2'=>array('type'=>'string') * ) */ protected $iniFields = null ; /** * Array of .ini file loaded values, or values changed by user * array('field1'=>'my typed text') */ protected $iniArray = null ; /** * Array of invalid fields */ protected $errorStack = null ; /** * Check if specific field is valid or not * * @param mixed $field * @return bool */ public function isValidField($field) { $errorStack = $this->errorStack ; return !isset($errorStack[$field]) ; } /** * Will check all fields, fill $errorStack and return if object is valid or not * * @return bool */ public function isValid() { $errorStack = array() ; $array = $this->iniArray ; foreach ($this->iniFields as $fieldName=>$validationRules) { if ( !$this->validateField($this->iniArray[$fieldName], $validationRules) ) $errorStack[$fieldName] = 'Invalid type' ; } $this->errorStack = $errorStack ; return empty($errorStack) ; } /** * Synchronize with $array; keeps the same API like Doctrine * * @param mixed $array * @return void */ public function synchronizeWithArray($array) { foreach ($array as $fieldName=>$fieldValue){ $this->$fieldName = is_numeric($fieldValue) ? (int) $fieldValue : $fieldValue; } } /** * Parses changed ini values and save it to file * * @return bool */ public function save() { if ( !$this->isValid() ) throw new Exception('Object is not valid') ; $iniFileContent = '' ; foreach ($this->iniArray as $fieldName=>$fieldValue) { $iniFileContent .= $fieldName . ' = ' . $fieldValue . "\n"; } return file_put_contents($this->getFilename(), $iniFileContent) ; } /* ******************** * Magic functions ******************** */ /** * Magic getter * * @param mixed $name * @return mixed */ public function __get($name) { if ( !isset($this->iniFields[$name]) ) throw new Exception('Property ' . $name . ' does not exist') ; return $this->iniArray[$name] ; } /** * Magic setter * * @param mixed $name * @param mixed $value */ public function __set($name, $value) { if ( !isset($this->iniFields[$name]) ) throw new Exception('Property ' . $name . ' does not exist') ; $this->iniArray[$name] = $value ; } /* ******************** * Protected functions ******************** */ // A private constructor; prevents direct creation of object private function __construct() {} /** * Validate $value according to array of $validationRules * * @param mixed $value * @param mixed $validationRules * @return bool */ protected function validateField($value, $validationRules) { foreach ($validationRules as $rule=>$expectedResult){ switch ($rule): case 'type': switch ($expectedResult): case 'integer': case 'string': if ( gettype($value)!=$expectedResult ) return false ; break ; case 'email': if( !eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $value) ) return false ; break ; default: throw new Exception('Unknown type: ' . $expectedResult) ; endswitch ; break ; case 'minvalue': if ( (int)$value < $expectedResult ) return false ; break ; case 'maxvalue': if ( (int)$value > $expectedResult ) return false ; break ; default: throw new Exception('Unknown validation rule: ' . $rule) ; endswitch ; } return true; } /** * Returns filename of ini file we need */ protected function getFilename() { return $this->iniFile ; } /** * Filename of used ini file */ protected $iniFile = null ; /** * Setup name of .ini file used * * @param mixed $filename */ protected function setFilename($filename) { $this->iniFile = $filename ; } /** * Set field name and array of validation rules for it * * @param mixed $fieldName * @param mixed $validationRules */ protected function hasField($fieldName, $validationRules) { $this->iniFields[$fieldName] = $validationRules ; } /** * Parse ini file and store it as array for later use */ protected function parseIniFile() { $filename = $this->getFilename() ; if ( !file_exists($filename) ) throw new Exception('Required file does not exist') ; $this->iniArray = parse_ini_file($filename, true) ; } /* ***************** * Static functions ***************** */ /** * Preserve instance of this class */ protected static $instance = null ; public static function getInstance($className) { if ( self::$instance==null ) { $class = new $className ; $class->setDefinition() ; $class->parseIniFile() ; self::$instance = $class ; } return self::$instance ; } }