Completed
Push — signal_search_issues ( 5556b2...f328ba )
by André
63:06 queued 07:22
created

Aggregate::map()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the Aggregate document field value mapper class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 *
9
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\Search\Elasticsearch\Content\FieldValueMapper;
12
13
use eZ\Publish\Core\Search\Elasticsearch\Content\FieldValueMapper;
14
use eZ\Publish\SPI\Search\Field;
15
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
16
17
/**
18
 * Maps raw document field values to something Elasticsearch can index.
19
 */
20
class Aggregate extends FieldValueMapper
21
{
22
    /**
23
     * Array of available mappers.
24
     *
25
     * @var \eZ\Publish\Core\Search\Elasticsearch\Content\FieldValueMapper[]
26
     */
27
    protected $mappers = array();
28
29
    /**
30
     * Construct from optional mapper array.
31
     *
32
     * @param \eZ\Publish\Core\Search\Elasticsearch\Content\FieldValueMapper[] $mappers
33
     */
34
    public function __construct(array $mappers = array())
35
    {
36
        foreach ($mappers as $mapper) {
37
            $this->addMapper($mapper);
38
        }
39
    }
40
41
    /**
42
     * Adds mapper.
43
     *
44
     * @param \eZ\Publish\Core\Search\Elasticsearch\Content\FieldValueMapper $mapper
45
     */
46
    public function addMapper(FieldValueMapper $mapper)
47
    {
48
        $this->mappers[] = $mapper;
49
    }
50
51
    /**
52
     * Check if field can be mapped.
53
     *
54
     * @param \eZ\Publish\SPI\Search\Field $field
55
     *
56
     * @return bool
57
     */
58
    public function canMap(Field $field)
59
    {
60
        return true;
61
    }
62
63
    /**
64
     * Map field value to a proper Elasticsearch representation.
65
     *
66
     * @throws \eZ\Publish\API\Repository\Exceptions\NotImplementedException
67
     *
68
     * @param \eZ\Publish\SPI\Search\Field $field
69
     *
70
     * @return mixed
71
     */
72
    public function map(Field $field)
73
    {
74
        foreach ($this->mappers as $mapper) {
75
            if ($mapper->canMap($field)) {
76
                return $mapper->map($field);
77
            }
78
        }
79
80
        throw new NotImplementedException(
81
            'No mapper available for: ' . get_class($field->type)
0 ignored issues
show
Documentation introduced by
The property $type is declared protected in eZ\Publish\SPI\Search\Field. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
82
        );
83
    }
84
}
85