Completed
Push — master ( 70622d...3359b7 )
by Seth
04:38
created

SearchResult::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace smtech\StMarksSearch;
4
5
use JsonSerializable;
6
7
/**
8
 * An object representing a single search result
9
 *
10
 * @author Seth Battis <[email protected]>
11
 */
12
class SearchResult extends ParameterArrayConstructor implements JsonSerializable
13
{
14
    /**
15
     * URL of the search result
16
     * @var string
17
     */
18
    protected $url;
19
20
    /**
21
     * Description of result's relevance
22
     * @var Relevance
23
     */
24
    protected $relevance;
25
26
    /**
27
     * Human-readable title
28
     * @var string
29
     */
30
    protected $title;
31
32
    /**
33
     * Human-readable description
34
     *
35
     * Ideally 20-100 words, may be HTML-formatted (although links should be
36
     * stripped out).
37
     * @var string
38
     */
39
    protected $description;
40
41
    /**
42
     * Simplified description of search domain source of the result
43
     * @var SearchSource
44
     */
45
    protected $source;
46
47
    /**
48
     * Construct a SearchResult
49
     *
50
     * Expects an associative parameter array:
51
     *
52
     * ```
53
     * [
54
     *   'url' => URL of the search result as a string,
55
     *   'title' => Title of the search result as a string,
56
     *   'relevance' => instance of `Relevance`,
57
     *   'source' => instance of `SearchSource`,
58
     *   'description' => Optional: search result descriptin as a string
59
     * ]
60
     * ```
61
     *
62
     * @param mixed[string] $params
1 ignored issue
show
Documentation introduced by
The doc-type mixed[string] could not be parsed: Expected "]" at position 2, but found "string". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
63
     */
64
    public function __construct($params)
65
    {
66
        $this->requireParameter($params, 'url');
67
        $this->requireParameter($params, 'title');
68
        $this->requireParameter($params, 'relevance', Relevance::class);
69
        $this->requireParameter($params, 'source', SearchSource::class);
70
71
        $this->defaultParameter($params, 'description', '[no description available]');
72
73
        parent::__construct($params);
74
    }
75
76
    /**
77
     * Sort into order of descending relevance
78
     *
79
     * @param SearchResult[] $results
80
     * @return void
81
     */
82
    public static function sort(&$results)
83
    {
84
        usort($results, function (SearchResult $a, SearchResult $b) {
85
            if ($a->getRelevance()->getScore() < $b->getRelevance()->getScore()) {
1 ignored issue
show
Documentation Bug introduced by
The method getRelevance does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
                return 1;
87
            } elseif ($a->getRelevance()->getScore() > $b->getRelevance()->getScore()) {
1 ignored issue
show
Documentation Bug introduced by
The method getRelevance does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
88
                return -1;
89
            } else {
90
                return 0;
91
            }
92
        });
93
    }
94
95
    public function jsonSerialize()
96
    {
97
        return [
98
            'url' => $this->getUrl(),
1 ignored issue
show
Documentation Bug introduced by
The method getUrl does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
99
            'title' => $this->getTitle(),
1 ignored issue
show
Documentation Bug introduced by
The method getTitle does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
100
            'description' => $this->getDescription(),
1 ignored issue
show
Documentation Bug introduced by
The method getDescription does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
101
            'source' => $this->getSource()->jsonSerialize(),
1 ignored issue
show
Documentation Bug introduced by
The method getSource does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
102
            'relevance' => $this->getRelevance()->jsonSerialize()
1 ignored issue
show
Documentation Bug introduced by
The method getRelevance does not exist on object<smtech\StMarksSearch\SearchResult>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
103
        ];
104
    }
105
}
106