property.js 1.1 KB
Newer Older
B
baiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
import {guessType, isScalarType} from "./types";
import {camelCase, indent, isEmpty} from "./utils";

export const Visibility = {
    PUBLIC: "public",
    PRIVATE: "private",
    PROTECTED: "protected"
};

export const buildProperties = (properties, {visibility, typedProperties}) => {
    let result = "";
    properties.forEach((p) => {
        if (p.type){
            result += indent(`/** @var ${p.type} */\n`, 1);
        }
        result += indent(`${visibility}${(p.type && typedProperties) ? " " + p.type : ""} $${p.name};\n`, 1);

    });
    return result;
}

const subtype = (type, value, key) => {
    if (isScalarType(type) || isEmpty(value) || (value === {})) return null;
    let subKey = key;
    if (type === "array") {
        return guessType(
            value.length > 0 ? value[0] : null,
            subKey,
        )
    }
    return guessType(
        value,
        subKey,
    )
}

export const getPropertyInfo = (key, value) => {
    let type = guessType(value, key);
    return {
        name: camelCase(key),
        type: type,
        originalName: key,
        value: value,
        subtype: subtype(type, value, key)
    }
}