/** Convert model to JSON. */ public static function convertToJSON($model, $toString = false) { $conversionRules = Models::getConversionRules(get_class($model)); $result = array(); // Keep original simple values: $objectVars = get_object_vars($model); foreach ($objectVars as $key => $value) { if (is_string($value) || is_numeric($value)) { $result[$key] = $value; } } foreach ($conversionRules as $conversionRule) { if ($conversionRule instanceof FieldConversionRule) { $fieldName = $conversionRule->field; // TODO what if field names in JSON and in model are not the same? $result[$fieldName] = $conversionRule->toJSON($model->{$fieldName}); } else { if ($conversionRule instanceof FieldConversionRule) { // TODO } } } if ($toString) { return json_encode($result); } return $result; }