Application.php
1 year ago
ApplicationCollection.php
1 year ago
BaseObject.php
1 year ago
Campaign.php
1 year ago
CampaignCapping.php
1 year ago
CampaignCollection.php
1 year ago
CampaignSchedule.php
1 year ago
CampaignSchedulePressure.php
1 year ago
CampaignScheduleUrlCriterion.php
1 year ago
CampaignStats.php
1 year ago
CampaignSuccessResponse.php
1 year ago
CampaignUrlFilters.php
1 year ago
Collection.php
1 year ago
DeliveriesCreateResponse.php
1 year ago
Event.php
1 year ago
FrequentFieldValues.php
1 year ago
GeoLocation.php
1 year ago
Installation.php
1 year ago
InstallationApplication.php
1 year ago
InstallationApplicationApple.php
1 year ago
InstallationCollection.php
1 year ago
InstallationDevice.php
1 year ago
InstallationDeviceCapabilities.php
1 year ago
InstallationDeviceConfiguration.php
1 year ago
InstallationPreferences.php
1 year ago
InstallationPushToken.php
1 year ago
Notification.php
1 year ago
NotificationAlert.php
1 year ago
NotificationAlertAndroid.php
1 year ago
NotificationAlertAndroidButton.php
1 year ago
NotificationAlertIos.php
1 year ago
NotificationAlertIosAttachment.php
1 year ago
NotificationAlertIosForeground.php
1 year ago
NotificationAlertWeb.php
1 year ago
NotificationAlertWebButton.php
1 year ago
NotificationButton.php
1 year ago
NotificationButtonAction.php
1 year ago
NotificationButtonActionEvent.php
1 year ago
NotificationInApp.php
1 year ago
NotificationInAppButton.php
1 year ago
NotificationInAppMap.php
1 year ago
NotificationInAppMapPlace.php
1 year ago
NotificationPush.php
1 year ago
NotificationPushAndroid.php
1 year ago
NotificationPushIos.php
1 year ago
NotificationPushWeb.php
1 year ago
NullObject.php
1 year ago
Pagination.php
1 year ago
Segment.php
1 year ago
SegmentCollection.php
1 year ago
SuccessResponse.php
1 year ago
User.php
1 year ago
WebSdkInitOptions.php
1 year ago
BaseObject.php
172 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WonderPush\Obj; |
| 4 | |
| 5 | if (count(get_included_files()) === 1) { http_response_code(403); exit(); } // Prevent direct access |
| 6 | |
| 7 | /** |
| 8 | * Base class for DTO objects. |
| 9 | * @noinspection PhpUndefinedClassInspection |
| 10 | */ |
| 11 | class BaseObject implements \WonderPush\Util\JsonSerializable { |
| 12 | |
| 13 | public function __construct($data = null) { |
| 14 | if ($data !== null) { |
| 15 | $this->updateFieldsFromData($data); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Factory style constructor. |
| 21 | * Compensates PHP 5.3's lack of (new Class())-> syntax support. |
| 22 | * @param array $data |
| 23 | * @return \static |
| 24 | */ |
| 25 | public static function _new($data = null) { |
| 26 | return new static($data); |
| 27 | } |
| 28 | |
| 29 | public function clearAllFields() { |
| 30 | try { |
| 31 | $class = new \ReflectionClass($this); |
| 32 | } catch (\ReflectionException $ex) { |
| 33 | /** @noinspection ForgottenDebugOutputInspection */ |
| 34 | error_log('[' . __METHOD__ . "()] Unexpected reflection exception\n" . $ex->getTraceAsString()); |
| 35 | return; |
| 36 | } |
| 37 | $methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC); |
| 38 | foreach ($methods as $method) { |
| 39 | /* @var $method \ReflectionMethod */ |
| 40 | if (!\WonderPush\Util\StringUtil::beginsWith($method->getName(), 'set')) { |
| 41 | continue; // not a setter |
| 42 | } |
| 43 | try { |
| 44 | $this->{$method->getName()}(null); |
| 45 | } catch (\Exception $ex) { |
| 46 | /** @noinspection ForgottenDebugOutputInspection */ |
| 47 | error_log('[' . __METHOD__ . '()] Exception caught while invoking ' . $method->getName() . "(null)\n" . $ex->getTraceAsString()); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Updates the fields present in $data. |
| 54 | * @param array|\stdClass $data |
| 55 | */ |
| 56 | protected function updateFieldsFromData($data) { |
| 57 | if (!is_array($data) && !is_object($data)) { |
| 58 | $ex = new \Exception(); |
| 59 | /** @noinspection ForgottenDebugOutputInspection */ |
| 60 | error_log('[' . __METHOD__ . '] Not an array or object: ' . json_encode($data) . "\n" . $ex->getTraceAsString()); |
| 61 | } |
| 62 | foreach ($data as $key => $value) { |
| 63 | $methodName = 'set' . ucfirst($key); |
| 64 | if (method_exists($this, $methodName)) { |
| 65 | if ($value === null) { |
| 66 | $value = NullObject::getInstance(); |
| 67 | } |
| 68 | $this->{$methodName}($value); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | protected function buildDataFromField($value) { |
| 74 | if ($value instanceof self) { |
| 75 | return $value->buildDataFromFields(); |
| 76 | } |
| 77 | if ($value instanceof \JsonSerializable) { |
| 78 | return $value->jsonSerialize(); |
| 79 | } |
| 80 | if (is_array($value)) { |
| 81 | $newValues = array(); |
| 82 | foreach ($value as $k => $v) { |
| 83 | $newValues[$k] = $this->buildDataFromField($v); |
| 84 | } |
| 85 | return $newValues; |
| 86 | } |
| 87 | return $value; |
| 88 | } |
| 89 | |
| 90 | protected function buildDataFromFields() { |
| 91 | $data = new \stdClass(); |
| 92 | |
| 93 | try { |
| 94 | $x = new \ReflectionClass(get_class($this)); |
| 95 | } catch (\ReflectionException $ex) { |
| 96 | /** @noinspection ForgottenDebugOutputInspection */ |
| 97 | error_log('[' . __METHOD__ . "()] Unexpected reflection exception\n" . $ex->getTraceAsString()); |
| 98 | return null; |
| 99 | } |
| 100 | $methods = $x->getMethods(\ReflectionMethod::IS_PUBLIC); |
| 101 | foreach ($methods as $method) { |
| 102 | if (!$method->isStatic() && \WonderPush\Util\StringUtil::beginsWith($method->name, 'get')) { |
| 103 | $field = substr($method->name, 3); |
| 104 | $field[0] = strtolower($field[0]); |
| 105 | $value = $method->invoke($this); |
| 106 | if ($value !== null) { |
| 107 | $data->{$field} = $this->buildDataFromField($value); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $data; |
| 113 | } |
| 114 | |
| 115 | protected static function instantiateForSetter($type, $data) { |
| 116 | if (\WonderPush\Util\StringUtil::endsWith($type, '[]')) { |
| 117 | $type = substr($type, 0, -2); |
| 118 | return array_map(function($item) use ($type) { |
| 119 | return BaseObject::instantiateForSetter($type, $item); |
| 120 | }, $data); |
| 121 | } |
| 122 | if (is_array($data) || $data instanceof \stdClass) { |
| 123 | return new $type($data); |
| 124 | } |
| 125 | return $data; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Implements JsonSerializable |
| 130 | * @return mixed |
| 131 | */ |
| 132 | #[\ReturnTypeWillChange] |
| 133 | public function jsonSerialize() { |
| 134 | return $this->toData(); |
| 135 | } |
| 136 | |
| 137 | public function __toString() { |
| 138 | if (defined('JSON_UNESCAPED_SLASHES')) { |
| 139 | // @codingStandardsIgnoreLine |
| 140 | return '<' . get_class($this) . '>' . json_encode($this, JSON_UNESCAPED_SLASHES); |
| 141 | } |
| 142 | return '<' . get_class($this) . '>' . json_encode($this); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns an object representation of this instance. |
| 147 | * |
| 148 | * Note that empty() returns null for an object with no properties, unlike for an empty array. |
| 149 | * |
| 150 | * @return \stdClass |
| 151 | */ |
| 152 | public function toData() { |
| 153 | return $this->buildDataFromFields(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Returns an array representation of this instance. |
| 158 | * |
| 159 | * Note that when serializing an empty instance to JSON you will get `[]` instead of `{}`. |
| 160 | * |
| 161 | * @return array |
| 162 | */ |
| 163 | public function toArray() { |
| 164 | return (array)$this->buildDataFromFields(); |
| 165 | } |
| 166 | |
| 167 | public function deepClone() { |
| 168 | return unserialize(serialize($this)); |
| 169 | } |
| 170 | |
| 171 | } |
| 172 |