<commit_before><?php
namespace Swagger\Object;

use Swagger\Exception as SwaggerException;
use stdClass;

abstract class AbstractObject implements ObjectInterface
{
    protected $document;
    
    public function __construct($document) {
        $this->setDocument($document);
    }
    
    public function getVendorExtension($extension, $class = null)
    {
        if(is_string($class)) {
            return $this->getDocumentObjectProperty("x-{$extension}", $class);
        } else {
            return $this->getDocumentProperty("x-{$extension}");
        }
    }
    
    public function setVendorExtension($extension, $value)
    {
        if($value instanceof SwaggerObject\ObjectInterface || (is_array($value) && reset($value) instanceof SwaggerObject\ObjectInterface)) {
            return $this->setDocumentObjectProperty("x-{$extension}", $value);
        } else {
            return $this->setDocumentProperty("x-{$extension}", $value);
        }
    }
    
    public function getDocument() {
        if(!($this->document instanceof stdClass)) {
            $this->document = new stdClass;
        }
        return $this->document;
    }
    
    public function setDocument($document) {
        if(!($document instanceof stdClass)) {
            throw new \InvalidArgumentException(
                'Document must be an stdClass'
            );
        }
        $this->document = $document;
        return $this;
    }
    
    public function getDocumentProperty($name) {
        if(!property_exists($this->getDocument(), $name)) {
            throw new SwaggerException\MissingDocumentPropertyException("Property {$name} does not exist");
        }
        
        return $this->getDocument()->$name;
    }
    
    public function setDocumentProperty($name, $value) {
        $this->getDocument()->$name = $value;
        
        return $this;
    }
    
    public function getDocumentObjectProperty($name, $swaggerObjectClass, $allowsRef = false)
    {
        $value = $this->getDocumentProperty($name);
        
        if(is_array($value)) {
            $newValue = [];
            
            foreach($value as $arrayValue) {
                if($allowsRef && property_exists($arrayValue, '$ref')) {
                    $newValue[] = new Reference($arrayValue);
                } else {
                    $newValue[] = new $swaggerObjectClass($arrayValue);
                }
            }
            
            return $newValue;
        } else {
            if($allowsRef && $value instanceof stdClass && property_exists($value, '$ref')) {
                return new Reference($value);
            } else {
                return new $swaggerObjectClass($value);
            }
        }
    }
    
    public function setDocumentObjectProperty($name, $object)
    {
        if(is_array($object)) {
            $value = [];
            
            foreach($object as $arrayObject) {
                $value[] = $object->getSwaggerObjectValue();
            }
        } else {
            $value = $object->getSwaggerObjectValue();
        }
        
        return $this->setDocumentProperty($name, $value);
    }
}
<commit_msg>Respect original key association in converting arrays.<commit_after><?php
namespace Swagger\Object;

use Swagger\Exception as SwaggerException;
use stdClass;

abstract class AbstractObject implements ObjectInterface
{
    protected $document;
    
    public function __construct($document) {
        $this->setDocument($document);
    }
    
    public function getVendorExtension($extension, $class = null)
    {
        if(is_string($class)) {
            return $this->getDocumentObjectProperty("x-{$extension}", $class);
        } else {
            return $this->getDocumentProperty("x-{$extension}");
        }
    }
    
    public function setVendorExtension($extension, $value)
    {
        if($value instanceof SwaggerObject\ObjectInterface || (is_array($value) && reset($value) instanceof SwaggerObject\ObjectInterface)) {
            return $this->setDocumentObjectProperty("x-{$extension}", $value);
        } else {
            return $this->setDocumentProperty("x-{$extension}", $value);
        }
    }
    
    public function getDocument() {
        if(!($this->document instanceof stdClass)) {
            $this->document = new stdClass;
        }
        return $this->document;
    }
    
    public function setDocument($document) {
        if(!($document instanceof stdClass)) {
            throw new \InvalidArgumentException(
                'Document must be an stdClass'
            );
        }
        $this->document = $document;
        return $this;
    }
    
    public function getDocumentProperty($name) {
        if(!property_exists($this->getDocument(), $name)) {
            throw new SwaggerException\MissingDocumentPropertyException("Property {$name} does not exist");
        }
        
        return $this->getDocument()->$name;
    }
    
    public function setDocumentProperty($name, $value) {
        $this->getDocument()->$name = $value;
        
        return $this;
    }
    
    public function getDocumentObjectProperty($name, $swaggerObjectClass, $allowsRef = false)
    {
        $value = $this->getDocumentProperty($name);
        
        if(is_array($value)) {
            $newValue = [];
            
            foreach($value as $key => $arrayValue) {
                if($allowsRef && property_exists($arrayValue, '$ref')) {
                    $newValue[$key] = new Reference($arrayValue);
                } else {
                    $newValue[$key] = new $swaggerObjectClass($arrayValue);
                }
            }
            
            return $newValue;
        } else {
            if($allowsRef && $value instanceof stdClass && property_exists($value, '$ref')) {
                return new Reference($value);
            } else {
                return new $swaggerObjectClass($value);
            }
        }
    }
    
    public function setDocumentObjectProperty($name, $object)
    {
        if(is_array($object)) {
            $value = [];
            
            foreach($object as $arrayObject) {
                $value[] = $object->getSwaggerObjectValue();
            }
        } else {
            $value = $object->getSwaggerObjectValue();
        }
        
        return $this->setDocumentProperty($name, $value);
    }
}

{'fraction_non_alphanumeric': 0.12258974760934316, 'fraction_numerical': 0.0, 'mean_word_length': 1.9702048417132216, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1351255', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1610, 'n_words': 321}