supernova-ws /
SuperNova
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Created by Gorlum 14.11.2018 0:27 |
||||
| 4 | */ |
||||
| 5 | |||||
| 6 | namespace Common; |
||||
| 7 | |||||
| 8 | use DBAL\DbFieldDescription; |
||||
| 9 | use DBAL\PropertyDescription; |
||||
| 10 | |||||
| 11 | /** |
||||
| 12 | * Class AccessLoggedTranslatedV2 |
||||
| 13 | * |
||||
| 14 | * Class introduces field-to-property translation |
||||
| 15 | * |
||||
| 16 | * "Fields" - input data with external namespace and vague types translated to "properties" - internal representation with own namespace and fixed types |
||||
| 17 | * |
||||
| 18 | * @package Common |
||||
| 19 | */ |
||||
| 20 | class AccessLoggedTranslatedV2 extends AccessLoggedV2 { |
||||
| 21 | /** |
||||
| 22 | * Field name translation to property names |
||||
| 23 | * |
||||
| 24 | * @var string[] $_fieldsToProperties |
||||
| 25 | */ |
||||
| 26 | protected static $_fieldsToProperties = []; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * @var PropertyDescription[] $_fields |
||||
| 30 | */ |
||||
| 31 | protected static $_properties = []; |
||||
| 32 | /** |
||||
| 33 | * Default values |
||||
| 34 | * |
||||
| 35 | * @var array $_defaults |
||||
| 36 | */ |
||||
| 37 | protected static $_defaults = []; |
||||
| 38 | /** |
||||
| 39 | * Mandatory field list |
||||
| 40 | * |
||||
| 41 | * @var array $_mandatory |
||||
| 42 | */ |
||||
| 43 | protected static $_mandatory = []; |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * Imports fields definitions from Storage |
||||
| 47 | * |
||||
| 48 | * Importing field type, defaults and creating field->property mappings in $_properties array |
||||
| 49 | * |
||||
| 50 | * @param DbFieldDescription[] $fields |
||||
| 51 | */ |
||||
| 52 | protected function importFieldDefinitions($fields) { |
||||
| 53 | static::$_properties = []; |
||||
| 54 | static::$_defaults = []; |
||||
| 55 | static::$_mandatory = []; |
||||
| 56 | |||||
| 57 | foreach ($fields as $fieldName => $fieldDescription) { |
||||
| 58 | $property = new PropertyDescription(); |
||||
| 59 | $property->fromDbFieldDescription($fieldDescription); |
||||
| 60 | |||||
| 61 | $propertyName = |
||||
| 62 | !empty(static::$_fieldsToProperties[$property->field]) |
||||
| 63 | ? static::$_fieldsToProperties[$property->field] |
||||
| 64 | : $property->field; |
||||
| 65 | |||||
| 66 | static::$_properties[$propertyName] = $property->setName($propertyName); |
||||
| 67 | // TODO - DEFAULT SHOULD BE CONVERTED TO PROPERTY!!!! |
||||
| 68 | static::$_defaults[$propertyName] = $property->default; |
||||
| 69 | |||||
| 70 | if ($property->mandatory) { |
||||
| 71 | static::$_mandatory[$propertyName] = true; |
||||
| 72 | } |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | // var_dump(static::$_properties); |
||||
| 76 | } |
||||
| 77 | |||||
| 78 | |||||
| 79 | /** |
||||
| 80 | * Converts one array (of properties, fields, user inputs, etc) to another with or w/o callback function |
||||
| 81 | * |
||||
| 82 | * @param array $array |
||||
| 83 | * @param string $callBackName Name of Property callback which will be used. Can be empty for no callback |
||||
| 84 | * @param bool $useDefaults Should defaults used if property/field is absent from input array |
||||
| 85 | * @param bool $useFieldName Use field name as array key. If empty - property name will be used |
||||
| 86 | * |
||||
| 87 | * @return array |
||||
| 88 | */ |
||||
| 89 | protected function convertToPropertyWithCallback($array, $callBackName = '', $useDefaults = false, $useFieldName = false) { |
||||
| 90 | $result = []; |
||||
| 91 | |||||
| 92 | foreach (static::$_properties as $propName => $property) { |
||||
| 93 | if (array_key_exists($propName, $array)) { |
||||
| 94 | $value = $array[$propName]; |
||||
| 95 | } elseif ($useDefaults) { |
||||
| 96 | $value = static::$_defaults[$propName]; |
||||
| 97 | } else { |
||||
| 98 | continue; |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | if (!empty($callBackName) && is_callable($property->$callBackName)) { |
||||
| 102 | $call = $property->$callBackName; |
||||
| 103 | $value = $call($value, $property); |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | $result[$useFieldName ? $property->field : $propName] = $value; |
||||
| 107 | } |
||||
| 108 | |||||
| 109 | return $result; |
||||
| 110 | } |
||||
| 111 | |||||
| 112 | |||||
| 113 | /** |
||||
| 114 | * |
||||
| 115 | * (remark) Here too much to redo is with ::convertToPropertyWithCallback() |
||||
| 116 | * |
||||
| 117 | * @param array $fieldArray |
||||
| 118 | * |
||||
| 119 | * @return static |
||||
| 120 | */ |
||||
| 121 | public function fromFieldArray(array $fieldArray) { |
||||
| 122 | foreach (static::$_properties as $propName => $property) { |
||||
| 123 | if (!array_key_exists($property->field, $fieldArray)) { |
||||
| 124 | continue; |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | $value = $fieldArray[$property->field]; |
||||
| 128 | |||||
| 129 | if (is_callable($property->toProperty)) { |
||||
| 130 | $call = $property->toProperty; |
||||
| 131 | $value = $call($value); |
||||
| 132 | } |
||||
| 133 | |||||
| 134 | $this->$propName = $value; |
||||
| 135 | } |
||||
| 136 | |||||
| 137 | return $this; |
||||
| 138 | } |
||||
| 139 | |||||
| 140 | /** |
||||
| 141 | * Converts record to field array |
||||
| 142 | * |
||||
| 143 | * Field array is basically ready to be saved to Storage (some kind of DB) |
||||
| 144 | * Check for mandatory fields also applied |
||||
| 145 | * |
||||
| 146 | * @param bool $useDefaults - Use default values if there is no original value |
||||
| 147 | * |
||||
| 148 | * @return array - field array to save |
||||
| 149 | */ |
||||
| 150 | public function toFieldArray($useDefaults = false) { |
||||
| 151 | $fieldValues = []; |
||||
| 152 | |||||
| 153 | // Making local copy of mandatory properties list |
||||
| 154 | $mandatory = static::$_mandatory; |
||||
| 155 | |||||
| 156 | foreach (static::$_properties as $propName => $property) { |
||||
| 157 | if (array_key_exists($propName, $this->values)) { |
||||
| 158 | $value = $this->$propName; |
||||
| 159 | } elseif ($useDefaults) { |
||||
| 160 | $value = static::$_defaults[$propName]; |
||||
| 161 | } else { |
||||
| 162 | continue; |
||||
| 163 | } |
||||
| 164 | |||||
| 165 | if (is_callable($property->fromProperty)) { |
||||
| 166 | $call = $property->fromProperty; |
||||
| 167 | $value = $call($value); |
||||
| 168 | } |
||||
| 169 | |||||
| 170 | $fieldValues[$property->field] = $value; |
||||
| 171 | |||||
| 172 | // Removing this property from mandatory list - if it present |
||||
| 173 | unset($mandatory[$propName]); |
||||
| 174 | } |
||||
| 175 | |||||
| 176 | if (!empty($mandatory)) { |
||||
| 177 | var_dump($mandatory); |
||||
|
0 ignored issues
–
show
Security
Debugging Code
introduced
by
Loading history...
|
|||||
| 178 | die('NO MANDATORY FIELDS'); // TODO - Exception |
||||
|
0 ignored issues
–
show
|
|||||
| 179 | } |
||||
| 180 | |||||
| 181 | return $fieldValues; |
||||
| 182 | } |
||||
| 183 | |||||
| 184 | /** |
||||
| 185 | * Convert changes and deltas from properties to fields for update |
||||
| 186 | * |
||||
| 187 | * @param $propertyArray |
||||
| 188 | * |
||||
| 189 | * @return array |
||||
| 190 | */ |
||||
| 191 | protected function changesToFields($propertyArray) { |
||||
| 192 | return $this->convertToPropertyWithCallback($propertyArray, 'fromProperty', false, true); |
||||
| 193 | } |
||||
| 194 | |||||
| 195 | /** |
||||
| 196 | * Mass-assign properties from user input |
||||
| 197 | * |
||||
| 198 | * Makes internal conversion types |
||||
| 199 | * DOES NOT fill default values |
||||
| 200 | * TODO - ADD VALIDATION |
||||
| 201 | * |
||||
| 202 | * @param array $propertyArray |
||||
| 203 | */ |
||||
| 204 | public function assignProperties(array $propertyArray, $validate = false) { |
||||
|
0 ignored issues
–
show
The parameter
$validate is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 205 | $properties = $this->convertToPropertyWithCallback($propertyArray, 'fromUser', false, false); |
||||
| 206 | |||||
| 207 | foreach ($properties as $propName => $value) { |
||||
| 208 | $this->$propName = $value; |
||||
| 209 | } |
||||
| 210 | } |
||||
| 211 | |||||
| 212 | /** |
||||
| 213 | * Fills empty field with default values |
||||
| 214 | */ |
||||
| 215 | public function fillDefaults() { |
||||
| 216 | foreach (static::$_properties as $propertyName => $propertyDescription) { |
||||
| 217 | if (!isset($this->$propertyName)) { |
||||
| 218 | $this->$propertyName = $propertyDescription->default; |
||||
| 219 | } |
||||
| 220 | } |
||||
| 221 | } |
||||
| 222 | |||||
| 223 | } |
||||
| 224 |