Completed
Pull Request — master (#5)
by Dan Michael O.
01:38
created

Model::isSearchResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Scriptotek\GoogleBooks;
4
5
abstract class Model implements \IteratorAggregate
6
{
7
    protected $client;
8
    private $data;
9
10
    public function __construct(GoogleBooks $client, \stdClass $data)
11
    {
12
        $this->client = $client;
13
        $this->data = $data;
14
    }
15
16
    /**
17
     * Returns true if the model is created from a search result response
18
     * (and thus do not contain all the data of the full record).
19
     *
20
     * @return bool
21
     */
22
    public function isSearchResult()
23
    {
24
        return $this->has('searchInfo');
25
    }
26
27
    /**
28
     * Expand a search result response object to a full record.
29
     */
30
    public function expandToFullRecord()
31
    {
32
        $url = $this->client->removeBaseUrl($this->selfLink);
0 ignored issues
show
Documentation introduced by
The property selfLink does not exist on object<Scriptotek\GoogleBooks\Model>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
33
        $this->data = $this->client->getItem($url);
34
    }
35
36
    /**
37
     * Special method that allows the object to be iterated over, for example
38
     * with a foreach statement.
39
     */
40
    public function getIterator()
41
    {
42
        return new \ArrayIterator($this->data);
43
    }
44
45
    /**
46
     * Get an item from an array using "dot" notation.
47
     *
48
     * @param  string  $key
49
     * @param  mixed   $default
50
     * @return mixed
51
     */
52 View Code Duplication
    public function get($key, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        $current = $this->data;
55
        foreach (explode('.', $key) as $segment) {
56
            if (!isset($current->{$segment})) {
57
                return $default;
58
            }
59
            $current = $current->{$segment};
60
        }
61
        return $current;
62
    }
63
64
    /**
65
     * Check if an item or items exist in an array using "dot" notation.
66
     *
67
     * @param  string  $key
68
     * @return mixed
69
     */
70 View Code Duplication
    public function has($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        $current = $this->data;
73
        foreach (explode('.', $key) as $segment) {
74
            if (!isset($current->{$segment})) {
75
                return false;
76
            }
77
            $current = $current->{$segment};
78
        }
79
        return true;
80
    }
81
82
    /**
83
     * Get a string representation of the object
84
     *
85
     * @return string
86
     */
87
    public function __toString()
88
    {
89
        return json_encode($this);
90
    }
91
92
    /**
93
     * Provide object-like access to the data.
94
     *
95
     * @param  string  $key
96
     * @return mixed
97
     */
98
    public function __get($key)
99
    {
100
        return $this->get($key);
101
    }
102
}
103