| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID\Util\Forms; |
| 24: | |
| 25: | use SimpleID\Util\ArrayWrapper; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | class FormState extends ArrayWrapper { |
| 34: | |
| 35: | public const REQUEST_KEY = 'rq'; |
| 36: | public const RESPONSE_KEY = 'rs'; |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | public function __construct($data = []) { |
| 42: | if (!is_array($data)) $data = []; |
| 43: | parent::__construct($data); |
| 44: | } |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | public function setRequest(ArrayWrapper $request) { |
| 50: | $this->offsetSet(self::REQUEST_KEY, $request); |
| 51: | } |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | public function getRequest() { |
| 57: | return $this->offsetGet(self::REQUEST_KEY); |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | public function setResponse(ArrayWrapper $response) { |
| 64: | $this->offsetSet(self::RESPONSE_KEY, $response); |
| 65: | } |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | public function getResponse(): ArrayWrapper { |
| 71: | return $this->offsetGet(self::RESPONSE_KEY); |
| 72: | } |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | public function encode() { |
| 80: | $data = $this->container; |
| 81: | |
| 82: | if (isset($data[self::REQUEST_KEY]) && $data[self::REQUEST_KEY] instanceof ArrayWrapper) { |
| 83: | $data[self::REQUEST_KEY] = $data[self::REQUEST_KEY]->toArray(); |
| 84: | } |
| 85: | if (isset($data[self::RESPONSE_KEY]) && $data[self::RESPONSE_KEY] instanceof ArrayWrapper) { |
| 86: | $data[self::RESPONSE_KEY] = $data[self::RESPONSE_KEY]->toArray(); |
| 87: | } |
| 88: | return $data; |
| 89: | } |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | public static function decode($data, $request_class = null, $response_class = null) { |
| 100: | if (!is_array($data)) return new FormState(); |
| 101: | |
| 102: | if (isset($data[self::REQUEST_KEY]) && $request_class != null) { |
| 103: | $request = new $request_class($data[self::REQUEST_KEY]); |
| 104: | } else { |
| 105: | $request = null; |
| 106: | } |
| 107: | if ($request != null) $data[self::REQUEST_KEY] = $request; |
| 108: | |
| 109: | if (isset($data[self::RESPONSE_KEY]) && $response_class != null) { |
| 110: | $data[self::RESPONSE_KEY] = new $response_class($request, $data[self::RESPONSE_KEY]); |
| 111: | } |
| 112: | return new FormState($data); |
| 113: | } |
| 114: | } |
| 115: | |
| 116: | ?> |