nilportugues / symfony-jsonapi

JSON API Transformer Bundle for Symfony 2 and Symfony 3
http://nilportugues.com
MIT License
115 stars 19 forks source link

Any reason underscores and not dashes? #5

Open mmucklo opened 8 years ago

mmucklo commented 8 years ago

I notice the attribute keys get passed through this function:

    /**
     * Transforms a given string from camelCase to under_score style.
     *
     * @param string $camel
     * @param string $splitter
     *
     * @return string
     */
    public static function camelCaseToUnderscore($camel, $splitter = '_')
    {
        $camel = \preg_replace(
            '/(?!^)[[:upper:]][[:lower:]]/',
            '$0',
            \preg_replace('/(?!^)[[:upper:]]+/', $splitter.'$0', $camel)
        );

        return \strtolower($camel);
    }

However the spec seems to recommend dashes:

http://jsonapi.org/recommendations/#naming

Any thoughts? Can you support both?

nilportugues commented 8 years ago

It's just a convention for most JSON format APIs, and JSONAPI doesn't enforce it but recommends.

Back to the bundle itself, thing is you may rename attributes and the class name using dashes and nothing will happen, as they will be preserved by the serializer, so you'll get the desired result. I understand your concern is it doesn't come in by default, right?

It agree ut would be nice to allow the bundle user decide. I'll just allow to pass down a variable. I'll mark this as an enhancement.