<?php

namespace Silktide\LazyBoy\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * RestControllerTrait
 */
trait RestControllerTrait
{

    private $prohibitedKeys = [
        "password" => true,
        "salt" => true
    ];

    protected function success($data = null, $code = 200)
    {
        if ($data === null) {
            $data = ["success" => true];
        } elseif (is_array($data)) {
            $data = $this->normaliseData($data);
        }
        return new JsonResponse($data, $code);
    }

    protected function error($message, $data = [], $code = 400)
    {
        $payload = [
            "success" => false,
            "error" => $message
        ];
        if (!empty($data)) {
            $payload["context"] = $this->normaliseData($data);
        }
        return new JsonResponse($payload, $code);
    }

    protected function normaliseData($data)
    {
        foreach ($data as $key => $value) {
            if (isset($this->prohibitedKeys[$key])) {
                unset ($data[$key]);
            } elseif (is_array($value)) {
                $data[$key] = $this->normaliseData($value);
            }
        }
        return $data;
    }

}
<?php

namespace Silktide\LazyBoy\Controller;

use Symfony\Component\HttpFoundation\JsonResponse;

/**
 * RestControllerTrait
 */
trait RestControllerTrait
{

    private $prohibitedKeys = [
        "password" => true,
        "salt" => true
    ];

    protected function success($data = null, $code = 200)
    {
        if ($data === null) {
            $data = ["success" => true];
        } else {
            $data = $this->normaliseData($data);
        }
        return new JsonResponse($data, $code);
    }

    protected function error($message, $data = [], $code = 400)
    {
        $payload = [
            "success" => false,
            "error" => $message
        ];
        if (!empty($data)) {
            $payload["context"] = $this->normaliseData($data);
        }
        return new JsonResponse($payload, $code);
    }

    protected function normaliseData($data)
    {
        if (is_array($data)) {
            foreach ($data as $key => $value) {
                if (isset($this->prohibitedKeys[$key])) {
                    unset ($data[$key]);
                } else {
                    $data[$key] = $this->normaliseData($value);
                }
            }
        }
        return $data;
    }

}
Allow normaliseData to accept non-array values for the $data argument - means we can run the method on all data types, including objects, so if the method is overridden we don't have to convert them to arrays beforehand
Allow normaliseData to accept non-array values for the $data argument
 - means we can run the method on all data types, including objects, so if the method is overridden we don't have to convert them to arrays beforehand
