Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CarListSorter
- {
- public const DIRECTION_ASC = 'ASC';
- public const DIRECTION_DESC = 'DESC';
- private static $properties = [
- 'brandName', 'modelName', 'auctionName', 'auctionDateTime', 'transmissionType', 'gearType', 'year',
- 'enginePowerHp', 'mileageKm', 'color', 'lot', 'bodyModel', 'rate', 'startPriceJpy', 'finishPriceJpy',
- 'avgPriceJpy'
- ];
- private $sorting = [];
- public function __isset($name)
- {
- if (!in_array($name, self::$properties))
- throw new LogicException("Property '{$name}' is not available for list sorting");
- return isset($this->sorting[$name]);
- }
- public function __set($name, ?string $value = null)
- {
- // todo: valid options in message
- if ($value !== null && !in_array($value, [self::DIRECTION_ASC, self::DIRECTION_DESC], true))
- throw new LogicException("Sorting value '{$value}' is incorrect.");
- if (!in_array($name, $this->sorting) && $value !== null)
- $this->sorting[$name] = $value;
- if ($value === null)
- unset($this->sorting[$name]);
- }
- public function __get($name)
- {
- if (!in_array($name, self::$properties))
- throw new LogicException("Property '{$name}' is not available for list sorting");
- return $this->sorting[$name] ?? null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement