Completed
Pull Request — master (#5)
by
unknown
01:27
created

GoogleMapsField::getCustomisationsVarName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Firesphere\GoogleMapsField\Forms;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Forms\Form;
7
use SilverStripe\Forms\HiddenField;
8
use SilverStripe\Forms\TextField;
9
use SilverStripe\SiteConfig\SiteConfig;
10
use SilverStripe\View\Requirements;
11
12
/**
13
 * Class GoogleMapsField
14
 * @package Firesphere\GoogleMapsField\Forms
15
 */
16
class GoogleMapsField extends TextField
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $extraFields = [
22
        'GoogleMapsLatField',
23
        'GoogleMapsLngField',
24
        'subpremise',
25
        'street_number',
26
        'route',
27
        'sublocality_level_1',
28
        'locality',
29
        'administrative_area_level_1',
30
        'country',
31
        'postal_code'
32
    ];
33
34
    /**
35
     * @var array
36
     */
37
    protected $customOptions = [];
38
39
    /**
40
     * @var string
41
     */
42
    protected $customisationsVarName;
43
44
    /**
45
     * GoogleMapsField constructor.
46
     * @param $name
47
     * @param null $title
48
     * @param string $value
49
     * @param null $maxLength
50
     * @param Form|null $form
51
     */
52
    public function __construct($name, $title = null, $value = '', $maxLength = null, Form $form = null)
53
    {
54
        $this->setAttribute('data-mapsfield', 'mapsfield');
55
        $this->setCustomisationsVarName($name . 'Customisations');
56
        parent::__construct($name, $title, $value, $maxLength, $form);
57
    }
58
59
    /**
60
     * @param array $properties
61
     * @return \SilverStripe\ORM\FieldType\DBHTMLText
62
     */
63
    public function field($properties = [])
64
    {
65
        $config = SiteConfig::current_site_config();
66
67
        Requirements::javascript(
68
            'https://maps.googleapis.com/maps/api/js?key=' . $config->MapsBrowserKey . '&libraries=places'
69
        );
70
        Requirements::javascript('firesphere/googlemapsfield:client/dist/main.js');
71
72
        return parent::Field($properties);
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function getHiddenFields()
79
    {
80
        $output = '';
81
        foreach ($this->extraFields as $field) {
82
            $output .= HiddenField::create($this->Name . $field)->Field();
0 ignored issues
show
Documentation introduced by
The property Name does not exist on object<Firesphere\Google...\Forms\GoogleMapsField>. 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...
83
        }
84
        return $output;
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function getExtraFields()
91
    {
92
        return $this->extraFields;
93
    }
94
95
    /**
96
     * @param array $extraFields
97
     */
98
    public function setExtraFields($extraFields)
99
    {
100
        $this->extraFields = $extraFields;
101
    }
102
103
    /**
104
     * @param string $field
105
     */
106
    public function addExtraField($field)
107
    {
108
        $this->extraFields[] = $field;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getCustomOptions()
115
    {
116
        return Convert::array2json($this->customOptions);
117
    }
118
119
    /**
120
     * @param array $customOptions
121
     */
122
    public function setCustomOptions($customOptions)
123
    {
124
        $this->customOptions = $customOptions;
125
    }
126
127
    /**
128
     * @return string
129
     */
130
    public function getCustomisationsVarName()
131
    {
132
        return $this->customisationsVarName;
133
    }
134
135
    /**
136
     * @param string $varName
137
     */
138
    public function setCustomisationsVarName($varName)
139
    {
140
        $this->customisationsVarName = $varName;
141
    }
142
}
143