@param array $exists

@return array
10906
private function prepareExists(array $exists)
    {
        if ($exists['sql'] instanceof SelectQuery) {
            [$sql, $params] = $exists['sql']->toSql();

            $exists['sql'] = $sql;

            $exists['params'] = $params;
        }

        return $exists;
    }
1318350
public function exists(array $options = [])
    {
        if (!$this->id) {
            throw new InvalidArgumentException('Kuzzle\Document::exists: cannot check if the document exists without a document ID');
        }

        $response = $this->collection->getKuzzle()->query(
            $this->collection->buildQueryArgs('document', 'exists'),
            $this->collection->getKuzzle()->addHeaders($this->serialize(), $this->headers),
            $options
        );

        return  $response['result'];
    }
1206762
public function newInstance($attributes = [], $exists = false)
    {
        $model = new static((array) $attributes, $this->parent, $this->table, $exists);
        $model->setPivotKeys($this->getForeignKey(), $this->getRelatedKey());
        return $model;
    }
638426
protected function addTimestampsToAttachment(array $record, $exists = false)
    {
        $fresh = $this->parent->freshTimestamp();

        if (! $exists && $this->hasPivotColumn($this->createdAt())) {
            $record[$this->createdAt()] = $fresh;
        }

        if ($this->hasPivotColumn($this->updatedAt())) {
            $record[$this->updatedAt()] = $fresh;
        }

        return $record;
    }
255229
protected function setTimestampsOnAttach(array $record, $exists = false)
	{
		$fresh = $this->parent->freshTimestamp();

		if ( ! $exists) $record[$this->createdAt()] = $fresh;

		$record[$this->updatedAt()] = $fresh;

		return $record;
	}
127413
public static function fromArray(array $options)
    {
        $defaultOptions = [
            'host' => '127.0.0.1',
            'port' => 9200,
            'index' => '',
            'shards' => 3,
            'replicas' => 0,
            'minScore' => 4,
            'highlight' => true,
            'populate_order'     => self::POPULATE_ORDER_RID,
            'populate_direction' => self::POPULATE_DIRECTION_DESC,
            'activeTab' => null,
        ];

        foreach(self::getAggregableTechnicalFields() as $k => $f) {
            $defaultOptions[$k.'_limit'] = 0;
        }
        $options = array_replace($defaultOptions, $options);


        $self = new self();
        $self->setHost($options['host']);
        $self->setPort($options['port']);
        $self->setIndexName($options['index']);
        $self->setShards($options['shards']);
        $self->setReplicas($options['replicas']);
        $self->setMinScore($options['minScore']);
        $self->setHighlight($options['highlight']);
        $self->setPopulateOrder($options['populate_order']);
        $self->setPopulateDirection($options['populate_direction']);
        $self->setActiveTab($options['activeTab']);
        foreach(self::getAggregableTechnicalFields() as $k => $f) {
            $self->setAggregableFieldLimit($k, $options[$k.'_limit']);
        }


        return $self;
    }
102425
public function fill(array $attributes, $exists = false)
    {
        $this->fillExists = $exists;

        foreach ($attributes as $key => $value) {
            $this->setAttribute($key, $value);
        }

        $this->fillExists = false;

        return $this;
    }
721642
protected function getNewAlias($name, array $existing) {
		$i=1;
		$alias = $name;
		while(in_array($alias, $existing))
			$alias = $name.$i++;
		return $alias;
	}
674380
public function getOptions(array $excludes = null): array
    {
        if (! is_null($excludes)) {
            return array_filter($this->options, function ($key) use ($excludes) {
                return ! in_array($key, $excludes, true);
            }, ARRAY_FILTER_USE_KEY);
        }

        return $this->options;
    }
194418
public function exists($key)
    {
        // Checks if we need to verify a nested array
        if (preg_match('/\.+/', $key))
        {
            $keys   = explode('.', $key);
            $levels = count($keys);

            // Quick workaround
            if ($levels == 4)
            {
                return isset($this->params[$keys[0]][$keys[1]][$keys[2]][$keys3]);
            }
            else if ($levels == 3)
            {
                return isset($this->params[$keys[0]][$keys[1]][$keys[2]]);
            }
            else
            {
                return isset($this->params[$keys[0]][$keys[1]]);
            }
        }

        // Non-nested array validation
        if (array_key_exists($key, $this->params))
        {
            return true;
        }

        return false;
    }
623424
public function newPivot(array $attributes = [], $exists = false)
    {

        $using = $this->using;

        $pivot = $using ? $using::fromRawAttributes($this->parent, $attributes, $this->table, $exists)
                        : new MorphPivot($attributes, $this->parent, $this->table, $exists);

        $pivot->setPivotKeys($this->foreignKey, $this->relatedKey)
              ->setMorphType($this->morphType)
              ->setMorphClass($this->morphClass);

        return $pivot;
    }
339670
public function several(array $ids)
    {
        $result = [];
        foreach ($ids as $id) {
            if ($this->has($id)) {
                $result[$id] = $this->get($id);
            }
        }
        return $result;

    }
515355
protected function prepare_options($options)
    {
        $original = [];
        $toReplace = [];

        foreach ($options as $key => &$value) {
            if (is_array($value)) {
                $subArray = $this->prepare_options($value);
                $value = $subArray['options'];
                $original = array_merge($original, $subArray['original']);
                $toReplace = array_merge($toReplace, $subArray['toReplace']);
            } elseif (preg_match('/function.*?/', $value)) {
                $original[] = $value;
                $value = "%{$key}%";
                $toReplace[] = "\"{$value}\"";
            }
        }

        return compact('original', 'toReplace', 'options');
    }
1051611
public function build($attributes = [], $exists = false)
    {
        $class = $this->getEntityClass();

        // default values
        $schema = $this->getSchema();
        $defaults = $schema->getDefaultValues();
        $attributes = array_merge($defaults, $attributes);

        $entity = new $class($this, $attributes, $exists);
        if ($raikiri = $this->raikiri()) {
            $entity->setContainer($raikiri);
        }
        $entity->initialize();

        return $entity;
    }
962105
public function whereExists($name, $exists = true)
    {

        if ($exists) {
            $this->must[] = ["exists" => ["field" => $name]];
        } else {
            $this->must_not[] = ["exists" => ["field" => $name]];
        }

        return $this;
    }
1214191
public function getSort(array $available = [])
    {
        $sort = [];

        if ($input = $this->getInput('sort')) {
            $fields = explode(',', $input);

            foreach ($fields as $field) {
                if (substr($field, 0, 1) === '-') {
                    $field = substr($field, 1);
                    $order = 'desc';
                } else {
                    $order = 'asc';
                }

                $sort[$field] = $order;
            }

            $invalid = array_diff(array_keys($sort), $available);

            if (count($invalid)) {
                throw new InvalidParameterException(
                    'Invalid sort fields ['.implode(',', $invalid).']',
                    3,
                    null,
                    'sort'
                );
            }
        }

        return $sort;
    }
161354
protected function buildSelectItemsFromAvailableTemplate($availableTemplates)
    {
        $newItems = [];
        $newItems['Use Default'] = ['Use Default', null];
        foreach ($availableTemplates as $availableTemplate) {
            $label = isset($availableTemplate['label']) ? $availableTemplate['label'] : '';
            $value = isset($availableTemplate['file']) ? $availableTemplate['file'] : '';
            $newItems[$label] = [$label, $value];
        }

        return $newItems;
    }