<commit_before><?php

namespace JimmyOak\Utility;

class ObjectUtils extends UtilsBase
{
    const SHALLOW = false;
    const DEEP = true;

    /** @var ArrayUtils */
    private $arrayUtils;

    protected function __construct()
    {
        $this->arrayUtils = ArrayUtils::instance();
    }

    /**
     * @param object $objectToParse
     * @param bool $deep
     *
     * @return \SimpleXMLElement
     */
    public function toXml($objectToParse, $deep = self::SHALLOW)
    {
        return simplexml_load_string($this->toXmlString($objectToParse, $deep));
    }

    /**
     * @param object $object
     * @param bool $deep
     *
     * @return string
     */
    public function toXmlString($object, $deep = self::SHALLOW)
    {
        return $this->arrayUtils->toXmlString($this->toArray($object, $deep));
    }

    /**
     * @param object $object
     * @param bool $deep
     *
     * @return array
     */
    public function toArray($object, $deep = self::SHALLOW)
    {
        return $deep ? $this->toDeepArray($object) : $this->toShallowArray($object);
    }

    private function toDeepArray($object)
    {
        $array = [];

        if ($this->isDateTimeExtensionObject($object)) {
            return $this->parseDateTimeExtensionObject($object);
        }

        $vars = $this->getAllObjectVarsOrArrayProperties($object);

        foreach ($vars as $property => $value) {
            if (is_object($value) || is_array($value)) {
                $array[$property] = $this->toDeepArray($value);
            } elseif (is_resource($value)) {
                $array[$property] = (string)$value;
            } else {
                $array[$property] = $value;
            }
        }

        return $array;
    }

    private function isDateTimeExtensionObject($object)
    {
        return $object instanceof \DateTimeInterface
            || $object instanceof \DateInterval
            || $object instanceof \DateTimeZone;
    }

    private function parseDateTimeExtensionObject($object)
    {
        if ($object instanceof \DateTimeInterface) {
            return $object->format(\DateTime::ISO8601);
        } elseif ($object instanceof \DateInterval) {
            return $object->format('P%yY%mM%dDT%hH%iM%sS');
        } elseif ($object instanceof \DateTimeZone) {
            return $object->getName();
        }

        return (string) $object;
    }

    private function getAllObjectVarsOrArrayProperties($object): array
    {
        if (is_object($object)) {
            $vars = $this->getAllObjectVars($object);<commit_msg>Fix return type for php<7.0 compatibility<commit_after><?php

namespace JimmyOak\Utility;

class ObjectUtils extends UtilsBase
{
    const SHALLOW = false;
    const DEEP = true;

    /** @var ArrayUtils */
    private $arrayUtils;

    protected function __construct()
    {
        $this->arrayUtils = ArrayUtils::instance();
    }

    /**
     * @param object $objectToParse
     * @param bool $deep
     *
     * @return \SimpleXMLElement
     */
    public function toXml($objectToParse, $deep = self::SHALLOW)
    {
        return simplexml_load_string($this->toXmlString($objectToParse, $deep));
    }

    /**
     * @param object $object
     * @param bool $deep
     *
     * @return string
     */
    public function toXmlString($object, $deep = self::SHALLOW)
    {
        return $this->arrayUtils->toXmlString($this->toArray($object, $deep));
    }

    /**
     * @param object $object
     * @param bool $deep
     *
     * @return array
     */
    public function toArray($object, $deep = self::SHALLOW)
    {
        return $deep ? $this->toDeepArray($object) : $this->toShallowArray($object);
    }

    private function toDeepArray($object)
    {
        $array = [];

        if ($this->isDateTimeExtensionObject($object)) {
            return $this->parseDateTimeExtensionObject($object);
        }

        $vars = $this->getAllObjectVarsOrArrayProperties($object);

        foreach ($vars as $property => $value) {
            if (is_object($value) || is_array($value)) {
                $array[$property] = $this->toDeepArray($value);
            } elseif (is_resource($value)) {
                $array[$property] = (string)$value;
            } else {
                $array[$property] = $value;
            }
        }

        return $array;
    }

    private function isDateTimeExtensionObject($object)
    {
        return $object instanceof \DateTimeInterface
            || $object instanceof \DateInterval
            || $object instanceof \DateTimeZone;
    }

    private function parseDateTimeExtensionObject($object)
    {
        if ($object instanceof \DateTimeInterface) {
            return $object->format(\DateTime::ISO8601);
        } elseif ($object instanceof \DateInterval) {
            return $object->format('P%yY%mM%dDT%hH%iM%sS');
        } elseif ($object instanceof \DateTimeZone) {
            return $object->getName();
        }

        return (string) $object;
    }

    private function getAllObjectVarsOrArrayProperties($object)
    {
        if (is_object($object)) {
            $vars = $this->getAllObjectVars($object);
{'fraction_non_alphanumeric': 0.1309408341416101, 'fraction_numerical': 0.0019398642095053346, 'mean_word_length': 2.2798982188295165, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '940481', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1434, 'n_words': 293}