Parse field parameter.

@param array $fieldsets Array of fields to include. It must be an array whose keys
are resource types and values an array or a string
of the fields to return, separated by a comma

@return $this
352782
public function parseFieldsets(array $fieldsets)
    {
        $this->requestedFieldsets = [];
        foreach ($fieldsets as $type => $fields) {
            if (is_string($fields)) {
                $fields = explode(',', $fields);
            }

            //Remove empty and repeated fields
            $this->requestedFieldsets[$type] = array_unique(array_filter($fields));
        }
        return $this;
    }
531733
public function addFields($fields)
    {
        $fieldsArr = $this->parseFields($fields);
        !is_array($this->fields) ? $this->fields = [] : null;
        $this->fields = array_unique(ArrayLib::array_add($this->fields, $fieldsArr));
        return $this;
    }
1316396
public function useFields($fields)
    {
        if (is_string($fields)) {
            $fields = explode(',', $fields);
        }
        $this->fields = $fields;

        return $this;
    }
905061
public function field(...$fields)
    {
        if(!isset($fields[1]) && is_array($fields[0]))
        {
            $this->option->field = array_merge($this->option->field, $fields[0]);
        }
        else
        {
            $this->option->field = array_merge($this->option->field, $fields);
        }
        return $this;
    }
605376
public function set_parent(Fieldset $fieldset)
	{
		if ( ! empty($this->fieldset_parent))
		{
			throw new \RuntimeException('Fieldset already has a parent, belongs to "'.$this->parent()->name.'".');
		}

		$children = $fieldset->children();
		while ($child = array_shift($children))
		{
			if ($child === $this)
			{
				throw new \RuntimeException('Circular reference detected, adding a Fieldset that\'s already a child as a parent.');
			}
			$children = array_merge($child->children(), $children);
		}

		$this->fieldset_parent = $fieldset;
		$fieldset->add_child($this);
		return $this;
	}
340015
protected function setFieldset($values)
    {
        foreach ($values as $name => $field) {
            $this->fields[$name] = $field;
        }
    }
1195722
public function parse($name, $fields)
    {
        list($action, $tableName) = $this->parseMigrationName($name);

        $this->action = $action;
        $this->tableName = $tableName;
        $this->fields = $fields;

        return $this;
    }
1250679
public function _parseParams(array $matches)
    {
        $params = array();
        foreach ($matches as $k => $v) {
            if (is_numeric($k)) {
                if ($k === 0) {
                    continue;
                }
                $params[] = $v;
            } else {
                $params[$k] = $v;
            }
        }
        return new Set($params);
    }
484049
public function fields($fields)
    {
        if (is_array($fields) || $fields === null) {
            $this->fields = $fields;
        } else {
            $this->fields = func_get_args();
        }
        return $this;
    }
369645
public function setPostFields($fields)
    {
        // Form raw string version of fields
        $raw   = '';
        $first = true;

        foreach ($fields as $key => $value) {
            if (!$first) {
                $raw .= '&';
            }

            $raw .= $key . '=' . $value;

            $first = false;
        }

        $this->setOpt(CURLOPT_POST, count($fields));
        $this->setOpt(CURLOPT_POSTFIELDS, $raw);

        return $this;
    }
942680
public function fields( $fields )
	{
		if ( !is_array( $fields ) && !is_null( $fields ) )
		{
			$fields = array( $fields );
		}
		
		// do nothing if we get nothing
		if ( empty( $fields ) || $fields == array( '*' ) )
		{
			$this->fields = array(); return $this;
		}
		
		$this->fields = $fields;

		// return self so we can continue running the next function
		return $this;
	}
963640
private function parseFields($fields)
    {
        // If fields parameter is set, parse it using regex
        preg_match_all(static::FIELDS_REGEX, $fields, $matches);

        if (!empty($matches[0])) {
            foreach ($matches[0] as $match) {

                preg_match_all(static::FIELD_PARTS_REGEX, $match, $parts);

                $fieldName = $parts[1][0];

                if (Str::contains($fieldName, ":") || call_user_func($this->model . "::relationExists", $fieldName)) {
                    // If field name has a colon, we assume its a relations
                    // OR
                    // If method with field name exists in the class, we assume its a relation
                    // This is default laravel behavior

                    $limit = ($parts[3][0] == "") ? config("api.defaultLimit") : $parts[3][0];
                    $offset = ($parts[4][0] == "") ? 0 : $parts[4][0];
                    $order = ($parts[5][0] == "chronological") ? "chronological" : "reverse_chronological";

                    if (!empty($parts[7][0])) {
                        $subFields = explode(",", $parts[7][0]);
                        // This indicates if user specified fields for relation or not
                        $userSpecifiedFields = true;
                    }
                    else {
                        $subFields = [];
                        $userSpecifiedFields = false;
                    }

                    $fieldName = str_replace(":", ".", $fieldName);

                    if (!isset($this->relations[$fieldName])) {
                        $this->relations[$fieldName] = [
                            "limit" => $limit,
                            "offset" => $offset,
                            "order" => $order,
                            "fields" => $subFields,
                            "userSpecifiedFields" => $userSpecifiedFields
                        ];
                    }
                    else {
                        $this->relations[$fieldName]["limit"] = $limit;
                        $this->relations[$fieldName]["offset"] = $offset;
                        $this->relations[$fieldName]["order"] = $order;
                        $this->relations[$fieldName]["fields"] = array_merge($this->relations[$fieldName]["fields"], $subFields);
                    }

                    // We also need to add the relation's foreign key field to select. If we don't,
                    // relations always return null

                    if (Str::contains($fieldName, ".")) {

                        $relationNameParts = explode('.', $fieldName);
                        $model = $this->model;

                        $relation = null;

                        foreach ($relationNameParts as $rp) {
                            $relation = call_user_func([ new $model(), $rp]);
                            $model = $relation->getRelated();
                        }

                        // Its a multi level relations
                        $fieldParts = explode(".", $fieldName);

                        if ($relation instanceof BelongsTo) {
                            $singular = $relation->getForeignKey();
                        }
                        else if ($relation instanceof HasOne || $relation instanceof HasMany) {
                            $singular = $relation->getForeignKeyName();
                        }

                        // Unset last element of array
                        unset($fieldParts[count($fieldParts) - 1]);

                        $parent = implode(".", $fieldParts);

                        if ($relation instanceof HasOne || $relation instanceof HasMany) {
                            // For hasMany and HasOne, the foreign key is in current relation table, not in parent
                            $this->relations[$fieldName]["fields"][] = $singular;
                        }
                        else {
                            // The parent might already been set because we cannot rely on order
                            // in which user sends relations in request
                            if (!isset($this->relations[$parent])) {
                                $this->relations[$parent] = [
                                    "limit" => config("api.defaultLimit"),
                                    "offset" => 0,
                                    "order" => "chronological",
                                    "fields" => isset($singular) ? [$singular] : [],
                                    "userSpecifiedFields" => true
                                ];
                            }
                            else {
                                if (isset($singular)) {
                                    $this->relations[$parent]["fields"][] = $singular;
                                }
                            }
                        }

                        if ($relation instanceof BelongsTo) {
                            $this->relations[$fieldName]["limit"] = max($this->relations[$fieldName]["limit"], $this->relations[$parent]["limit"]);
                        }
                        else if ($relation instanceof HasMany) {
                            $this->relations[$fieldName]["limit"] = $this->relations[$fieldName]["limit"] * $this->relations[$parent]["limit"];
                        }
                    }
                    else {

                        $relation = call_user_func([new $this->model(), $fieldName]);

                        if ($relation instanceof HasOne) {
                            $keyField = explode(".", $relation->getQualifiedParentKeyName())[1];
                        }
                        else if ($relation instanceof BelongsTo) {
                            $keyField = explode(".", $relation->getQualifiedForeignKey())[1];
                        }

                        if (isset($keyField) && !in_array($keyField, $this->fields)) {
                            $this->fields[] = $keyField;
                        }

                        if ($relation instanceof BelongsTo) {
                            $this->relations[$fieldName]["limit"] = max($this->relations[$fieldName]["limit"], $this->limit);
                        }
                        else if ($relation instanceof HasMany) {
                            $this->relations[$fieldName]["limit"] = $this->relations[$fieldName]["limit"] * $this->limit;
                        }
                    }

                }
                else {
                    // Else, its a normal field
                    $this->fields[] = $fieldName;
                }
            }
        }
    }
233225
public function fields($fields = [])
    {
        $this->builder->select([]);
        $this->casts = [];

        if (empty($fields)) {
            foreach ($this->model->fields() as $field) {
                $this->assignField($field);
            }

            return $this;
        }

        foreach ($fields as $field) {
            $this->assignField($this->model->field($field));
        }

        return $this;
    }
406814
public function setMultiFields(array $fields = [])
    {
        if (empty($fields)) {
            unset($this->query['multi-fields']);
        } else {
            $this->query['multi-fields'] = $fields;
        }
        return $this;
    }
939863
public function getFieldset($set = null)
	{
		$fields = array();

		// Get all of the field elements in the fieldset.
		if ($set)
		{
			$elements = $this->findFieldsByFieldset($set);
		}
		else
		// Get all fields.
		{
			$elements = $this->findFieldsByGroup();
		}

		// If no field elements were found return empty.
		if (empty($elements))
		{
			return $fields;
		}

		// Build the result array from the found field elements.
		/** @var \SimpleXMLElement $element */
		foreach ($elements as $element)
		{
			// Get the field groups for the element.
			$attrs = $element->xpath('ancestor::fields[@name]/@name');
			$groups = array_map('strval', $attrs ? $attrs : array());
			$group = implode('.', $groups);

			// If the field is successfully loaded add it to the result array.
			if ($field = $this->loadField($element, $group))
			{
				$fields[$field->id] = $field;
			}
		}

		return $fields;
	}
838223
public function findByFields($fieldValues)
    {
        if (empty($fieldValues)) {
            throw new \Exception('No field values specified');
        }
        $this->primeCommandArray();
        $this->addToCommandArray($fieldValues);
        $this->addToCommandArray(['-find' => null]);
        return $this;
    }
232903
private function extractFields(array $params)
    {
        if (false === $this->issetNotEmpty(self::PARAM_FIELDSETS, $params)) {
            return $this;
        }
        $fields = $params[self::PARAM_FIELDSETS];
        if (!is_array($fields)) {
            throw RestException::invalidQueryParam(self::PARAM_FIELDSETS, 'The field parameter must be an array of entity type keys to fields.');
        }
        foreach ($fields as $entityType => $string) {
            $this->fields[$entityType] = explode(',', $string);
        }
        return $this;
    }