<commit_before><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 */

class Pimcore_Tool_Serialize {


    /**
     * @static
     * @param mixed $data
     * @return string
     */
    public static function serialize ($data) {

        // load all data if it is an object, because of lazyloaded fields and _fulldump
        if($data instanceof Object_Concrete) {
            Object_Service::loadAllObjectFields($data);
        }

        $filteredData = self::mapElementReferences($data, true);
        $serializedData = serialize($filteredData);

        // now we have to remap the elements, because of pass by reference (because of combination of version/caching, ...)
        self::reverseMapElementReferences($filteredData);

        return $serializedData;
    }

    /**
     * @static
     * @param $data
     * @return mixed
     */
    public static function unserialize ($data) {

        // only strings are allowed ;-)
        if(!is_string($data)) {
            return $data;
        }

        $unserialized = unserialize($data);

        // put the origin object directly to the registry, this is because of cyclic references which should be resolved by self::reverseMapElementReferences()
        if($unserialized instanceof Element_Interface) {
            Zend_Registry::set(Element_Service::getType($unserialized) . "_" . $unserialized->getId(), $unserialized);
        }

        $remappedData = self::reverseMapElementReferences($unserialized);
        return $remappedData;
    }

    /**
     * @static
     * @param $data
     * @param bool $isOrigin
     * @return mixed
     */
    public static function mapElementReferences ($data, $isOrigin = true) {

        if(is_object($data)) {
            if($data instanceof Element_Interface && !$isOrigin) {
                return new Element_Reference_Placeholder($data->getId(), Element_Service::getType($data));
            } else {
                $vars = get_object_vars($data);
                foreach ($vars as $key => $value) {
                    $data->$key = self::mapElementReferences($value, false);
                }
            }
        } else if (is_array($data)) {
            foreach ($data as &$value) {
                $value = self::mapElementReferences($value, false);
            }
        }

        return $data;
    }

    /**
     * @static
     * @param $data
     * @return mixed
     */
    public static function reverseMapElementReferences ($data) {
<commit_msg>optimizations in Pimcore_Tool_Serialize<commit_after><?php
/**
 * Pimcore
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.pimcore.org/license
 *
 * @copyright  Copyright (c) 2009-2010 elements.at New Media Solutions GmbH (http://www.elements.at)
 * @license    http://www.pimcore.org/license     New BSD License
 */

class Pimcore_Tool_Serialize {


    /**
     * @static
     * @param mixed $data
     * @return string
     */
    public static function serialize ($data) {

        // load all data if it is an object, because of lazyloaded fields and _fulldump
        if($data instanceof Object_Concrete) {
            Object_Service::loadAllObjectFields($data);
        }

        $filteredData = self::mapElementReferences($data, true);
        $serializedData = serialize($filteredData);

        // now we have to remap the elements, because of pass by reference (because of combination of version/caching, ...)
        self::reverseMapElementReferences($filteredData);

        return $serializedData;
    }

    /**
     * @static
     * @param $data
     * @return mixed
     */
    public static function unserialize ($data) {

        // only strings are allowed ;-)
        if(!is_string($data)) {
            return $data;
        }

        $unserialized = unserialize($data);

        // put the origin object directly to the registry, this is because of cyclic references which should be resolved by self::reverseMapElementReferences()
        if($unserialized instanceof Element_Interface) {
            Zend_Registry::set(Element_Service::getType($unserialized) . "_" . $unserialized->getId(), $unserialized);
        }

        $remappedData = self::reverseMapElementReferences($unserialized);
        return $remappedData;
    }

    /**
     * @static
     * @param $data
     * @param bool $isOrigin
     * @return mixed
     */
    public static function mapElementReferences ($data, $isOrigin = true) {

        if(is_object($data)) {
            if($data instanceof Element_Interface && !$isOrigin) {
                return new Element_Reference_Placeholder($data->getId(), Element_Service::getType($data));
            } else {
                $vars = get_object_vars($data);

                // check for blocked vars by $data::__sleep();
                // same behavior like serialize();
                // per default all keys are valid
                if(method_exists($data, "__sleep")) {
                    $allowedVars = $data->__sleep();
                } else {
                    $allowedVars = array_keys($vars);
                }

                foreach ($vars as $key => $value) {
                    if(in_array($key, $allowedVars)) {
                        $data->$key = self::mapElementReferences($value, false);
                    }
                }
            }
        } else if (is_array($data)) {
            foreach ($data as &$value) {
                $value = self::mapElementReferences($value, false);
            }
        }

        return $data;
    }

    /**
     * @static
     * @param $data
     * @return mixed
     */
    public static function reverseMapElementReferences ($data) {

{'fraction_non_alphanumeric': 0.11203117389186557, 'fraction_numerical': 0.002597824322130216, 'mean_word_length': 2.1396534148827726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '396196', 'n_tokens_mistral': 1692, 'n_tokens_neox': 1616, 'n_words': 449}