Prepare source.

@param   array   $source    Optional source data. If omitted, a copy of the server variable '_REQUEST' is used.
@param   boolean $reference If set to true, he source in first argument will be reference.

@return  void
546757
public function prepareSource(&$source = null, $reference = false)
    {
        if (is_null($source)) {
            $raw = static::loadRawFromRequest();

            if (isset($_SERVER['CONTENT_TYPE']) && strpos($_SERVER['CONTENT_TYPE'], 'multipart/form-data') === 0) {
                static::parseFormData($raw, $this->data);
            } else {
                parse_str($raw, $this->data);
            }

            if (!is_array($this->data)) {
                $this->data = [];
            }
        } else {
            if ($reference) {
                $this->data = &$source;
            } else {
                $this->data = $source;
            }
        }
    }
194519
protected function initSources(array $sources)
    {
        foreach ($sources as $source) {
            $this->sources[] = new EntitySource($this, $source);
        }
    }
42068
private function GenerateOutput($source) {
        array_push($this->Attributes, HtmlAttribute::Instanciate(HtmlAttributeConstants::Src, $source));
        $this->HtmlOutput = '<script type="application/javascript" {0}></script>';
        HtmlControlBuildHelper::Init()->GenerateAttributes($this);
    }
158623
public function setSource(Processable $source)
    {
        if ($source instanceof PrintedVariableEngine) {
            parent::setSource($source);
        } else {
            $msg = "PrintedVariableProcessingException::setSource only accepts PrintedVariableEngine objects.";
            throw new InvalidArgumentException($msg);
        }
    }
199840
private function copySource(TranslationValueInterface $source, WritableTranslationValueInterface $target): void
    {
        if (self::DO_NOT_COPY === $this->copySource) {
            return;
        }

        if (($oldValue = $target->getSource()) === ($newValue = $source->getSource())) {
            $this->logger->log(
                $this->logLevel,
                '{key}: Source is same, no need to update.',
                ['key' => $target->getKey(), 'old' => $oldValue, 'new' => $newValue]
            );
            return;
        }

        if ((self::COPY_IF_EMPTY === $this->copySource) && !$target->isSourceEmpty()) {
            $this->logger->log(
                $this->logLevel,
                '{key}: Source is not empty, no need to update.',
                ['key' => $target->getKey(), 'old' => $oldValue, 'new' => $newValue]
            );

            return;
        }

        $this->logger->log(
            LogLevel::NOTICE,
            '{key}: Updating source value.',
            ['key' => $target->getKey(), 'old' => $oldValue, 'new' => $newValue]
        );

        if ($this->dryRun) {
            return;
        }

        $target->setSource($newValue);
    }
501765
public function set_source($source) {
        $filerecord = new stdClass;
        $filerecord->source = $source;
        $this->update($filerecord);
    }
241296
public function setSource($source): void
    {
        if (!\is_array($source) && !($source instanceof \ArrayAccess)) {
            throw new Grid\GridException('Source of `ArraySource` should be array or implement ArrayAccess interface');
        }
        parent::setSource($source);
    }
1137423
protected function setSourceOrder()
    {
        $this->source = Registry::getInstance()->create('\Magento\Sales\Model\Order');
        /** @noinspection PhpDeprecationInspection http://magento.stackexchange.com/questions/114929/deprecated-save-and-load-methods-in-abstract-model */
        $this->source->load($this->id);
    }
1339088
protected function setInvoiceSource($source)
    {
        parent::setInvoiceSource($source);
        $this->order = $this->invoiceSource->getSource();
    }
185864
public function prepareSource(&$source = null, $reference = false)
    {
        $headers = [];

        foreach ((array) $source as $key => $value) {
            $headers[HeaderHelper::normalizeHeaderName($key)] = implode(', ', $value);
        }

        parent::prepareSource($headers, $reference);
    }
907568
function loadString($source)
    {
        $this->setDimensions($this->max_x + 1, $this->max_y + 1);
        $this->appendString($source);
    }
23106
protected function _copy(Model $source, Model $destination, array $references, array $exclusions)
    {
        try {
            // Perhaps source was already copied, then simply load destination model and return
            if (isset($this->mapping[$source->table]) && isset($this->mapping[$source->table][$source->id])) {
                $this->debug('Skipping '.get_class($source));

                $destination->load($this->mapping[$source->table][$source->id]);
            } else {
                $this->debug('Copying '.get_class($source));

                // TODO transform data from source to destination with a possible callback
                // $data = $source->get(); transformData($data);
                $data = $source->get();
                unset($data[$source->id_field]);
                foreach ($this->extractKeys($exclusions) as $key => $val) {
                    unset($data[$key]);
                }

                // TODO add a way here to look for duplicates based on unique fields
                // foreach($destination->unique fields) { try load by

                // Copy fields as they are
                foreach ($data as $key => $val) {
                    if (
                        ($field = $destination->hasField($key)) &&
                        $field->isEditable()
                    ) {
                        $destination->set($key, $val);
                    }
                }
            }
            $destination->hook('afterCopy', [$source]);

            // Look for hasOne references that needs to be mapped. Make sure records can be mapped, or copy them
            foreach ($this->extractKeys($references) as $ref_key => $ref_val) {
                $this->debug("Considering $ref_key");

                if (($ref = $source->hasRef($ref_key)) && $ref instanceof HasOne) {
                    $this->debug("Proceeding with $ref_key");

                    // load destination model through $source
                    $source_table = $ref->refModel()->table;

                    if (
                        isset($this->mapping[$source_table]) &&
                        array_key_exists($source[$ref_key], $this->mapping[$source_table])
                    ) {
                        // no need to deep copy, simply alter ID
                        $destination[$ref_key] = $this->mapping[$source_table][$source[$ref_key]];
                        $this->debug(' already copied '.$source[$ref_key].' as '.$destination[$ref_key]);
                    } else {
                        // hasOne points to null!
                        $this->debug('Value is '.$source[$ref_key]);
                        if (!$source[$ref_key]) {
                            $destination[$ref_key] = $source[$ref_key];
                            continue;
                        }

                        // pointing to non-existent record. Would need to copy
                        try {
                            $destination[$ref_key] = $this->_copy(
                                $source->ref($ref_key),
                                $destination->refModel($ref_key),
                                $ref_val,
                                $exclusions[$ref_key] ?? []
                            )->id;
                            $this->debug(' ... mapped into '.$destination[$ref_key]);
                        } catch (DeepCopyException $e) {
                            $this->debug('escalating a problem from '.$ref_key);

                            throw $e->addDepth($ref_key);
                        }
                    }
                }
            }

            // Next copy our own data
            $destination->save();

            // Store mapping
            $this->mapping[$source->table][$source->id] = $destination->id;
            $this->debug(' .. copied '.get_class($source).' '.$source->id.' '.$destination->id);

            // Next look for hasMany relationships and copy those too

            foreach ($this->extractKeys($references) as $ref_key => $ref_val) {
                if (($ref = $source->hasRef($ref_key)) && $ref instanceof HasMany) {

                    // No mapping, will always copy
                    foreach ($source->ref($ref_key) as $ref_model) {
                        $this->_copy(
                            $ref_model,
                            $destination->ref($ref_key),
                            $ref_val,
                            $exclusions[$ref_key] ?? []
                        );
                    }
                }
            }

            return $destination;
        } catch (DeepCopyException $e) {
            throw $e;
        } catch (\atk4\core\Exception $e) {
            $this->debug('noticed a problem');

            throw new DeepCopyException([
                'Problem cloning model',
                'source'          => $source,
                'source_info'     => $source->__debugInfo(),
                'source_data'     => $source->get(),
                'destination'     => $destination,
                'destination_info'=> $destination->__debugInfo(),
                'depth'           => $e->getParams()['field'] ?? '?',
                ], null, $e);
        }
    }
1296804
public final static function append(string $source, array $Append): string {
		return self::encode(Arr::append(self::decode($source), $Append));
	}
695007
public function mergeSource(SourceInterface $source): void
    {
        if (array_key_exists($source->sourceId(), $this->sources)) {
            unset($this->sources[$source->sourceId()]);
        } else {
            $this->newSources[$source->sourceId()] = $source;
        }
        $this->sources[$source->sourceId()] = $source;
    }
1071573
private function copyData(string $source, string $to, array $mapping)
    {
        $sourceColumns = array_keys($mapping);
        $targetColumns = array_values($mapping);

        //Preparing mapping
        $sourceColumns = array_map([$this, 'identify'], $sourceColumns);
        $targetColumns = array_map([$this, 'identify'], $targetColumns);

        $query = sprintf(
            'INSERT INTO %s (%s) SELECT %s FROM %s',
            $this->identify($to),
            implode(', ', $targetColumns),
            implode(', ', $sourceColumns),
            $this->identify($source)
        );

        $this->run($query);
    }
105008
public function setSource($source): void
    {
        if (!$source instanceof Db\Query\Select) {
            throw new Grid\GridException('Source of `SelectSource` should be `Db\\Query\\Select` object');
        }
        parent::setSource($source);
    }
1323672
public function setReference($reference)
    {
      if(!is_string($reference) || strlen(trim($reference)) == 0)
        throw new InvalidArgumentException(__METHOD__ . '; Invalid reference, must be a non empty string');

      $this->reference = $reference;
    }