CodeIgniter Forums
Decryption of data before creating CSV file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Decryption of data before creating CSV file (/showthread.php?tid=74478)



Decryption of data before creating CSV file - WinterThought - 09-29-2019

I have a database table that I need to convert into a CSV file using the DB Utility library's csv_from_result() function
however I need to run encryption->decrypt() on a few of the columns before I build the CSV. I can't create a new array of unencrypted data because csv_from_result() requires a valid db result object, not an array. Is there a way to do this?


RE: Decryption of data before creating CSV file - InsiteFX - 09-30-2019

Place these into one of your helper files.

PHP Code:
// -----------------------------------------------------------------------

if ( ! function_exists('objectToArray'))
{
    /**
     * objectToArray ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return array
     */
    function objectToArray(object $data) : array
    {
        if (is_object($data))
        {
            /**
             * Gets the properties of the given object
             * with get_object_vars function
             */
            $data get_object_vars($data);
        }

        /**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        return (is_array($data)) ? array_map(__FUNCTION__$data) : $data;
    }
}

// -----------------------------------------------------------------------

if ( ! function_exists('arrayToObject'))
{
    /**
     * arrayToObject ()
     * -------------------------------------------------------------------
     *
     * @param  $data
     * @return object
     */
    function arrayToObject(array $data) : object
    
{
        /**
         * Return array converted to object Using __FUNCTION__ (Magic constant)
         * for recursive call
         */
        return (is_array($data)) ? (object) array_map(__FUNCTION__$data) : $data;
    }


Method objectToArray converts an object to an array
Method arrayToObject converts an array to an object