toDiscountType(), value: $this->toDiscountValue(), is_old_price_displayed: $this->boolean('is_old_price_displayed'), period: $this->toDatePeriod(), associatedProductsIds: $this->toAssociatedProductsIds(), ); } public function authorize(): bool { return $this->authService->check(); } #[\Override] protected function prepareForValidation(): void { $this->merge([ 'is_old_price_displayed' => $this->boolean('is_old_price_displayed'), 'associated_products' => $this->array('associated_products'), ]); } /** @return array */ public function getAssociatedProductsIds(): array { $filtered = array_filter( $this->array('associated_products'), fn ($id) => is_string($id) || is_int($id) ); return array_map( fn ($id) => (string) $id, $filtered ); } /** @return array */ public function toAssociatedProductsIds(): array { return $this->getAssociatedProductsIds(); } public function toDiscountType(): DiscountType { /** @var DiscountType */ return $this->enum('type', DiscountType::class); } public function getDiscountValue(): DiscountValue { $type = $this->enum('type', DiscountType::class); if ($type === DiscountType::PERCENT) { return DiscountValue::fromPercentage( $this->integer('value') ); } return DiscountValue::fromAmount( new Money( intval($this->integer('value')), $this->currency, ) ); } public function toDiscountValue(): DiscountValue { return $this->getDiscountValue(); } public function toDatePeriod(): DatePeriod { $start = $this->filled('date_start') ? new \DateTimeImmutable($this->string('date_start')->value(), new \DateTimeZone('UTC')) : null; $end = $this->filled('date_end') ? new \DateTimeImmutable($this->string('date_end')->value(), new \DateTimeZone('UTC')) : null; return new DatePeriod($start, $end); } /** @return array */ public function rules(): array { return [ 'type' => ['required', new Enum(DiscountType::class)], 'value' => [ 'required', 'integer', 'min:0', Rule::when( $this->input('type') == DiscountType::PERCENT->value(), ['max:100'] ), ], 'is_old_price_displayed' => ['required', 'boolean'], 'date_start' => ['nullable', 'date_format:Y-m-d\TH:i'], 'date_end' => ['nullable', 'date_format:Y-m-d\TH:i', 'after:date_start'], 'client_timezone' => ['required', 'timezone'], 'associated_products' => ['present', 'array'], 'associated_products.*' => ['string', 'exists:products,id'], ]; } #[\Override] protected function passedValidation() { $this->mergeLocalDatetimeAsUtc('date_start'); $this->mergeLocalDatetimeAsUtc('date_end'); } #[\Override] public function attributes(): array { return [ 'type' => trans('validation.custom.attributes.discount_type'), 'value' => trans('validation.custom.attributes.discount_value'), 'is_old_price_displayed' => trans('validation.custom.attributes.is_old_price_displayed'), 'date_start' => trans('validation.custom.attributes.date_start_show'), 'date_end' => trans('validation.custom.attributes.date_end_show'), 'associated_products' => trans('validation.custom.attributes.associated_products'), ]; } }