public function _castArray($name, $data, $options)
    {
        $options['type'] = isset($options['relation']) && $options['relation'] === 'hasManyThrough' ? 'through' : 'set';
        $class = ltrim($options['class'], '\\');
        $classes = $class::classes();
        $collection = $classes[$options['type']];

        if ($data instanceof $collection) {
            return $data;
        }

        $isThrough = $options['type'] === 'through';

        $isDocument = $class === Document::class;

        $config = [
            'schema'    => $isDocument ? $this : $class::definition(),
            'basePath'  => $isDocument ? $name : null,
            'meta'      => isset($options['meta']) ? $options['meta'] : [],
            'exists'    => $options['exists'],
            'defaults'  => $options['defaults']
        ];
        if (isset($options['config'])){
            $config = Set::extend($config, $options['config']);
        }

        $column = isset($this->_columns[$name]) ? $this->_columns[$name] : [];
        if (!empty($column['format']) && $data !== null) {
            $type = $column['format'];
            $data = $this->convert('cast', $type, $data, $column, $config);
        }

        if ($isThrough) {
            $config['parent'] = $options['parent'];
            $config['through'] = $options['through'];
            $config['using'] = $options['using'];
            $config['data'] = $data;
        } else {
            $config['data'] = $data ?: [];
        }
        return new $collection($config);
    }