AbstractObject::getFieldsEnum()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
4
 *
5
 * You are hereby granted a non-exclusive, worldwide, royalty-free license to
6
 * use, copy, modify, and distribute this software in source code or binary
7
 * form for use in connection with the web services and APIs provided by
8
 * Facebook.
9
 *
10
 * As with any software that integrates with the Facebook platform, your use
11
 * of this software is subject to the Facebook Developer Principles and
12
 * Policies [http://developers.facebook.com/policy/]. This copyright notice
13
 * shall be included in all copies or substantial portions of the software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
namespace FacebookAds\Object;
25
use FacebookAds\ApiConfig;
26
use FacebookAds\TypeChecker;
27
use FacebookAds\Enum\EmptyEnum;
28
class AbstractObject {
29
  /**
30
   * @var mixed[] set of key value pairs representing data
31
   */
32
  protected $data = array();
33
  protected $_type_checker;
0 ignored issues
show
Coding Style introduced by
$_type_checker does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
34
35
  public function __construct() {
36
    $this->data = static::getFieldsEnum()->getValuesMap();
37
    $this->_type_checker = new TypeChecker(
38
      static::getFieldTypes(), static::getReferencedEnums());
39
  }
40
41
  protected static function getFieldTypes() {
42
    $fields_enum = static::getFieldsEnum();
43
    if (method_exists($fields_enum, 'getFieldTypes')) {
44
      return $fields_enum->getFieldTypes();
0 ignored issues
show
Bug introduced by
The method getFieldTypes() does not seem to exist on object<FacebookAds\Enum\EmptyEnum>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
45
    } else {
46
      return array();
47
    }
48
  }
49
50
  protected static function getReferencedEnums() {
51
    return array();
52
  }
53
54
  /**
55
   * @param string $name
56
   * @param mixed $value
57
   */
58
  public function __set($name, $value) {
59
    if (ApiConfig::TYPE_CHECKER_STRICT_MODE
60
      && $this->_type_checker->isValidParam($name)
61
    ) {
62
      if ($this->_type_checker->isValidParamPair($name, $value)) {
63
        $this->data[$name] = $value;
64
      } else {
65
        throw new \InvalidArgumentException(
66
          $name." and ".$this->exportValue($value)
67
          ." are not a valid type value pair");
68
      }
69
    } else {
70
      $this->data[$name] = $value;
71
    }
72
    return $this;
73
  }
74
75
  /**
76
   * @param string $name
77
   * @return mixed
78
   * @throws \InvalidArgumentException
79
   */
80
  public function __get($name) {
81
    if (array_key_exists($name, $this->data)) {
82
      return $this->data[$name];
83
    } else {
84
      throw new \InvalidArgumentException(
85
        $name.' is not a field of '.get_class($this));
86
    }
87
  }
88
  /**
89
   * @param string $name
90
   * @return boolean
91
   */
92
  public function __isset($name) {
93
    return array_key_exists($name, $this->data);
94
  }
95
  /**
96
   * @param array
97
   * @return $this
98
   */
99
  public function setData(array $data) {
100
    foreach ($data as $key => $value) {
101
      $this->{$key} = $value;
102
    }
103
    // Handle class-specific situations
104
    if (method_exists($this, 'setDataTrigger')) {
105
      $this->setDataTrigger($data);
0 ignored issues
show
Bug introduced by
The method setDataTrigger() does not exist on FacebookAds\Object\AbstractObject. Did you maybe mean setData()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
106
    }
107
108
    return $this;
109
  }
110
  /**
111
   * Like setData but will skip field validation
112
   *
113
   * @param array
114
   * @return $this
115
   */
116
  public function setDataWithoutValidation(array $data) {
117
    foreach ($data as $key => $value) {
118
      $this->data[$key] = $value;
119
    }
120
    // Handle class-specific situations
121
    if (method_exists($this, 'setDataTrigger')) {
122
      $this->setDataTrigger($data);
0 ignored issues
show
Bug introduced by
The method setDataTrigger() does not exist on FacebookAds\Object\AbstractObject. Did you maybe mean setData()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
123
    }
124
    return $this;
125
  }
126
  /**
127
   * @return array
128
   */
129
  public function getData() {
130
    return $this->data;
131
  }
132
  /**
133
   * @param mixed $value
134
   * @return mixed
135
   */
136
  protected function exportValue($value) {
137
    $result = $value;
138
    switch (true) {
139
      case $value === null:
140
        break;
141
      case $value instanceof AbstractObject:
142
        $result = $value->exportData();
143
        break;
144
      case is_array($value):
145
        $result = array();
146
        foreach ($value as $key => $sub_value) {
147
          if ($sub_value !== null) {
148
            $result[$key] = $this->exportValue($sub_value);
149
          }
150
        }
151
        break;
152
    }
153
    return $result;
154
  }
155
  /**
156
   * @return array
157
   */
158
  public function exportData() {
159
    return $this->exportValue($this->data);
160
  }
161
  /**
162
   * @return array
163
   */
164
  public function exportAllData() {
165
    return $this->exportValue($this->data);
166
  }
167
  /**
168
   * @return EmptyEnum
169
   */
170
  public static function getFieldsEnum() {
171
    return EmptyEnum::getInstance();
172
  }
173
  /**
174
   * @return array
175
   */
176
  public static function getFields() {
177
    return static::getFieldsEnum()->getValues();
178
  }
179
  /**
180
   * @return string
181
   */
182
  public static function className() {
183
    return get_called_class();
184
  }
185
}
186