'required|int', 'support_code' => 'required|int', 'label_type_support' => 'required|string|min:2|max:255', 'order_date' => 'required|date_format:Y/m/d H:i:s', 'order_num' => 'required|int', 'state_order' => ['required'], 'package_unit_weight' => 'required|int' ]; public function __construct(array $properties = []) { parent::__construct(); if (count(self::$validation['state_order']) < 2) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed self::$validation['state_order'][] = function ($attribute, $value, $fail) { if (!is_object($value) or !\in_array($value, ArcadiaOrderType::toArray())) { $type = \gettype($value); $fail("State order ($type)$value is not in ArcadiaOrderType enum list"); } }; } $this->fill($properties); return $this; } public function __get(string $field) { $props = \array_diff( $this->modelFields, $this->invisibleFields ); if (in_array($field, $props)) { if ($field === 'state_order' and !\is_object($this->{$field})) { return ArcadiaOrderType::make($this->{$field}); } } return parent::__get($field); } /** * Set prop * * @param string $field * @param mixed $value * @throws TypeError if the field has an incorrect value * @return mixed|null The input value or null is this prop does not exists */ public function __set(string $field, $value) { $out = parent::__set($field, $value); if (in_array($field, $this->modelFields)) { $this->updatedValues[$field] = (\is_object($value) and get_class($value) === ArcadiaOrderType::class) ? $value->value : $value; $this->setRawProperty($this, $field, $this->updatedValues[$field]); } return $out; } /** * Fill data into this model * using validation rules * * @param array $properties * @return void * @throws TypeError if any valid is not validated */ public function fill(array $properties) { if (isset($properties['state_order'])) { try { $properties['state_order'] = (\is_int($properties['state_order']) or \ctype_digit($properties['state_order'])) ? ArcadiaOrderType::make((int)$properties['state_order']) : $properties['state_order']; } catch (TypeError $e) { // cast anyway $properties['state_order'] = (int)$properties['state_order']; } } parent::fill($properties); } /** * Validate all model properties * * @return array the validated values */ protected function validateAllValues(array $ignore = []): array { $out = []; if (!\in_array('state_order', $ignore)) { try { $this->validateValue('state_order', ArcadiaOrderType::make(self::getRawProperty($this, 'state_order'))); } catch (TypeError $e) { // this to throw validation error if failure way $this->validateValue('state_order', self::getRawProperty($this, 'state_order')); } $out['state_order'] = self::getRawProperty($this, 'state_order'); } return array_merge($out, parent::validateAllValues(array_merge(['state_order'], $ignore))); } // PUBLIC METHODS /** * Send The actual mail * * @return boolean true if succeeded * @throws \LDC\LDCCRM\Exceptions\CrmException * if an error occurs on server side or on getting response * @throws \LDC\LDCCRM\Exceptions\ApiErrorException * If any error or \LDC\LDCCRM\Enums\ApiError occurs */ public function send(Customer $customer): bool { // validate customer hashcode $refClass = new ReflectionClass(\get_class($customer)); $refMeth = $refClass->getMethod('validateValue'); $refMeth->setAccessible(true); $refMeth->invokeArgs($customer, ['hashcode', $customer->hashcode]); // Send mail $data = $this->validateAllValues(); static::setUpApiInterface(); // set apiInterface $request = Utils::getResquest('apiV2.arcadia.outgoingMailDefault', [ 'customer' => $customer->hashcode ], $data); $response = static::$apiInterface->call($request); if ($status = $response->status->equals(ApiStatus::OK())) { $this->updatedValues = []; } $this->isNew = !$status; return $status; } /** * Serialize this object to an array * * @return array */ public function jsonSerialize() { return [ 'operation' => $this->operation, 'support_code' => $this->support_code, 'label_type_support' => $this->label_type_support, 'order_date' => $this->order_date, 'order_num' => $this->order_num, 'state_order' => $this->state_order, 'package_unit_weight' => $this->package_unit_weight ]; } }