/** * 前処理、入力値のバリデーションやログイン処理を行う * __before__メソッドを定義することで拡張する */ public function before() { list(, $method) = explode('::', $this->get_selected_pattern()['action']); $annon = \ebi\Annotation::get_method(get_class($this), $method, ['http_method', 'request', 'user_role']); if (isset($annon['http_method']['value']) && strtoupper($annon['http_method']['value']) != \ebi\Request::method()) { throw new \ebi\exception\BadMethodCallException('Method Not Allowed'); } if (isset($annon['request'])) { foreach ($annon['request'] as $k => $an) { if (isset($an['type'])) { try { \ebi\Validator::type($k, $this->in_vars($k), $an); } catch (\ebi\exception\InvalidArgumentException $e) { \ebi\Exceptions::add($e, $k); } \ebi\Validator::value($k, $this->in_vars($k), $an); } } } \ebi\Exceptions::throw_over(); if (method_exists($this, '__before__')) { $this->__before__(); } if ($this->has_object_plugin('before_flow_action_request')) { /** * 前処理 * @param \ebi\flow\Request $arg1 */ $this->call_object_plugin_funcs('before_flow_action_request', $this); } if (!$this->is_user_logged_in() && (isset($this->login_anon) || $this->has_object_plugin('login_condition'))) { $this->login_required(); } if ($this->is_user_logged_in() && (isset($annon['user_role']) || isset($this->login_anon['user_role']))) { if (!in_array(\ebi\UserRole::class, \ebi\Util::get_class_traits(get_class($this->user()))) || isset($this->login_anon['user_role']) && !in_array($this->login_anon['user_role'], $this->user()->get_role()) || isset($annon['user_role']['value']) && !in_array($annon['user_role']['value'], $this->user()->get_role())) { throw new \ebi\exception\NotPermittedException(); } } }
/** * 配列からプロパティに値をセットする * @param mixed{} $arg * @return $this */ public function set_props($arg) { if (isset($arg) && (is_array($arg) || is_object($arg) && $arg instanceof \Traversable)) { $vars = get_object_vars($this); foreach ($arg as $name => $value) { if ($name[0] != '_' && array_key_exists($name, $vars)) { try { $this->{$name}($value); } catch (\Exception $e) { \ebi\Exceptions::add(new \ebi\exception\InvalidArgumentException($e->getMessage()), $name); } } } } return $this; }
<?php \ebi\Exceptions::add(new \LogicException('AAA')); try { \ebi\Exceptions::throw_over(); fail('例外でるはず'); } catch (\ebi\Exceptions $e) { $i = 0; foreach ($e as $g => $exception) { $i++; } eq(1, $i); }
public function exceptions405() { \ebi\HttpHeader::send_status(405); \ebi\Exceptions::add(new \LogicException('Method Not Allowed')); \ebi\Exceptions::throw_over(); }
/** * * @param string $name * @param mixed $v * @param mixed{} $anon */ public static function value($name, $v, $anon) { $e_require = false; $get = function ($an) use($anon) { return isset($anon[$an]) ? $anon[$an] : null; }; if ($get('require') === true && ($v === '' || $v === null)) { \ebi\Exceptions::add(new \ebi\exception\RequiredException($name . ' required'), $name); } else { if ($v !== null) { switch ($get('type')) { case 'number': case 'integer': if ($get('min') !== null && (double) $get('min') > $v) { \ebi\Exceptions::add(new \ebi\exception\LengthException($name . ' less than minimum'), $name); } if ($get('max') !== null && (double) $get('max') < $v) { \ebi\Exceptions::add(new \ebi\exception\LengthException($name . ' exceeds maximum'), $name); } break; case 'text': case 'string': case 'alnum': if ($get('min') !== null && (int) $get('min') > mb_strlen($v)) { \ebi\Exceptions::add(new \ebi\exception\LengthException($name . ' less than minimum'), $name); } if ($get('max') !== null && (int) $get('max') < mb_strlen($v)) { \ebi\Exceptions::add(new \ebi\exception\LengthException($name . ' exceeds maximum'), $name); } break; } } } }