Nqxcode\LuceneSearch\Model\Config::optionalAttributes PHP Method

optionalAttributes() public method

Get optional attributes for indexing for model.
public optionalAttributes ( Model $model ) : array
$model Illuminate\Database\Eloquent\Model
return array
    public function optionalAttributes(Model $model)
    {
        $attributes = [];
        $c = $this->config($model);
        $option = snake_case(__FUNCTION__);
        if (!is_null(array_get($c, $option))) {
            $accessor = array_get($c, $option) === true ? $option : array_get($c, "{$option}.accessor");
            if (!is_null($accessor)) {
                $attributes = $model->{$accessor} ?: [];
                if (array_values($attributes) === $attributes) {
                    // Transform to the associative
                    $attributes = array_combine(array_map(function ($i) use($accessor) {
                        return "{$accessor}_{$i}";
                    }, array_keys($attributes)), $attributes);
                }
            }
        }
        $attributes = array_map(function ($value) {
            $boost = 1;
            if (is_array($value)) {
                $boost = array_get($value, 'boost', 1);
                $value = array_get($value, 'value');
            }
            return ['boost' => $boost, 'value' => $value];
        }, $attributes);
        return $attributes;
    }

Usage Example

 public function testOptionalAttributes()
 {
     $this->productMock->shouldReceive('getAttribute')->with('optional_attributes')->andReturn(['name' => 'value', 'boosted_name' => ['boost' => 0.1, 'value' => 'boosted_value']])->byDefault();
     $expected = ['name' => ['boost' => 1, 'value' => 'value'], 'boosted_name' => ['boost' => 0.1, 'value' => 'boosted_value']];
     $this->assertEquals($expected, $this->config->optionalAttributes($this->productMock));
     $this->productMock->shouldReceive('getAttribute')->with('optional_attributes')->andReturn([0 => 'value', 1 => ['boost' => 0.1, 'value' => 'boosted_value']])->byDefault();
     $expected = ['optional_attributes_0' => ['boost' => 1, 'value' => 'value'], 'optional_attributes_1' => ['boost' => 0.1, 'value' => 'boosted_value']];
     $this->assertEquals($expected, $this->config->optionalAttributes($this->productMock));
     $this->dummyMock->shouldReceive('getAttribute')->with('custom_optional_attributes')->andReturn(['name' => 'value', 'boosted_name' => ['boost' => 0.1, 'value' => 'boosted_value']])->byDefault();
     $expected = ['name' => ['boost' => 1, 'value' => 'value'], 'boosted_name' => ['boost' => 0.1, 'value' => 'boosted_value']];
     $this->assertEquals($expected, $this->config->optionalAttributes($this->dummyMock));
     $this->dummyMock->shouldReceive('getAttribute')->with('custom_optional_attributes')->andReturn([0 => 'value', 1 => ['boost' => 0.1, 'value' => 'boosted_value']])->byDefault();
     $expected = ['custom_optional_attributes_0' => ['boost' => 1, 'value' => 'value'], 'custom_optional_attributes_1' => ['boost' => 0.1, 'value' => 'boosted_value']];
     $this->assertEquals($expected, $this->config->optionalAttributes($this->dummyMock));
 }
All Usage Examples Of Nqxcode\LuceneSearch\Model\Config::optionalAttributes