CoolCMS API

ResponseCurrencyGuardTrait

Currency guard for advert responses and company inquiries (Bazaar and Comcat) in the Api layer.

The currency is required only when a price (budget) is filled, and the "Credits" currency or any other internal currency must not be a price currency. Forms enforce this too, but a response is also written by the admin and by the external :Api:*:Response:save, so the rule must hold in the one place every write passes through (beforeSave()):

  • no price: the currency is dropped (null; a currency preselected by the form means nothing),
  • price without currency: VALIDATION_FAILED (errorCode = currencyRequired),
  • currency that is not a public monetary one: VALIDATION_FAILED (errorCode = currencyNotAllowed).

Zero means "no price": 0 / 0.000 is normalized to null and the currency is dropped. Otherwise a zero would count as a filled price with a required currency, while the Bazaar e-mail hides it (> 0) and Comcat shows it in the e-mail and detail.

An update checks only a change: the classification of a currency can change after a response is inserted (an admin switches internal / monetary). Editing the text, read or deleted of a historical response must not fail on today's classification, so the guard applies only when the price/currency pair differs from the stored state. A new disallowed currency still cannot be introduced through an update.

IMPORTANT: call after ResponseColumnGuardTrait::guardResponseColumnLimits(), which normalizes an empty price to null; otherwise "filled price" is ambiguous here.

Tags
requires

ResponseManager providing:

  • $currencyManager (App\UI\Api\System\Models\Managers\CurrencyManager)
  • a service with findPersistedValues(int $id, array $fields) (update only)

Table of Contents

Methods

guardResponseCurrency()  : Result|null
hasResponsePriceChanged()  : bool
Tells whether the price or currency differs from the stored row. Without an id or a readable row it counts as changed: the guard then checks rather than let an unverified currency through.

Methods

hasResponsePriceChanged()

Tells whether the price or currency differs from the stored row. Without an id or a readable row it counts as changed: the guard then checks rather than let an unverified currency through.

private hasResponsePriceChanged(Response|Response $response) : bool
Parameters
$response : Response|Response
Return values
bool
On this page

Search results