检查状态转换

发布于 2024-09-15 10:03:00 字数 775 浏览 1 评论 0原文

我创建了一个简单的函数,它允许我确定更改可以具有哪些状态选项。例如,“待处理”状态只能更改为“有效”、“已拒绝”和“已删除”。

在我的班级中,我将这些存储为常量,这些常量引用状态列中的必要记录,以便在整个站点中进行比较。

我的“过渡”数组具有以下内容:-

protected static $allowedTransitions=array(
 Booking::STATUS_ACTIVE=>array(Booking::STATUS_REVOKED),
     Booking::STATUS_PENDING=>array(Booking::STATUS_ACTIVE, Booking::STATUS_REJECTED, Booking::STATUS_REVOKED),
     Booking::STATUS_REJECTED=>array(Booking::STATUS_ACTIVE, Booking::STATUS_REVOKED),
     Booking::STATUS_REVOKED=>array()
);

对这种方法有何看法?在数组中添加常量似乎不是一个好主意。

为了检查是否可以满足状态请求,我将当前状态类型 Id 传递给 statusTransitions(),这允许我确定更改是否有效。

public static function statusTransitions($statusTypeId) {
 return self::$allowedTransitions[$statusTypeId];
}

I've created a simple function which allows me to determine which status options a change can have. For example a status of 'Pending' can only be changed to 'Active', 'Rejected' and 'Removed'.

In my class I store these as constants which reference the necessary record in a status column for purposes of comparison throughout the site.

My 'transition' array has the following:-

protected static $allowedTransitions=array(
 Booking::STATUS_ACTIVE=>array(Booking::STATUS_REVOKED),
     Booking::STATUS_PENDING=>array(Booking::STATUS_ACTIVE, Booking::STATUS_REJECTED, Booking::STATUS_REVOKED),
     Booking::STATUS_REJECTED=>array(Booking::STATUS_ACTIVE, Booking::STATUS_REVOKED),
     Booking::STATUS_REVOKED=>array()
);

What are the opinions on this approach? It doesn't seem like a very good idea to add constants in an array.

In order to check if a status request can be fulfilled, I pass the current status type Id to statusTransitions() which allows me to determine if the change is valid.

public static function statusTransitions($statusTypeId) {
 return self::$allowedTransitions[$statusTypeId];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

土豪我们做朋友吧 2024-09-22 10:03:00

我不确定为什么别人的意见比你的更重要。如果这有效并且您对此感到满意,那就继续吧。为什么你选择这样做,可能有我们“局外人”看不到的原因。 (例如,I18N 就是这样做的一个很好的理由)

我的观点是,只要你没有粗暴地做你不应该做的事情(比如抑制代码中的错误,这样你就不会做对你有用的事情)看看他们)。我不认为你的方法有什么问题。

I'm not certain why someone elses opinion would matter more than yours. If this works and you are comfortable with it, go with it. There could be reasons unseen to us "outsiders" why you chose to do it like this. (I18N for example would be a good reason to do it like this)

My opinion is, do what works for you as long as you are not grossly doing something you shouldn't (like suppressing errors in your code so you don't have to look at them). I don't see anything wrong with your approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文