检查状态转换
我创建了一个简单的函数,它允许我确定更改可以具有哪些状态选项。例如,“待处理”状态只能更改为“有效”、“已拒绝”和“已删除”。
在我的班级中,我将这些存储为常量,这些常量引用状态列中的必要记录,以便在整个站点中进行比较。
我的“过渡”数组具有以下内容:-
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么别人的意见比你的更重要。如果这有效并且您对此感到满意,那就继续吧。为什么你选择这样做,可能有我们“局外人”看不到的原因。 (例如,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.