1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5/** 6 * Validate the data for a whole schema 7 * 8 * Should be aquired through AccessDataTable::getValidator() 9 */ 10class AccessDataValidator extends ValueValidator { 11 12 /** @var AccessTable */ 13 protected $access; 14 15 /** @var array */ 16 protected $data; 17 18 /** 19 * ValidationResult constructor. 20 * @param AccessTable $access 21 * @param array $data the data to validate (and save) 22 */ 23 public function __construct(AccessTable $access, $data) { 24 parent::__construct(); 25 $this->access = $access; 26 $this->data = $data; 27 } 28 29 /** 30 * Validate the given data 31 * 32 * checks for assignments 33 * validates 34 * returns changed data only 35 * 36 * @param array $data array('schema' => ( 'fieldlabel' => 'value', ...)) 37 * @param string $pageid 38 * @param string[] $errors validation errors 39 * @return AccessDataValidator[]|bool savable data or false on validation error 40 */ 41 static public function validateDataForPage($data, $pageid, &$errors) { 42 $tosave = array(); 43 $valid = true; 44 $errors = array(); 45 46 $assignments = new Assignments(); 47 $tables = $assignments->getPageAssignments($pageid); 48 foreach($tables as $table) { 49 $access = AccessTable::byTableName($table, $pageid); 50 $validation = $access->getValidator($data[$table]); 51 if(!$validation->validate()) { 52 $valid = false; 53 $errors = array_merge($errors, $validation->getErrors()); 54 } else { 55 if($validation->hasChanges()) { 56 $tosave[] = $validation; 57 } 58 } 59 } 60 if($valid) return $tosave; 61 return false; 62 } 63 64 /** 65 * Validate the data. This will clean the data according to type! 66 * 67 * @return bool 68 */ 69 public function validate() { 70 $result = true; 71 foreach($this->access->getSchema()->getColumns() as $col) { 72 $label = $col->getType()->getLabel(); 73 $result = $result && $this->validateValue($col, $this->data[$label]); 74 } 75 return $result; 76 } 77 78 /** 79 * Check if the data changed (selects current data) 80 * 81 * @return bool 82 */ 83 public function hasChanges() { 84 $olddata = $this->access->getDataArray(); 85 return ($olddata != $this->data); 86 } 87 88 /** 89 * @return AccessTable 90 */ 91 public function getAccessTable() { 92 return $this->access; 93 } 94 95 /** 96 * Access the data after it has been cleand in the validation process 97 * 98 * @return array 99 */ 100 public function getCleanData() { 101 return $this->data; 102 } 103 104 /** 105 * Saves the data 106 * 107 * This saves no matter what. You have to chcek validation results and changes on your own! 108 * 109 * @param int $ts the timestamp to use when saving the data 110 * @return bool 111 */ 112 public function saveData($ts = 0) { 113 $this->access->setTimestamp($ts); 114 return $this->access->saveData($this->data); 115 } 116 117} 118