@@ -39,132 +39,132 @@ |
||
39 | 39 | */ |
40 | 40 | class TemplateResponse extends Response { |
41 | 41 | |
42 | - const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts'; |
|
43 | - const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn'; |
|
44 | - |
|
45 | - /** |
|
46 | - * name of the template |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - protected $templateName; |
|
50 | - |
|
51 | - /** |
|
52 | - * parameters |
|
53 | - * @var array |
|
54 | - */ |
|
55 | - protected $params; |
|
56 | - |
|
57 | - /** |
|
58 | - * rendering type (admin, user, blank) |
|
59 | - * @var string |
|
60 | - */ |
|
61 | - protected $renderAs; |
|
62 | - |
|
63 | - /** |
|
64 | - * app name |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - protected $appName; |
|
68 | - |
|
69 | - /** |
|
70 | - * constructor of TemplateResponse |
|
71 | - * @param string $appName the name of the app to load the template from |
|
72 | - * @param string $templateName the name of the template |
|
73 | - * @param array $params an array of parameters which should be passed to the |
|
74 | - * template |
|
75 | - * @param string $renderAs how the page should be rendered, defaults to user |
|
76 | - * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0 |
|
77 | - */ |
|
78 | - public function __construct($appName, $templateName, array $params=[], |
|
79 | - $renderAs='user') { |
|
80 | - parent::__construct(); |
|
81 | - |
|
82 | - $this->templateName = $templateName; |
|
83 | - $this->appName = $appName; |
|
84 | - $this->params = $params; |
|
85 | - $this->renderAs = $renderAs; |
|
86 | - |
|
87 | - $this->setContentSecurityPolicy(new ContentSecurityPolicy()); |
|
88 | - $this->setFeaturePolicy(new FeaturePolicy()); |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Sets template parameters |
|
94 | - * @param array $params an array with key => value structure which sets template |
|
95 | - * variables |
|
96 | - * @return TemplateResponse Reference to this object |
|
97 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
98 | - */ |
|
99 | - public function setParams(array $params) { |
|
100 | - $this->params = $params; |
|
101 | - |
|
102 | - return $this; |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - /** |
|
107 | - * Used for accessing the set parameters |
|
108 | - * @return array the params |
|
109 | - * @since 6.0.0 |
|
110 | - */ |
|
111 | - public function getParams() { |
|
112 | - return $this->params; |
|
113 | - } |
|
114 | - |
|
115 | - |
|
116 | - /** |
|
117 | - * Used for accessing the name of the set template |
|
118 | - * @return string the name of the used template |
|
119 | - * @since 6.0.0 |
|
120 | - */ |
|
121 | - public function getTemplateName() { |
|
122 | - return $this->templateName; |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * Sets the template page |
|
128 | - * @param string $renderAs admin, user or blank. Admin also prints the admin |
|
129 | - * settings header and footer, user renders the normal |
|
130 | - * normal page including footer and header and blank |
|
131 | - * just renders the plain template |
|
132 | - * @return TemplateResponse Reference to this object |
|
133 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
134 | - */ |
|
135 | - public function renderAs($renderAs) { |
|
136 | - $this->renderAs = $renderAs; |
|
137 | - |
|
138 | - return $this; |
|
139 | - } |
|
140 | - |
|
141 | - |
|
142 | - /** |
|
143 | - * Returns the set renderAs |
|
144 | - * @return string the renderAs value |
|
145 | - * @since 6.0.0 |
|
146 | - */ |
|
147 | - public function getRenderAs() { |
|
148 | - return $this->renderAs; |
|
149 | - } |
|
150 | - |
|
151 | - |
|
152 | - /** |
|
153 | - * Returns the rendered html |
|
154 | - * @return string the rendered html |
|
155 | - * @since 6.0.0 |
|
156 | - */ |
|
157 | - public function render() { |
|
158 | - // \OCP\Template needs an empty string instead of 'blank' for an unwrapped response |
|
159 | - $renderAs = $this->renderAs === 'blank' ? '' : $this->renderAs; |
|
160 | - |
|
161 | - $template = new \OCP\Template($this->appName, $this->templateName, $renderAs); |
|
162 | - |
|
163 | - foreach($this->params as $key => $value){ |
|
164 | - $template->assign($key, $value); |
|
165 | - } |
|
166 | - |
|
167 | - return $template->fetchPage($this->params); |
|
168 | - } |
|
42 | + const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts'; |
|
43 | + const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn'; |
|
44 | + |
|
45 | + /** |
|
46 | + * name of the template |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + protected $templateName; |
|
50 | + |
|
51 | + /** |
|
52 | + * parameters |
|
53 | + * @var array |
|
54 | + */ |
|
55 | + protected $params; |
|
56 | + |
|
57 | + /** |
|
58 | + * rendering type (admin, user, blank) |
|
59 | + * @var string |
|
60 | + */ |
|
61 | + protected $renderAs; |
|
62 | + |
|
63 | + /** |
|
64 | + * app name |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + protected $appName; |
|
68 | + |
|
69 | + /** |
|
70 | + * constructor of TemplateResponse |
|
71 | + * @param string $appName the name of the app to load the template from |
|
72 | + * @param string $templateName the name of the template |
|
73 | + * @param array $params an array of parameters which should be passed to the |
|
74 | + * template |
|
75 | + * @param string $renderAs how the page should be rendered, defaults to user |
|
76 | + * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0 |
|
77 | + */ |
|
78 | + public function __construct($appName, $templateName, array $params=[], |
|
79 | + $renderAs='user') { |
|
80 | + parent::__construct(); |
|
81 | + |
|
82 | + $this->templateName = $templateName; |
|
83 | + $this->appName = $appName; |
|
84 | + $this->params = $params; |
|
85 | + $this->renderAs = $renderAs; |
|
86 | + |
|
87 | + $this->setContentSecurityPolicy(new ContentSecurityPolicy()); |
|
88 | + $this->setFeaturePolicy(new FeaturePolicy()); |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Sets template parameters |
|
94 | + * @param array $params an array with key => value structure which sets template |
|
95 | + * variables |
|
96 | + * @return TemplateResponse Reference to this object |
|
97 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
98 | + */ |
|
99 | + public function setParams(array $params) { |
|
100 | + $this->params = $params; |
|
101 | + |
|
102 | + return $this; |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + /** |
|
107 | + * Used for accessing the set parameters |
|
108 | + * @return array the params |
|
109 | + * @since 6.0.0 |
|
110 | + */ |
|
111 | + public function getParams() { |
|
112 | + return $this->params; |
|
113 | + } |
|
114 | + |
|
115 | + |
|
116 | + /** |
|
117 | + * Used for accessing the name of the set template |
|
118 | + * @return string the name of the used template |
|
119 | + * @since 6.0.0 |
|
120 | + */ |
|
121 | + public function getTemplateName() { |
|
122 | + return $this->templateName; |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * Sets the template page |
|
128 | + * @param string $renderAs admin, user or blank. Admin also prints the admin |
|
129 | + * settings header and footer, user renders the normal |
|
130 | + * normal page including footer and header and blank |
|
131 | + * just renders the plain template |
|
132 | + * @return TemplateResponse Reference to this object |
|
133 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
134 | + */ |
|
135 | + public function renderAs($renderAs) { |
|
136 | + $this->renderAs = $renderAs; |
|
137 | + |
|
138 | + return $this; |
|
139 | + } |
|
140 | + |
|
141 | + |
|
142 | + /** |
|
143 | + * Returns the set renderAs |
|
144 | + * @return string the renderAs value |
|
145 | + * @since 6.0.0 |
|
146 | + */ |
|
147 | + public function getRenderAs() { |
|
148 | + return $this->renderAs; |
|
149 | + } |
|
150 | + |
|
151 | + |
|
152 | + /** |
|
153 | + * Returns the rendered html |
|
154 | + * @return string the rendered html |
|
155 | + * @since 6.0.0 |
|
156 | + */ |
|
157 | + public function render() { |
|
158 | + // \OCP\Template needs an empty string instead of 'blank' for an unwrapped response |
|
159 | + $renderAs = $this->renderAs === 'blank' ? '' : $this->renderAs; |
|
160 | + |
|
161 | + $template = new \OCP\Template($this->appName, $this->templateName, $renderAs); |
|
162 | + |
|
163 | + foreach($this->params as $key => $value){ |
|
164 | + $template->assign($key, $value); |
|
165 | + } |
|
166 | + |
|
167 | + return $template->fetchPage($this->params); |
|
168 | + } |
|
169 | 169 | |
170 | 170 | } |
@@ -39,8 +39,8 @@ discard block |
||
39 | 39 | */ |
40 | 40 | class TemplateResponse extends Response { |
41 | 41 | |
42 | - const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts'; |
|
43 | - const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn'; |
|
42 | + const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class.'::loadAdditionalScripts'; |
|
43 | + const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class.'::loadAdditionalScriptsLoggedIn'; |
|
44 | 44 | |
45 | 45 | /** |
46 | 46 | * name of the template |
@@ -75,8 +75,8 @@ discard block |
||
75 | 75 | * @param string $renderAs how the page should be rendered, defaults to user |
76 | 76 | * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0 |
77 | 77 | */ |
78 | - public function __construct($appName, $templateName, array $params=[], |
|
79 | - $renderAs='user') { |
|
78 | + public function __construct($appName, $templateName, array $params = [], |
|
79 | + $renderAs = 'user') { |
|
80 | 80 | parent::__construct(); |
81 | 81 | |
82 | 82 | $this->templateName = $templateName; |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | |
161 | 161 | $template = new \OCP\Template($this->appName, $this->templateName, $renderAs); |
162 | 162 | |
163 | - foreach($this->params as $key => $value){ |
|
163 | + foreach ($this->params as $key => $value) { |
|
164 | 164 | $template->assign($key, $value); |
165 | 165 | } |
166 | 166 |
@@ -36,125 +36,125 @@ |
||
36 | 36 | */ |
37 | 37 | class PublicTemplateResponse extends TemplateResponse { |
38 | 38 | |
39 | - private $headerTitle = ''; |
|
40 | - private $headerDetails = ''; |
|
41 | - private $headerActions = []; |
|
42 | - private $footerVisible = true; |
|
43 | - |
|
44 | - /** |
|
45 | - * PublicTemplateResponse constructor. |
|
46 | - * |
|
47 | - * @param string $appName |
|
48 | - * @param string $templateName |
|
49 | - * @param array $params |
|
50 | - * @since 14.0.0 |
|
51 | - */ |
|
52 | - public function __construct(string $appName, string $templateName, array $params = []) { |
|
53 | - parent::__construct($appName, $templateName, $params, 'public'); |
|
54 | - \OC_Util::addScript('core', 'public/publicpage'); |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param string $title |
|
59 | - * @since 14.0.0 |
|
60 | - */ |
|
61 | - public function setHeaderTitle(string $title) { |
|
62 | - $this->headerTitle = $title; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @return string |
|
67 | - * @since 14.0.0 |
|
68 | - */ |
|
69 | - public function getHeaderTitle(): string { |
|
70 | - return $this->headerTitle; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @param string $details |
|
75 | - * @since 14.0.0 |
|
76 | - */ |
|
77 | - public function setHeaderDetails(string $details) { |
|
78 | - $this->headerDetails = $details; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @return string |
|
83 | - * @since 14.0.0 |
|
84 | - */ |
|
85 | - public function getHeaderDetails(): string { |
|
86 | - return $this->headerDetails; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param array $actions |
|
91 | - * @since 14.0.0 |
|
92 | - * @throws InvalidArgumentException |
|
93 | - */ |
|
94 | - public function setHeaderActions(array $actions) { |
|
95 | - foreach ($actions as $action) { |
|
96 | - if ($actions instanceof IMenuAction) { |
|
97 | - throw new InvalidArgumentException('Actions must be of type IMenuAction'); |
|
98 | - } |
|
99 | - $this->headerActions[] = $action; |
|
100 | - } |
|
101 | - usort($this->headerActions, function (IMenuAction $a, IMenuAction $b) { |
|
102 | - return $a->getPriority() > $b->getPriority(); |
|
103 | - }); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @return IMenuAction |
|
108 | - * @since 14.0.0 |
|
109 | - * @throws \Exception |
|
110 | - */ |
|
111 | - public function getPrimaryAction(): IMenuAction { |
|
112 | - if ($this->getActionCount() > 0) { |
|
113 | - return $this->headerActions[0]; |
|
114 | - } |
|
115 | - throw new \Exception('No header actions have been set'); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @return int |
|
120 | - * @since 14.0.0 |
|
121 | - */ |
|
122 | - public function getActionCount(): int { |
|
123 | - return count($this->headerActions); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @return IMenuAction[] |
|
128 | - * @since 14.0.0 |
|
129 | - */ |
|
130 | - public function getOtherActions(): array { |
|
131 | - return array_slice($this->headerActions, 1); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @since 14.0.0 |
|
136 | - */ |
|
137 | - public function setFooterVisible(bool $visible = false) { |
|
138 | - $this->footerVisible = $visible; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @since 14.0.0 |
|
143 | - */ |
|
144 | - public function getFooterVisible(): bool { |
|
145 | - return $this->footerVisible; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @return string |
|
150 | - * @since 14.0.0 |
|
151 | - */ |
|
152 | - public function render(): string { |
|
153 | - $params = array_merge($this->getParams(), [ |
|
154 | - 'template' => $this, |
|
155 | - ]); |
|
156 | - $this->setParams($params); |
|
157 | - return parent::render(); |
|
158 | - } |
|
39 | + private $headerTitle = ''; |
|
40 | + private $headerDetails = ''; |
|
41 | + private $headerActions = []; |
|
42 | + private $footerVisible = true; |
|
43 | + |
|
44 | + /** |
|
45 | + * PublicTemplateResponse constructor. |
|
46 | + * |
|
47 | + * @param string $appName |
|
48 | + * @param string $templateName |
|
49 | + * @param array $params |
|
50 | + * @since 14.0.0 |
|
51 | + */ |
|
52 | + public function __construct(string $appName, string $templateName, array $params = []) { |
|
53 | + parent::__construct($appName, $templateName, $params, 'public'); |
|
54 | + \OC_Util::addScript('core', 'public/publicpage'); |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param string $title |
|
59 | + * @since 14.0.0 |
|
60 | + */ |
|
61 | + public function setHeaderTitle(string $title) { |
|
62 | + $this->headerTitle = $title; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @return string |
|
67 | + * @since 14.0.0 |
|
68 | + */ |
|
69 | + public function getHeaderTitle(): string { |
|
70 | + return $this->headerTitle; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @param string $details |
|
75 | + * @since 14.0.0 |
|
76 | + */ |
|
77 | + public function setHeaderDetails(string $details) { |
|
78 | + $this->headerDetails = $details; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @return string |
|
83 | + * @since 14.0.0 |
|
84 | + */ |
|
85 | + public function getHeaderDetails(): string { |
|
86 | + return $this->headerDetails; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param array $actions |
|
91 | + * @since 14.0.0 |
|
92 | + * @throws InvalidArgumentException |
|
93 | + */ |
|
94 | + public function setHeaderActions(array $actions) { |
|
95 | + foreach ($actions as $action) { |
|
96 | + if ($actions instanceof IMenuAction) { |
|
97 | + throw new InvalidArgumentException('Actions must be of type IMenuAction'); |
|
98 | + } |
|
99 | + $this->headerActions[] = $action; |
|
100 | + } |
|
101 | + usort($this->headerActions, function (IMenuAction $a, IMenuAction $b) { |
|
102 | + return $a->getPriority() > $b->getPriority(); |
|
103 | + }); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @return IMenuAction |
|
108 | + * @since 14.0.0 |
|
109 | + * @throws \Exception |
|
110 | + */ |
|
111 | + public function getPrimaryAction(): IMenuAction { |
|
112 | + if ($this->getActionCount() > 0) { |
|
113 | + return $this->headerActions[0]; |
|
114 | + } |
|
115 | + throw new \Exception('No header actions have been set'); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @return int |
|
120 | + * @since 14.0.0 |
|
121 | + */ |
|
122 | + public function getActionCount(): int { |
|
123 | + return count($this->headerActions); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @return IMenuAction[] |
|
128 | + * @since 14.0.0 |
|
129 | + */ |
|
130 | + public function getOtherActions(): array { |
|
131 | + return array_slice($this->headerActions, 1); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @since 14.0.0 |
|
136 | + */ |
|
137 | + public function setFooterVisible(bool $visible = false) { |
|
138 | + $this->footerVisible = $visible; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @since 14.0.0 |
|
143 | + */ |
|
144 | + public function getFooterVisible(): bool { |
|
145 | + return $this->footerVisible; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @return string |
|
150 | + * @since 14.0.0 |
|
151 | + */ |
|
152 | + public function render(): string { |
|
153 | + $params = array_merge($this->getParams(), [ |
|
154 | + 'template' => $this, |
|
155 | + ]); |
|
156 | + $this->setParams($params); |
|
157 | + return parent::render(); |
|
158 | + } |
|
159 | 159 | |
160 | 160 | } |
@@ -98,7 +98,7 @@ |
||
98 | 98 | } |
99 | 99 | $this->headerActions[] = $action; |
100 | 100 | } |
101 | - usort($this->headerActions, function (IMenuAction $a, IMenuAction $b) { |
|
101 | + usort($this->headerActions, function(IMenuAction $a, IMenuAction $b) { |
|
102 | 102 | return $a->getPriority() > $b->getPriority(); |
103 | 103 | }); |
104 | 104 | } |
@@ -42,73 +42,73 @@ |
||
42 | 42 | */ |
43 | 43 | abstract class OCSController extends ApiController { |
44 | 44 | |
45 | - /** @var int */ |
|
46 | - private $ocsVersion; |
|
45 | + /** @var int */ |
|
46 | + private $ocsVersion; |
|
47 | 47 | |
48 | - /** |
|
49 | - * constructor of the controller |
|
50 | - * @param string $appName the name of the app |
|
51 | - * @param IRequest $request an instance of the request |
|
52 | - * @param string $corsMethods comma separated string of HTTP verbs which |
|
53 | - * should be allowed for websites or webapps when calling your API, defaults to |
|
54 | - * 'PUT, POST, GET, DELETE, PATCH' |
|
55 | - * @param string $corsAllowedHeaders comma separated string of HTTP headers |
|
56 | - * which should be allowed for websites or webapps when calling your API, |
|
57 | - * defaults to 'Authorization, Content-Type, Accept' |
|
58 | - * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS |
|
59 | - * request should be cached, defaults to 1728000 seconds |
|
60 | - * @since 8.1.0 |
|
61 | - */ |
|
62 | - public function __construct($appName, |
|
63 | - IRequest $request, |
|
64 | - $corsMethods='PUT, POST, GET, DELETE, PATCH', |
|
65 | - $corsAllowedHeaders='Authorization, Content-Type, Accept', |
|
66 | - $corsMaxAge=1728000) { |
|
67 | - parent::__construct($appName, $request, $corsMethods, |
|
68 | - $corsAllowedHeaders, $corsMaxAge); |
|
69 | - $this->registerResponder('json', function ($data) { |
|
70 | - return $this->buildOCSResponse('json', $data); |
|
71 | - }); |
|
72 | - $this->registerResponder('xml', function ($data) { |
|
73 | - return $this->buildOCSResponse('xml', $data); |
|
74 | - }); |
|
75 | - } |
|
48 | + /** |
|
49 | + * constructor of the controller |
|
50 | + * @param string $appName the name of the app |
|
51 | + * @param IRequest $request an instance of the request |
|
52 | + * @param string $corsMethods comma separated string of HTTP verbs which |
|
53 | + * should be allowed for websites or webapps when calling your API, defaults to |
|
54 | + * 'PUT, POST, GET, DELETE, PATCH' |
|
55 | + * @param string $corsAllowedHeaders comma separated string of HTTP headers |
|
56 | + * which should be allowed for websites or webapps when calling your API, |
|
57 | + * defaults to 'Authorization, Content-Type, Accept' |
|
58 | + * @param int $corsMaxAge number in seconds how long a preflighted OPTIONS |
|
59 | + * request should be cached, defaults to 1728000 seconds |
|
60 | + * @since 8.1.0 |
|
61 | + */ |
|
62 | + public function __construct($appName, |
|
63 | + IRequest $request, |
|
64 | + $corsMethods='PUT, POST, GET, DELETE, PATCH', |
|
65 | + $corsAllowedHeaders='Authorization, Content-Type, Accept', |
|
66 | + $corsMaxAge=1728000) { |
|
67 | + parent::__construct($appName, $request, $corsMethods, |
|
68 | + $corsAllowedHeaders, $corsMaxAge); |
|
69 | + $this->registerResponder('json', function ($data) { |
|
70 | + return $this->buildOCSResponse('json', $data); |
|
71 | + }); |
|
72 | + $this->registerResponder('xml', function ($data) { |
|
73 | + return $this->buildOCSResponse('xml', $data); |
|
74 | + }); |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * @param int $version |
|
79 | - * @since 11.0.0 |
|
80 | - * @internal |
|
81 | - */ |
|
82 | - public function setOCSVersion($version) { |
|
83 | - $this->ocsVersion = $version; |
|
84 | - } |
|
77 | + /** |
|
78 | + * @param int $version |
|
79 | + * @since 11.0.0 |
|
80 | + * @internal |
|
81 | + */ |
|
82 | + public function setOCSVersion($version) { |
|
83 | + $this->ocsVersion = $version; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Since the OCS endpoints default to XML we need to find out the format |
|
88 | - * again |
|
89 | - * @param mixed $response the value that was returned from a controller and |
|
90 | - * is not a Response instance |
|
91 | - * @param string $format the format for which a formatter has been registered |
|
92 | - * @throws \DomainException if format does not match a registered formatter |
|
93 | - * @return Response |
|
94 | - * @since 9.1.0 |
|
95 | - */ |
|
96 | - public function buildResponse($response, $format = 'xml') { |
|
97 | - return parent::buildResponse($response, $format); |
|
98 | - } |
|
86 | + /** |
|
87 | + * Since the OCS endpoints default to XML we need to find out the format |
|
88 | + * again |
|
89 | + * @param mixed $response the value that was returned from a controller and |
|
90 | + * is not a Response instance |
|
91 | + * @param string $format the format for which a formatter has been registered |
|
92 | + * @throws \DomainException if format does not match a registered formatter |
|
93 | + * @return Response |
|
94 | + * @since 9.1.0 |
|
95 | + */ |
|
96 | + public function buildResponse($response, $format = 'xml') { |
|
97 | + return parent::buildResponse($response, $format); |
|
98 | + } |
|
99 | 99 | |
100 | - /** |
|
101 | - * Unwrap data and build ocs response |
|
102 | - * @param string $format json or xml |
|
103 | - * @param DataResponse $data the data which should be transformed |
|
104 | - * @since 8.1.0 |
|
105 | - * @return \OC\AppFramework\OCS\BaseResponse |
|
106 | - */ |
|
107 | - private function buildOCSResponse($format, DataResponse $data) { |
|
108 | - if ($this->ocsVersion === 1) { |
|
109 | - return new \OC\AppFramework\OCS\V1Response($data, $format); |
|
110 | - } |
|
111 | - return new \OC\AppFramework\OCS\V2Response($data, $format); |
|
112 | - } |
|
100 | + /** |
|
101 | + * Unwrap data and build ocs response |
|
102 | + * @param string $format json or xml |
|
103 | + * @param DataResponse $data the data which should be transformed |
|
104 | + * @since 8.1.0 |
|
105 | + * @return \OC\AppFramework\OCS\BaseResponse |
|
106 | + */ |
|
107 | + private function buildOCSResponse($format, DataResponse $data) { |
|
108 | + if ($this->ocsVersion === 1) { |
|
109 | + return new \OC\AppFramework\OCS\V1Response($data, $format); |
|
110 | + } |
|
111 | + return new \OC\AppFramework\OCS\V2Response($data, $format); |
|
112 | + } |
|
113 | 113 | |
114 | 114 | } |
@@ -61,15 +61,15 @@ |
||
61 | 61 | */ |
62 | 62 | public function __construct($appName, |
63 | 63 | IRequest $request, |
64 | - $corsMethods='PUT, POST, GET, DELETE, PATCH', |
|
65 | - $corsAllowedHeaders='Authorization, Content-Type, Accept', |
|
66 | - $corsMaxAge=1728000) { |
|
64 | + $corsMethods = 'PUT, POST, GET, DELETE, PATCH', |
|
65 | + $corsAllowedHeaders = 'Authorization, Content-Type, Accept', |
|
66 | + $corsMaxAge = 1728000) { |
|
67 | 67 | parent::__construct($appName, $request, $corsMethods, |
68 | 68 | $corsAllowedHeaders, $corsMaxAge); |
69 | - $this->registerResponder('json', function ($data) { |
|
69 | + $this->registerResponder('json', function($data) { |
|
70 | 70 | return $this->buildOCSResponse('json', $data); |
71 | 71 | }); |
72 | - $this->registerResponder('xml', function ($data) { |
|
72 | + $this->registerResponder('xml', function($data) { |
|
73 | 73 | return $this->buildOCSResponse('xml', $data); |
74 | 74 | }); |
75 | 75 | } |
@@ -34,13 +34,13 @@ |
||
34 | 34 | */ |
35 | 35 | class DoesNotExistException extends \Exception implements IMapperException { |
36 | 36 | |
37 | - /** |
|
38 | - * Constructor |
|
39 | - * @param string $msg the error message |
|
40 | - * @since 7.0.0 |
|
41 | - */ |
|
42 | - public function __construct($msg) { |
|
43 | - parent::__construct($msg); |
|
44 | - } |
|
37 | + /** |
|
38 | + * Constructor |
|
39 | + * @param string $msg the error message |
|
40 | + * @since 7.0.0 |
|
41 | + */ |
|
42 | + public function __construct($msg) { |
|
43 | + parent::__construct($msg); |
|
44 | + } |
|
45 | 45 | |
46 | 46 | } |
@@ -35,236 +35,236 @@ |
||
35 | 35 | */ |
36 | 36 | abstract class Entity { |
37 | 37 | |
38 | - public $id; |
|
39 | - |
|
40 | - private $_updatedFields = []; |
|
41 | - private $_fieldTypes = ['id' => 'integer']; |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * Simple alternative constructor for building entities from a request |
|
46 | - * @param array $params the array which was obtained via $this->params('key') |
|
47 | - * in the controller |
|
48 | - * @return Entity |
|
49 | - * @since 7.0.0 |
|
50 | - */ |
|
51 | - public static function fromParams(array $params) { |
|
52 | - $instance = new static(); |
|
53 | - |
|
54 | - foreach($params as $key => $value) { |
|
55 | - $method = 'set' . ucfirst($key); |
|
56 | - $instance->$method($value); |
|
57 | - } |
|
58 | - |
|
59 | - return $instance; |
|
60 | - } |
|
61 | - |
|
62 | - |
|
63 | - /** |
|
64 | - * Maps the keys of the row array to the attributes |
|
65 | - * @param array $row the row to map onto the entity |
|
66 | - * @since 7.0.0 |
|
67 | - */ |
|
68 | - public static function fromRow(array $row) { |
|
69 | - $instance = new static(); |
|
70 | - |
|
71 | - foreach($row as $key => $value){ |
|
72 | - $prop = ucfirst($instance->columnToProperty($key)); |
|
73 | - $setter = 'set' . $prop; |
|
74 | - $instance->$setter($value); |
|
75 | - } |
|
76 | - |
|
77 | - $instance->resetUpdatedFields(); |
|
78 | - |
|
79 | - return $instance; |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * @return array with attribute and type |
|
85 | - * @since 7.0.0 |
|
86 | - */ |
|
87 | - public function getFieldTypes() { |
|
88 | - return $this->_fieldTypes; |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Marks the entity as clean needed for setting the id after the insertion |
|
94 | - * @since 7.0.0 |
|
95 | - */ |
|
96 | - public function resetUpdatedFields() { |
|
97 | - $this->_updatedFields = []; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Generic setter for properties |
|
102 | - * @since 7.0.0 |
|
103 | - */ |
|
104 | - protected function setter($name, $args) { |
|
105 | - // setters should only work for existing attributes |
|
106 | - if(property_exists($this, $name)){ |
|
107 | - if($this->$name === $args[0]) { |
|
108 | - return; |
|
109 | - } |
|
110 | - $this->markFieldUpdated($name); |
|
111 | - |
|
112 | - // if type definition exists, cast to correct type |
|
113 | - if($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) { |
|
114 | - settype($args[0], $this->_fieldTypes[$name]); |
|
115 | - } |
|
116 | - $this->$name = $args[0]; |
|
117 | - |
|
118 | - } else { |
|
119 | - throw new \BadFunctionCallException($name . |
|
120 | - ' is not a valid attribute'); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Generic getter for properties |
|
126 | - * @since 7.0.0 |
|
127 | - */ |
|
128 | - protected function getter($name) { |
|
129 | - // getters should only work for existing attributes |
|
130 | - if(property_exists($this, $name)){ |
|
131 | - return $this->$name; |
|
132 | - } else { |
|
133 | - throw new \BadFunctionCallException($name . |
|
134 | - ' is not a valid attribute'); |
|
135 | - } |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * Each time a setter is called, push the part after set |
|
141 | - * into an array: for instance setId will save Id in the |
|
142 | - * updated fields array so it can be easily used to create the |
|
143 | - * getter method |
|
144 | - * @since 7.0.0 |
|
145 | - */ |
|
146 | - public function __call($methodName, $args) { |
|
147 | - if (strpos($methodName, 'set') === 0) { |
|
148 | - $this->setter(lcfirst(substr($methodName, 3)), $args); |
|
149 | - } elseif (strpos($methodName, 'get') === 0) { |
|
150 | - return $this->getter(lcfirst(substr($methodName, 3))); |
|
151 | - } elseif ($this->isGetterForBoolProperty($methodName)) { |
|
152 | - return $this->getter(lcfirst(substr($methodName, 2))); |
|
153 | - } else { |
|
154 | - throw new \BadFunctionCallException($methodName . |
|
155 | - ' does not exist'); |
|
156 | - } |
|
157 | - |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @param string $methodName |
|
162 | - * @return bool |
|
163 | - * @since 18.0.0 |
|
164 | - */ |
|
165 | - protected function isGetterForBoolProperty(string $methodName): bool { |
|
166 | - if (strpos($methodName, 'is') === 0) { |
|
167 | - $fieldName = lcfirst(substr($methodName, 2)); |
|
168 | - return isset($this->_fieldTypes[$fieldName]) && strpos($this->_fieldTypes[$fieldName], 'bool') === 0; |
|
169 | - } |
|
170 | - return false; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Mark am attribute as updated |
|
175 | - * @param string $attribute the name of the attribute |
|
176 | - * @since 7.0.0 |
|
177 | - */ |
|
178 | - protected function markFieldUpdated($attribute) { |
|
179 | - $this->_updatedFields[$attribute] = true; |
|
180 | - } |
|
181 | - |
|
182 | - |
|
183 | - /** |
|
184 | - * Transform a database columnname to a property |
|
185 | - * @param string $columnName the name of the column |
|
186 | - * @return string the property name |
|
187 | - * @since 7.0.0 |
|
188 | - */ |
|
189 | - public function columnToProperty($columnName) { |
|
190 | - $parts = explode('_', $columnName); |
|
191 | - $property = null; |
|
192 | - |
|
193 | - foreach($parts as $part){ |
|
194 | - if($property === null){ |
|
195 | - $property = $part; |
|
196 | - } else { |
|
197 | - $property .= ucfirst($part); |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - return $property; |
|
202 | - } |
|
203 | - |
|
204 | - |
|
205 | - /** |
|
206 | - * Transform a property to a database column name |
|
207 | - * @param string $property the name of the property |
|
208 | - * @return string the column name |
|
209 | - * @since 7.0.0 |
|
210 | - */ |
|
211 | - public function propertyToColumn($property) { |
|
212 | - $parts = preg_split('/(?=[A-Z])/', $property); |
|
213 | - $column = null; |
|
214 | - |
|
215 | - foreach($parts as $part){ |
|
216 | - if($column === null){ |
|
217 | - $column = $part; |
|
218 | - } else { |
|
219 | - $column .= '_' . lcfirst($part); |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - return $column; |
|
224 | - } |
|
225 | - |
|
226 | - |
|
227 | - /** |
|
228 | - * @return array array of updated fields for update query |
|
229 | - * @since 7.0.0 |
|
230 | - */ |
|
231 | - public function getUpdatedFields() { |
|
232 | - return $this->_updatedFields; |
|
233 | - } |
|
234 | - |
|
235 | - |
|
236 | - /** |
|
237 | - * Adds type information for a field so that its automatically casted to |
|
238 | - * that value once its being returned from the database |
|
239 | - * @param string $fieldName the name of the attribute |
|
240 | - * @param string $type the type which will be used to call settype() |
|
241 | - * @since 7.0.0 |
|
242 | - */ |
|
243 | - protected function addType($fieldName, $type) { |
|
244 | - $this->_fieldTypes[$fieldName] = $type; |
|
245 | - } |
|
246 | - |
|
247 | - |
|
248 | - /** |
|
249 | - * Slugify the value of a given attribute |
|
250 | - * Warning: This doesn't result in a unique value |
|
251 | - * @param string $attributeName the name of the attribute, which value should be slugified |
|
252 | - * @return string slugified value |
|
253 | - * @since 7.0.0 |
|
254 | - */ |
|
255 | - public function slugify($attributeName) { |
|
256 | - // toSlug should only work for existing attributes |
|
257 | - if(property_exists($this, $attributeName)){ |
|
258 | - $value = $this->$attributeName; |
|
259 | - // replace everything except alphanumeric with a single '-' |
|
260 | - $value = preg_replace('/[^A-Za-z0-9]+/', '-', $value); |
|
261 | - $value = strtolower($value); |
|
262 | - // trim '-' |
|
263 | - return trim($value, '-'); |
|
264 | - } else { |
|
265 | - throw new \BadFunctionCallException($attributeName . |
|
266 | - ' is not a valid attribute'); |
|
267 | - } |
|
268 | - } |
|
38 | + public $id; |
|
39 | + |
|
40 | + private $_updatedFields = []; |
|
41 | + private $_fieldTypes = ['id' => 'integer']; |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * Simple alternative constructor for building entities from a request |
|
46 | + * @param array $params the array which was obtained via $this->params('key') |
|
47 | + * in the controller |
|
48 | + * @return Entity |
|
49 | + * @since 7.0.0 |
|
50 | + */ |
|
51 | + public static function fromParams(array $params) { |
|
52 | + $instance = new static(); |
|
53 | + |
|
54 | + foreach($params as $key => $value) { |
|
55 | + $method = 'set' . ucfirst($key); |
|
56 | + $instance->$method($value); |
|
57 | + } |
|
58 | + |
|
59 | + return $instance; |
|
60 | + } |
|
61 | + |
|
62 | + |
|
63 | + /** |
|
64 | + * Maps the keys of the row array to the attributes |
|
65 | + * @param array $row the row to map onto the entity |
|
66 | + * @since 7.0.0 |
|
67 | + */ |
|
68 | + public static function fromRow(array $row) { |
|
69 | + $instance = new static(); |
|
70 | + |
|
71 | + foreach($row as $key => $value){ |
|
72 | + $prop = ucfirst($instance->columnToProperty($key)); |
|
73 | + $setter = 'set' . $prop; |
|
74 | + $instance->$setter($value); |
|
75 | + } |
|
76 | + |
|
77 | + $instance->resetUpdatedFields(); |
|
78 | + |
|
79 | + return $instance; |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * @return array with attribute and type |
|
85 | + * @since 7.0.0 |
|
86 | + */ |
|
87 | + public function getFieldTypes() { |
|
88 | + return $this->_fieldTypes; |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Marks the entity as clean needed for setting the id after the insertion |
|
94 | + * @since 7.0.0 |
|
95 | + */ |
|
96 | + public function resetUpdatedFields() { |
|
97 | + $this->_updatedFields = []; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Generic setter for properties |
|
102 | + * @since 7.0.0 |
|
103 | + */ |
|
104 | + protected function setter($name, $args) { |
|
105 | + // setters should only work for existing attributes |
|
106 | + if(property_exists($this, $name)){ |
|
107 | + if($this->$name === $args[0]) { |
|
108 | + return; |
|
109 | + } |
|
110 | + $this->markFieldUpdated($name); |
|
111 | + |
|
112 | + // if type definition exists, cast to correct type |
|
113 | + if($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) { |
|
114 | + settype($args[0], $this->_fieldTypes[$name]); |
|
115 | + } |
|
116 | + $this->$name = $args[0]; |
|
117 | + |
|
118 | + } else { |
|
119 | + throw new \BadFunctionCallException($name . |
|
120 | + ' is not a valid attribute'); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Generic getter for properties |
|
126 | + * @since 7.0.0 |
|
127 | + */ |
|
128 | + protected function getter($name) { |
|
129 | + // getters should only work for existing attributes |
|
130 | + if(property_exists($this, $name)){ |
|
131 | + return $this->$name; |
|
132 | + } else { |
|
133 | + throw new \BadFunctionCallException($name . |
|
134 | + ' is not a valid attribute'); |
|
135 | + } |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * Each time a setter is called, push the part after set |
|
141 | + * into an array: for instance setId will save Id in the |
|
142 | + * updated fields array so it can be easily used to create the |
|
143 | + * getter method |
|
144 | + * @since 7.0.0 |
|
145 | + */ |
|
146 | + public function __call($methodName, $args) { |
|
147 | + if (strpos($methodName, 'set') === 0) { |
|
148 | + $this->setter(lcfirst(substr($methodName, 3)), $args); |
|
149 | + } elseif (strpos($methodName, 'get') === 0) { |
|
150 | + return $this->getter(lcfirst(substr($methodName, 3))); |
|
151 | + } elseif ($this->isGetterForBoolProperty($methodName)) { |
|
152 | + return $this->getter(lcfirst(substr($methodName, 2))); |
|
153 | + } else { |
|
154 | + throw new \BadFunctionCallException($methodName . |
|
155 | + ' does not exist'); |
|
156 | + } |
|
157 | + |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @param string $methodName |
|
162 | + * @return bool |
|
163 | + * @since 18.0.0 |
|
164 | + */ |
|
165 | + protected function isGetterForBoolProperty(string $methodName): bool { |
|
166 | + if (strpos($methodName, 'is') === 0) { |
|
167 | + $fieldName = lcfirst(substr($methodName, 2)); |
|
168 | + return isset($this->_fieldTypes[$fieldName]) && strpos($this->_fieldTypes[$fieldName], 'bool') === 0; |
|
169 | + } |
|
170 | + return false; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Mark am attribute as updated |
|
175 | + * @param string $attribute the name of the attribute |
|
176 | + * @since 7.0.0 |
|
177 | + */ |
|
178 | + protected function markFieldUpdated($attribute) { |
|
179 | + $this->_updatedFields[$attribute] = true; |
|
180 | + } |
|
181 | + |
|
182 | + |
|
183 | + /** |
|
184 | + * Transform a database columnname to a property |
|
185 | + * @param string $columnName the name of the column |
|
186 | + * @return string the property name |
|
187 | + * @since 7.0.0 |
|
188 | + */ |
|
189 | + public function columnToProperty($columnName) { |
|
190 | + $parts = explode('_', $columnName); |
|
191 | + $property = null; |
|
192 | + |
|
193 | + foreach($parts as $part){ |
|
194 | + if($property === null){ |
|
195 | + $property = $part; |
|
196 | + } else { |
|
197 | + $property .= ucfirst($part); |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + return $property; |
|
202 | + } |
|
203 | + |
|
204 | + |
|
205 | + /** |
|
206 | + * Transform a property to a database column name |
|
207 | + * @param string $property the name of the property |
|
208 | + * @return string the column name |
|
209 | + * @since 7.0.0 |
|
210 | + */ |
|
211 | + public function propertyToColumn($property) { |
|
212 | + $parts = preg_split('/(?=[A-Z])/', $property); |
|
213 | + $column = null; |
|
214 | + |
|
215 | + foreach($parts as $part){ |
|
216 | + if($column === null){ |
|
217 | + $column = $part; |
|
218 | + } else { |
|
219 | + $column .= '_' . lcfirst($part); |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + return $column; |
|
224 | + } |
|
225 | + |
|
226 | + |
|
227 | + /** |
|
228 | + * @return array array of updated fields for update query |
|
229 | + * @since 7.0.0 |
|
230 | + */ |
|
231 | + public function getUpdatedFields() { |
|
232 | + return $this->_updatedFields; |
|
233 | + } |
|
234 | + |
|
235 | + |
|
236 | + /** |
|
237 | + * Adds type information for a field so that its automatically casted to |
|
238 | + * that value once its being returned from the database |
|
239 | + * @param string $fieldName the name of the attribute |
|
240 | + * @param string $type the type which will be used to call settype() |
|
241 | + * @since 7.0.0 |
|
242 | + */ |
|
243 | + protected function addType($fieldName, $type) { |
|
244 | + $this->_fieldTypes[$fieldName] = $type; |
|
245 | + } |
|
246 | + |
|
247 | + |
|
248 | + /** |
|
249 | + * Slugify the value of a given attribute |
|
250 | + * Warning: This doesn't result in a unique value |
|
251 | + * @param string $attributeName the name of the attribute, which value should be slugified |
|
252 | + * @return string slugified value |
|
253 | + * @since 7.0.0 |
|
254 | + */ |
|
255 | + public function slugify($attributeName) { |
|
256 | + // toSlug should only work for existing attributes |
|
257 | + if(property_exists($this, $attributeName)){ |
|
258 | + $value = $this->$attributeName; |
|
259 | + // replace everything except alphanumeric with a single '-' |
|
260 | + $value = preg_replace('/[^A-Za-z0-9]+/', '-', $value); |
|
261 | + $value = strtolower($value); |
|
262 | + // trim '-' |
|
263 | + return trim($value, '-'); |
|
264 | + } else { |
|
265 | + throw new \BadFunctionCallException($attributeName . |
|
266 | + ' is not a valid attribute'); |
|
267 | + } |
|
268 | + } |
|
269 | 269 | |
270 | 270 | } |
@@ -51,8 +51,8 @@ discard block |
||
51 | 51 | public static function fromParams(array $params) { |
52 | 52 | $instance = new static(); |
53 | 53 | |
54 | - foreach($params as $key => $value) { |
|
55 | - $method = 'set' . ucfirst($key); |
|
54 | + foreach ($params as $key => $value) { |
|
55 | + $method = 'set'.ucfirst($key); |
|
56 | 56 | $instance->$method($value); |
57 | 57 | } |
58 | 58 | |
@@ -68,9 +68,9 @@ discard block |
||
68 | 68 | public static function fromRow(array $row) { |
69 | 69 | $instance = new static(); |
70 | 70 | |
71 | - foreach($row as $key => $value){ |
|
71 | + foreach ($row as $key => $value) { |
|
72 | 72 | $prop = ucfirst($instance->columnToProperty($key)); |
73 | - $setter = 'set' . $prop; |
|
73 | + $setter = 'set'.$prop; |
|
74 | 74 | $instance->$setter($value); |
75 | 75 | } |
76 | 76 | |
@@ -103,20 +103,20 @@ discard block |
||
103 | 103 | */ |
104 | 104 | protected function setter($name, $args) { |
105 | 105 | // setters should only work for existing attributes |
106 | - if(property_exists($this, $name)){ |
|
107 | - if($this->$name === $args[0]) { |
|
106 | + if (property_exists($this, $name)) { |
|
107 | + if ($this->$name === $args[0]) { |
|
108 | 108 | return; |
109 | 109 | } |
110 | 110 | $this->markFieldUpdated($name); |
111 | 111 | |
112 | 112 | // if type definition exists, cast to correct type |
113 | - if($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) { |
|
113 | + if ($args[0] !== null && array_key_exists($name, $this->_fieldTypes)) { |
|
114 | 114 | settype($args[0], $this->_fieldTypes[$name]); |
115 | 115 | } |
116 | 116 | $this->$name = $args[0]; |
117 | 117 | |
118 | 118 | } else { |
119 | - throw new \BadFunctionCallException($name . |
|
119 | + throw new \BadFunctionCallException($name. |
|
120 | 120 | ' is not a valid attribute'); |
121 | 121 | } |
122 | 122 | } |
@@ -127,10 +127,10 @@ discard block |
||
127 | 127 | */ |
128 | 128 | protected function getter($name) { |
129 | 129 | // getters should only work for existing attributes |
130 | - if(property_exists($this, $name)){ |
|
130 | + if (property_exists($this, $name)) { |
|
131 | 131 | return $this->$name; |
132 | 132 | } else { |
133 | - throw new \BadFunctionCallException($name . |
|
133 | + throw new \BadFunctionCallException($name. |
|
134 | 134 | ' is not a valid attribute'); |
135 | 135 | } |
136 | 136 | } |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | } elseif ($this->isGetterForBoolProperty($methodName)) { |
152 | 152 | return $this->getter(lcfirst(substr($methodName, 2))); |
153 | 153 | } else { |
154 | - throw new \BadFunctionCallException($methodName . |
|
154 | + throw new \BadFunctionCallException($methodName. |
|
155 | 155 | ' does not exist'); |
156 | 156 | } |
157 | 157 | |
@@ -190,8 +190,8 @@ discard block |
||
190 | 190 | $parts = explode('_', $columnName); |
191 | 191 | $property = null; |
192 | 192 | |
193 | - foreach($parts as $part){ |
|
194 | - if($property === null){ |
|
193 | + foreach ($parts as $part) { |
|
194 | + if ($property === null) { |
|
195 | 195 | $property = $part; |
196 | 196 | } else { |
197 | 197 | $property .= ucfirst($part); |
@@ -212,11 +212,11 @@ discard block |
||
212 | 212 | $parts = preg_split('/(?=[A-Z])/', $property); |
213 | 213 | $column = null; |
214 | 214 | |
215 | - foreach($parts as $part){ |
|
216 | - if($column === null){ |
|
215 | + foreach ($parts as $part) { |
|
216 | + if ($column === null) { |
|
217 | 217 | $column = $part; |
218 | 218 | } else { |
219 | - $column .= '_' . lcfirst($part); |
|
219 | + $column .= '_'.lcfirst($part); |
|
220 | 220 | } |
221 | 221 | } |
222 | 222 | |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | */ |
255 | 255 | public function slugify($attributeName) { |
256 | 256 | // toSlug should only work for existing attributes |
257 | - if(property_exists($this, $attributeName)){ |
|
257 | + if (property_exists($this, $attributeName)) { |
|
258 | 258 | $value = $this->$attributeName; |
259 | 259 | // replace everything except alphanumeric with a single '-' |
260 | 260 | $value = preg_replace('/[^A-Za-z0-9]+/', '-', $value); |
@@ -262,7 +262,7 @@ discard block |
||
262 | 262 | // trim '-' |
263 | 263 | return trim($value, '-'); |
264 | 264 | } else { |
265 | - throw new \BadFunctionCallException($attributeName . |
|
265 | + throw new \BadFunctionCallException($attributeName. |
|
266 | 266 | ' is not a valid attribute'); |
267 | 267 | } |
268 | 268 | } |
@@ -37,339 +37,339 @@ |
||
37 | 37 | */ |
38 | 38 | abstract class Mapper { |
39 | 39 | |
40 | - protected $tableName; |
|
41 | - protected $entityClass; |
|
42 | - protected $db; |
|
43 | - |
|
44 | - /** |
|
45 | - * @param IDBConnection $db Instance of the Db abstraction layer |
|
46 | - * @param string $tableName the name of the table. set this to allow entity |
|
47 | - * @param string $entityClass the name of the entity that the sql should be |
|
48 | - * mapped to queries without using sql |
|
49 | - * @since 7.0.0 |
|
50 | - * @deprecated 14.0.0 Move over to QBMapper |
|
51 | - */ |
|
52 | - public function __construct(IDBConnection $db, $tableName, $entityClass=null) { |
|
53 | - $this->db = $db; |
|
54 | - $this->tableName = '*PREFIX*' . $tableName; |
|
55 | - |
|
56 | - // if not given set the entity name to the class without the mapper part |
|
57 | - // cache it here for later use since reflection is slow |
|
58 | - if($entityClass === null) { |
|
59 | - $this->entityClass = str_replace('Mapper', '', get_class($this)); |
|
60 | - } else { |
|
61 | - $this->entityClass = $entityClass; |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - |
|
66 | - /** |
|
67 | - * @return string the table name |
|
68 | - * @since 7.0.0 |
|
69 | - * @deprecated 14.0.0 Move over to QBMapper |
|
70 | - */ |
|
71 | - public function getTableName() { |
|
72 | - return $this->tableName; |
|
73 | - } |
|
74 | - |
|
75 | - |
|
76 | - /** |
|
77 | - * Deletes an entity from the table |
|
78 | - * @param Entity $entity the entity that should be deleted |
|
79 | - * @return Entity the deleted entity |
|
80 | - * @since 7.0.0 - return value added in 8.1.0 |
|
81 | - * @deprecated 14.0.0 Move over to QBMapper |
|
82 | - */ |
|
83 | - public function delete(Entity $entity) { |
|
84 | - $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
85 | - $stmt = $this->execute($sql, [$entity->getId()]); |
|
86 | - $stmt->closeCursor(); |
|
87 | - return $entity; |
|
88 | - } |
|
89 | - |
|
90 | - |
|
91 | - /** |
|
92 | - * Creates a new entry in the db from an entity |
|
93 | - * @param Entity $entity the entity that should be created |
|
94 | - * @return Entity the saved entity with the set id |
|
95 | - * @since 7.0.0 |
|
96 | - * @deprecated 14.0.0 Move over to QBMapper |
|
97 | - */ |
|
98 | - public function insert(Entity $entity) { |
|
99 | - // get updated fields to save, fields have to be set using a setter to |
|
100 | - // be saved |
|
101 | - $properties = $entity->getUpdatedFields(); |
|
102 | - $values = ''; |
|
103 | - $columns = ''; |
|
104 | - $params = []; |
|
105 | - |
|
106 | - // build the fields |
|
107 | - $i = 0; |
|
108 | - foreach($properties as $property => $updated) { |
|
109 | - $column = $entity->propertyToColumn($property); |
|
110 | - $getter = 'get' . ucfirst($property); |
|
111 | - |
|
112 | - $columns .= '`' . $column . '`'; |
|
113 | - $values .= '?'; |
|
114 | - |
|
115 | - // only append colon if there are more entries |
|
116 | - if($i < count($properties)-1){ |
|
117 | - $columns .= ','; |
|
118 | - $values .= ','; |
|
119 | - } |
|
120 | - |
|
121 | - $params[] = $entity->$getter(); |
|
122 | - $i++; |
|
123 | - |
|
124 | - } |
|
125 | - |
|
126 | - $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
127 | - $columns . ') VALUES(' . $values . ')'; |
|
128 | - |
|
129 | - $stmt = $this->execute($sql, $params); |
|
130 | - |
|
131 | - $entity->setId((int) $this->db->lastInsertId($this->tableName)); |
|
132 | - |
|
133 | - $stmt->closeCursor(); |
|
134 | - |
|
135 | - return $entity; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - |
|
140 | - /** |
|
141 | - * Updates an entry in the db from an entity |
|
142 | - * @throws \InvalidArgumentException if entity has no id |
|
143 | - * @param Entity $entity the entity that should be created |
|
144 | - * @return Entity the saved entity with the set id |
|
145 | - * @since 7.0.0 - return value was added in 8.0.0 |
|
146 | - * @deprecated 14.0.0 Move over to QBMapper |
|
147 | - */ |
|
148 | - public function update(Entity $entity) { |
|
149 | - // if entity wasn't changed it makes no sense to run a db query |
|
150 | - $properties = $entity->getUpdatedFields(); |
|
151 | - if(count($properties) === 0) { |
|
152 | - return $entity; |
|
153 | - } |
|
154 | - |
|
155 | - // entity needs an id |
|
156 | - $id = $entity->getId(); |
|
157 | - if($id === null){ |
|
158 | - throw new \InvalidArgumentException( |
|
159 | - 'Entity which should be updated has no id'); |
|
160 | - } |
|
161 | - |
|
162 | - // get updated fields to save, fields have to be set using a setter to |
|
163 | - // be saved |
|
164 | - // do not update the id field |
|
165 | - unset($properties['id']); |
|
166 | - |
|
167 | - $columns = ''; |
|
168 | - $params = []; |
|
169 | - |
|
170 | - // build the fields |
|
171 | - $i = 0; |
|
172 | - foreach($properties as $property => $updated) { |
|
173 | - |
|
174 | - $column = $entity->propertyToColumn($property); |
|
175 | - $getter = 'get' . ucfirst($property); |
|
176 | - |
|
177 | - $columns .= '`' . $column . '` = ?'; |
|
178 | - |
|
179 | - // only append colon if there are more entries |
|
180 | - if($i < count($properties)-1){ |
|
181 | - $columns .= ','; |
|
182 | - } |
|
183 | - |
|
184 | - $params[] = $entity->$getter(); |
|
185 | - $i++; |
|
186 | - } |
|
187 | - |
|
188 | - $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
189 | - $columns . ' WHERE `id` = ?'; |
|
190 | - $params[] = $id; |
|
191 | - |
|
192 | - $stmt = $this->execute($sql, $params); |
|
193 | - $stmt->closeCursor(); |
|
194 | - |
|
195 | - return $entity; |
|
196 | - } |
|
197 | - |
|
198 | - /** |
|
199 | - * Checks if an array is associative |
|
200 | - * @param array $array |
|
201 | - * @return bool true if associative |
|
202 | - * @since 8.1.0 |
|
203 | - * @deprecated 14.0.0 Move over to QBMapper |
|
204 | - */ |
|
205 | - private function isAssocArray(array $array) { |
|
206 | - return array_values($array) !== $array; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Returns the correct PDO constant based on the value type |
|
211 | - * @param $value |
|
212 | - * @return int PDO constant |
|
213 | - * @since 8.1.0 |
|
214 | - * @deprecated 14.0.0 Move over to QBMapper |
|
215 | - */ |
|
216 | - private function getPDOType($value) { |
|
217 | - switch (gettype($value)) { |
|
218 | - case 'integer': |
|
219 | - return \PDO::PARAM_INT; |
|
220 | - case 'boolean': |
|
221 | - return \PDO::PARAM_BOOL; |
|
222 | - default: |
|
223 | - return \PDO::PARAM_STR; |
|
224 | - } |
|
225 | - } |
|
226 | - |
|
227 | - |
|
228 | - /** |
|
229 | - * Runs an sql query |
|
230 | - * @param string $sql the prepare string |
|
231 | - * @param array $params the params which should replace the ? in the sql query |
|
232 | - * @param int $limit the maximum number of rows |
|
233 | - * @param int $offset from which row we want to start |
|
234 | - * @return \PDOStatement the database query result |
|
235 | - * @since 7.0.0 |
|
236 | - * @deprecated 14.0.0 Move over to QBMapper |
|
237 | - */ |
|
238 | - protected function execute($sql, array $params=[], $limit=null, $offset=null) { |
|
239 | - $query = $this->db->prepare($sql, $limit, $offset); |
|
240 | - |
|
241 | - if ($this->isAssocArray($params)) { |
|
242 | - foreach ($params as $key => $param) { |
|
243 | - $pdoConstant = $this->getPDOType($param); |
|
244 | - $query->bindValue($key, $param, $pdoConstant); |
|
245 | - } |
|
246 | - } else { |
|
247 | - $index = 1; // bindParam is 1 indexed |
|
248 | - foreach ($params as $param) { |
|
249 | - $pdoConstant = $this->getPDOType($param); |
|
250 | - $query->bindValue($index, $param, $pdoConstant); |
|
251 | - $index++; |
|
252 | - } |
|
253 | - } |
|
254 | - |
|
255 | - $query->execute(); |
|
256 | - |
|
257 | - return $query; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Returns an db result and throws exceptions when there are more or less |
|
262 | - * results |
|
263 | - * @see findEntity |
|
264 | - * @param string $sql the sql query |
|
265 | - * @param array $params the parameters of the sql query |
|
266 | - * @param int $limit the maximum number of rows |
|
267 | - * @param int $offset from which row we want to start |
|
268 | - * @throws DoesNotExistException if the item does not exist |
|
269 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
270 | - * @return array the result as row |
|
271 | - * @since 7.0.0 |
|
272 | - * @deprecated 14.0.0 Move over to QBMapper |
|
273 | - */ |
|
274 | - protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null) { |
|
275 | - $stmt = $this->execute($sql, $params, $limit, $offset); |
|
276 | - $row = $stmt->fetch(); |
|
277 | - |
|
278 | - if($row === false || $row === null){ |
|
279 | - $stmt->closeCursor(); |
|
280 | - $msg = $this->buildDebugMessage( |
|
281 | - 'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
|
282 | - ); |
|
283 | - throw new DoesNotExistException($msg); |
|
284 | - } |
|
285 | - $row2 = $stmt->fetch(); |
|
286 | - $stmt->closeCursor(); |
|
287 | - //MDB2 returns null, PDO and doctrine false when no row is available |
|
288 | - if( ! ($row2 === false || $row2 === null )) { |
|
289 | - $msg = $this->buildDebugMessage( |
|
290 | - 'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
|
291 | - ); |
|
292 | - throw new MultipleObjectsReturnedException($msg); |
|
293 | - } else { |
|
294 | - return $row; |
|
295 | - } |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Builds an error message by prepending the $msg to an error message which |
|
300 | - * has the parameters |
|
301 | - * @see findEntity |
|
302 | - * @param string $sql the sql query |
|
303 | - * @param array $params the parameters of the sql query |
|
304 | - * @param int $limit the maximum number of rows |
|
305 | - * @param int $offset from which row we want to start |
|
306 | - * @return string formatted error message string |
|
307 | - * @since 9.1.0 |
|
308 | - * @deprecated 14.0.0 Move over to QBMapper |
|
309 | - */ |
|
310 | - private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { |
|
311 | - return $msg . |
|
312 | - ': query "' . $sql . '"; ' . |
|
313 | - 'parameters ' . print_r($params, true) . '; ' . |
|
314 | - 'limit "' . $limit . '"; '. |
|
315 | - 'offset "' . $offset . '"'; |
|
316 | - } |
|
317 | - |
|
318 | - |
|
319 | - /** |
|
320 | - * Creates an entity from a row. Automatically determines the entity class |
|
321 | - * from the current mapper name (MyEntityMapper -> MyEntity) |
|
322 | - * @param array $row the row which should be converted to an entity |
|
323 | - * @return Entity the entity |
|
324 | - * @since 7.0.0 |
|
325 | - * @deprecated 14.0.0 Move over to QBMapper |
|
326 | - */ |
|
327 | - protected function mapRowToEntity($row) { |
|
328 | - return call_user_func($this->entityClass .'::fromRow', $row); |
|
329 | - } |
|
330 | - |
|
331 | - |
|
332 | - /** |
|
333 | - * Runs a sql query and returns an array of entities |
|
334 | - * @param string $sql the prepare string |
|
335 | - * @param array $params the params which should replace the ? in the sql query |
|
336 | - * @param int $limit the maximum number of rows |
|
337 | - * @param int $offset from which row we want to start |
|
338 | - * @return array all fetched entities |
|
339 | - * @since 7.0.0 |
|
340 | - * @deprecated 14.0.0 Move over to QBMapper |
|
341 | - */ |
|
342 | - protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
343 | - $stmt = $this->execute($sql, $params, $limit, $offset); |
|
344 | - |
|
345 | - $entities = []; |
|
346 | - |
|
347 | - while($row = $stmt->fetch()){ |
|
348 | - $entities[] = $this->mapRowToEntity($row); |
|
349 | - } |
|
350 | - |
|
351 | - $stmt->closeCursor(); |
|
352 | - |
|
353 | - return $entities; |
|
354 | - } |
|
355 | - |
|
356 | - |
|
357 | - /** |
|
358 | - * Returns an db result and throws exceptions when there are more or less |
|
359 | - * results |
|
360 | - * @param string $sql the sql query |
|
361 | - * @param array $params the parameters of the sql query |
|
362 | - * @param int $limit the maximum number of rows |
|
363 | - * @param int $offset from which row we want to start |
|
364 | - * @throws DoesNotExistException if the item does not exist |
|
365 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
366 | - * @return Entity the entity |
|
367 | - * @since 7.0.0 |
|
368 | - * @deprecated 14.0.0 Move over to QBMapper |
|
369 | - */ |
|
370 | - protected function findEntity($sql, array $params=[], $limit=null, $offset=null) { |
|
371 | - return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
372 | - } |
|
40 | + protected $tableName; |
|
41 | + protected $entityClass; |
|
42 | + protected $db; |
|
43 | + |
|
44 | + /** |
|
45 | + * @param IDBConnection $db Instance of the Db abstraction layer |
|
46 | + * @param string $tableName the name of the table. set this to allow entity |
|
47 | + * @param string $entityClass the name of the entity that the sql should be |
|
48 | + * mapped to queries without using sql |
|
49 | + * @since 7.0.0 |
|
50 | + * @deprecated 14.0.0 Move over to QBMapper |
|
51 | + */ |
|
52 | + public function __construct(IDBConnection $db, $tableName, $entityClass=null) { |
|
53 | + $this->db = $db; |
|
54 | + $this->tableName = '*PREFIX*' . $tableName; |
|
55 | + |
|
56 | + // if not given set the entity name to the class without the mapper part |
|
57 | + // cache it here for later use since reflection is slow |
|
58 | + if($entityClass === null) { |
|
59 | + $this->entityClass = str_replace('Mapper', '', get_class($this)); |
|
60 | + } else { |
|
61 | + $this->entityClass = $entityClass; |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + |
|
66 | + /** |
|
67 | + * @return string the table name |
|
68 | + * @since 7.0.0 |
|
69 | + * @deprecated 14.0.0 Move over to QBMapper |
|
70 | + */ |
|
71 | + public function getTableName() { |
|
72 | + return $this->tableName; |
|
73 | + } |
|
74 | + |
|
75 | + |
|
76 | + /** |
|
77 | + * Deletes an entity from the table |
|
78 | + * @param Entity $entity the entity that should be deleted |
|
79 | + * @return Entity the deleted entity |
|
80 | + * @since 7.0.0 - return value added in 8.1.0 |
|
81 | + * @deprecated 14.0.0 Move over to QBMapper |
|
82 | + */ |
|
83 | + public function delete(Entity $entity) { |
|
84 | + $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
85 | + $stmt = $this->execute($sql, [$entity->getId()]); |
|
86 | + $stmt->closeCursor(); |
|
87 | + return $entity; |
|
88 | + } |
|
89 | + |
|
90 | + |
|
91 | + /** |
|
92 | + * Creates a new entry in the db from an entity |
|
93 | + * @param Entity $entity the entity that should be created |
|
94 | + * @return Entity the saved entity with the set id |
|
95 | + * @since 7.0.0 |
|
96 | + * @deprecated 14.0.0 Move over to QBMapper |
|
97 | + */ |
|
98 | + public function insert(Entity $entity) { |
|
99 | + // get updated fields to save, fields have to be set using a setter to |
|
100 | + // be saved |
|
101 | + $properties = $entity->getUpdatedFields(); |
|
102 | + $values = ''; |
|
103 | + $columns = ''; |
|
104 | + $params = []; |
|
105 | + |
|
106 | + // build the fields |
|
107 | + $i = 0; |
|
108 | + foreach($properties as $property => $updated) { |
|
109 | + $column = $entity->propertyToColumn($property); |
|
110 | + $getter = 'get' . ucfirst($property); |
|
111 | + |
|
112 | + $columns .= '`' . $column . '`'; |
|
113 | + $values .= '?'; |
|
114 | + |
|
115 | + // only append colon if there are more entries |
|
116 | + if($i < count($properties)-1){ |
|
117 | + $columns .= ','; |
|
118 | + $values .= ','; |
|
119 | + } |
|
120 | + |
|
121 | + $params[] = $entity->$getter(); |
|
122 | + $i++; |
|
123 | + |
|
124 | + } |
|
125 | + |
|
126 | + $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
127 | + $columns . ') VALUES(' . $values . ')'; |
|
128 | + |
|
129 | + $stmt = $this->execute($sql, $params); |
|
130 | + |
|
131 | + $entity->setId((int) $this->db->lastInsertId($this->tableName)); |
|
132 | + |
|
133 | + $stmt->closeCursor(); |
|
134 | + |
|
135 | + return $entity; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + |
|
140 | + /** |
|
141 | + * Updates an entry in the db from an entity |
|
142 | + * @throws \InvalidArgumentException if entity has no id |
|
143 | + * @param Entity $entity the entity that should be created |
|
144 | + * @return Entity the saved entity with the set id |
|
145 | + * @since 7.0.0 - return value was added in 8.0.0 |
|
146 | + * @deprecated 14.0.0 Move over to QBMapper |
|
147 | + */ |
|
148 | + public function update(Entity $entity) { |
|
149 | + // if entity wasn't changed it makes no sense to run a db query |
|
150 | + $properties = $entity->getUpdatedFields(); |
|
151 | + if(count($properties) === 0) { |
|
152 | + return $entity; |
|
153 | + } |
|
154 | + |
|
155 | + // entity needs an id |
|
156 | + $id = $entity->getId(); |
|
157 | + if($id === null){ |
|
158 | + throw new \InvalidArgumentException( |
|
159 | + 'Entity which should be updated has no id'); |
|
160 | + } |
|
161 | + |
|
162 | + // get updated fields to save, fields have to be set using a setter to |
|
163 | + // be saved |
|
164 | + // do not update the id field |
|
165 | + unset($properties['id']); |
|
166 | + |
|
167 | + $columns = ''; |
|
168 | + $params = []; |
|
169 | + |
|
170 | + // build the fields |
|
171 | + $i = 0; |
|
172 | + foreach($properties as $property => $updated) { |
|
173 | + |
|
174 | + $column = $entity->propertyToColumn($property); |
|
175 | + $getter = 'get' . ucfirst($property); |
|
176 | + |
|
177 | + $columns .= '`' . $column . '` = ?'; |
|
178 | + |
|
179 | + // only append colon if there are more entries |
|
180 | + if($i < count($properties)-1){ |
|
181 | + $columns .= ','; |
|
182 | + } |
|
183 | + |
|
184 | + $params[] = $entity->$getter(); |
|
185 | + $i++; |
|
186 | + } |
|
187 | + |
|
188 | + $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
189 | + $columns . ' WHERE `id` = ?'; |
|
190 | + $params[] = $id; |
|
191 | + |
|
192 | + $stmt = $this->execute($sql, $params); |
|
193 | + $stmt->closeCursor(); |
|
194 | + |
|
195 | + return $entity; |
|
196 | + } |
|
197 | + |
|
198 | + /** |
|
199 | + * Checks if an array is associative |
|
200 | + * @param array $array |
|
201 | + * @return bool true if associative |
|
202 | + * @since 8.1.0 |
|
203 | + * @deprecated 14.0.0 Move over to QBMapper |
|
204 | + */ |
|
205 | + private function isAssocArray(array $array) { |
|
206 | + return array_values($array) !== $array; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Returns the correct PDO constant based on the value type |
|
211 | + * @param $value |
|
212 | + * @return int PDO constant |
|
213 | + * @since 8.1.0 |
|
214 | + * @deprecated 14.0.0 Move over to QBMapper |
|
215 | + */ |
|
216 | + private function getPDOType($value) { |
|
217 | + switch (gettype($value)) { |
|
218 | + case 'integer': |
|
219 | + return \PDO::PARAM_INT; |
|
220 | + case 'boolean': |
|
221 | + return \PDO::PARAM_BOOL; |
|
222 | + default: |
|
223 | + return \PDO::PARAM_STR; |
|
224 | + } |
|
225 | + } |
|
226 | + |
|
227 | + |
|
228 | + /** |
|
229 | + * Runs an sql query |
|
230 | + * @param string $sql the prepare string |
|
231 | + * @param array $params the params which should replace the ? in the sql query |
|
232 | + * @param int $limit the maximum number of rows |
|
233 | + * @param int $offset from which row we want to start |
|
234 | + * @return \PDOStatement the database query result |
|
235 | + * @since 7.0.0 |
|
236 | + * @deprecated 14.0.0 Move over to QBMapper |
|
237 | + */ |
|
238 | + protected function execute($sql, array $params=[], $limit=null, $offset=null) { |
|
239 | + $query = $this->db->prepare($sql, $limit, $offset); |
|
240 | + |
|
241 | + if ($this->isAssocArray($params)) { |
|
242 | + foreach ($params as $key => $param) { |
|
243 | + $pdoConstant = $this->getPDOType($param); |
|
244 | + $query->bindValue($key, $param, $pdoConstant); |
|
245 | + } |
|
246 | + } else { |
|
247 | + $index = 1; // bindParam is 1 indexed |
|
248 | + foreach ($params as $param) { |
|
249 | + $pdoConstant = $this->getPDOType($param); |
|
250 | + $query->bindValue($index, $param, $pdoConstant); |
|
251 | + $index++; |
|
252 | + } |
|
253 | + } |
|
254 | + |
|
255 | + $query->execute(); |
|
256 | + |
|
257 | + return $query; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Returns an db result and throws exceptions when there are more or less |
|
262 | + * results |
|
263 | + * @see findEntity |
|
264 | + * @param string $sql the sql query |
|
265 | + * @param array $params the parameters of the sql query |
|
266 | + * @param int $limit the maximum number of rows |
|
267 | + * @param int $offset from which row we want to start |
|
268 | + * @throws DoesNotExistException if the item does not exist |
|
269 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
270 | + * @return array the result as row |
|
271 | + * @since 7.0.0 |
|
272 | + * @deprecated 14.0.0 Move over to QBMapper |
|
273 | + */ |
|
274 | + protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null) { |
|
275 | + $stmt = $this->execute($sql, $params, $limit, $offset); |
|
276 | + $row = $stmt->fetch(); |
|
277 | + |
|
278 | + if($row === false || $row === null){ |
|
279 | + $stmt->closeCursor(); |
|
280 | + $msg = $this->buildDebugMessage( |
|
281 | + 'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
|
282 | + ); |
|
283 | + throw new DoesNotExistException($msg); |
|
284 | + } |
|
285 | + $row2 = $stmt->fetch(); |
|
286 | + $stmt->closeCursor(); |
|
287 | + //MDB2 returns null, PDO and doctrine false when no row is available |
|
288 | + if( ! ($row2 === false || $row2 === null )) { |
|
289 | + $msg = $this->buildDebugMessage( |
|
290 | + 'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
|
291 | + ); |
|
292 | + throw new MultipleObjectsReturnedException($msg); |
|
293 | + } else { |
|
294 | + return $row; |
|
295 | + } |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Builds an error message by prepending the $msg to an error message which |
|
300 | + * has the parameters |
|
301 | + * @see findEntity |
|
302 | + * @param string $sql the sql query |
|
303 | + * @param array $params the parameters of the sql query |
|
304 | + * @param int $limit the maximum number of rows |
|
305 | + * @param int $offset from which row we want to start |
|
306 | + * @return string formatted error message string |
|
307 | + * @since 9.1.0 |
|
308 | + * @deprecated 14.0.0 Move over to QBMapper |
|
309 | + */ |
|
310 | + private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { |
|
311 | + return $msg . |
|
312 | + ': query "' . $sql . '"; ' . |
|
313 | + 'parameters ' . print_r($params, true) . '; ' . |
|
314 | + 'limit "' . $limit . '"; '. |
|
315 | + 'offset "' . $offset . '"'; |
|
316 | + } |
|
317 | + |
|
318 | + |
|
319 | + /** |
|
320 | + * Creates an entity from a row. Automatically determines the entity class |
|
321 | + * from the current mapper name (MyEntityMapper -> MyEntity) |
|
322 | + * @param array $row the row which should be converted to an entity |
|
323 | + * @return Entity the entity |
|
324 | + * @since 7.0.0 |
|
325 | + * @deprecated 14.0.0 Move over to QBMapper |
|
326 | + */ |
|
327 | + protected function mapRowToEntity($row) { |
|
328 | + return call_user_func($this->entityClass .'::fromRow', $row); |
|
329 | + } |
|
330 | + |
|
331 | + |
|
332 | + /** |
|
333 | + * Runs a sql query and returns an array of entities |
|
334 | + * @param string $sql the prepare string |
|
335 | + * @param array $params the params which should replace the ? in the sql query |
|
336 | + * @param int $limit the maximum number of rows |
|
337 | + * @param int $offset from which row we want to start |
|
338 | + * @return array all fetched entities |
|
339 | + * @since 7.0.0 |
|
340 | + * @deprecated 14.0.0 Move over to QBMapper |
|
341 | + */ |
|
342 | + protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
343 | + $stmt = $this->execute($sql, $params, $limit, $offset); |
|
344 | + |
|
345 | + $entities = []; |
|
346 | + |
|
347 | + while($row = $stmt->fetch()){ |
|
348 | + $entities[] = $this->mapRowToEntity($row); |
|
349 | + } |
|
350 | + |
|
351 | + $stmt->closeCursor(); |
|
352 | + |
|
353 | + return $entities; |
|
354 | + } |
|
355 | + |
|
356 | + |
|
357 | + /** |
|
358 | + * Returns an db result and throws exceptions when there are more or less |
|
359 | + * results |
|
360 | + * @param string $sql the sql query |
|
361 | + * @param array $params the parameters of the sql query |
|
362 | + * @param int $limit the maximum number of rows |
|
363 | + * @param int $offset from which row we want to start |
|
364 | + * @throws DoesNotExistException if the item does not exist |
|
365 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
366 | + * @return Entity the entity |
|
367 | + * @since 7.0.0 |
|
368 | + * @deprecated 14.0.0 Move over to QBMapper |
|
369 | + */ |
|
370 | + protected function findEntity($sql, array $params=[], $limit=null, $offset=null) { |
|
371 | + return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
|
372 | + } |
|
373 | 373 | |
374 | 374 | |
375 | 375 | } |
@@ -49,13 +49,13 @@ discard block |
||
49 | 49 | * @since 7.0.0 |
50 | 50 | * @deprecated 14.0.0 Move over to QBMapper |
51 | 51 | */ |
52 | - public function __construct(IDBConnection $db, $tableName, $entityClass=null) { |
|
52 | + public function __construct(IDBConnection $db, $tableName, $entityClass = null) { |
|
53 | 53 | $this->db = $db; |
54 | - $this->tableName = '*PREFIX*' . $tableName; |
|
54 | + $this->tableName = '*PREFIX*'.$tableName; |
|
55 | 55 | |
56 | 56 | // if not given set the entity name to the class without the mapper part |
57 | 57 | // cache it here for later use since reflection is slow |
58 | - if($entityClass === null) { |
|
58 | + if ($entityClass === null) { |
|
59 | 59 | $this->entityClass = str_replace('Mapper', '', get_class($this)); |
60 | 60 | } else { |
61 | 61 | $this->entityClass = $entityClass; |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | * @deprecated 14.0.0 Move over to QBMapper |
82 | 82 | */ |
83 | 83 | public function delete(Entity $entity) { |
84 | - $sql = 'DELETE FROM `' . $this->tableName . '` WHERE `id` = ?'; |
|
84 | + $sql = 'DELETE FROM `'.$this->tableName.'` WHERE `id` = ?'; |
|
85 | 85 | $stmt = $this->execute($sql, [$entity->getId()]); |
86 | 86 | $stmt->closeCursor(); |
87 | 87 | return $entity; |
@@ -105,15 +105,15 @@ discard block |
||
105 | 105 | |
106 | 106 | // build the fields |
107 | 107 | $i = 0; |
108 | - foreach($properties as $property => $updated) { |
|
108 | + foreach ($properties as $property => $updated) { |
|
109 | 109 | $column = $entity->propertyToColumn($property); |
110 | - $getter = 'get' . ucfirst($property); |
|
110 | + $getter = 'get'.ucfirst($property); |
|
111 | 111 | |
112 | - $columns .= '`' . $column . '`'; |
|
112 | + $columns .= '`'.$column.'`'; |
|
113 | 113 | $values .= '?'; |
114 | 114 | |
115 | 115 | // only append colon if there are more entries |
116 | - if($i < count($properties)-1){ |
|
116 | + if ($i < count($properties) - 1) { |
|
117 | 117 | $columns .= ','; |
118 | 118 | $values .= ','; |
119 | 119 | } |
@@ -123,8 +123,8 @@ discard block |
||
123 | 123 | |
124 | 124 | } |
125 | 125 | |
126 | - $sql = 'INSERT INTO `' . $this->tableName . '`(' . |
|
127 | - $columns . ') VALUES(' . $values . ')'; |
|
126 | + $sql = 'INSERT INTO `'.$this->tableName.'`('. |
|
127 | + $columns.') VALUES('.$values.')'; |
|
128 | 128 | |
129 | 129 | $stmt = $this->execute($sql, $params); |
130 | 130 | |
@@ -148,13 +148,13 @@ discard block |
||
148 | 148 | public function update(Entity $entity) { |
149 | 149 | // if entity wasn't changed it makes no sense to run a db query |
150 | 150 | $properties = $entity->getUpdatedFields(); |
151 | - if(count($properties) === 0) { |
|
151 | + if (count($properties) === 0) { |
|
152 | 152 | return $entity; |
153 | 153 | } |
154 | 154 | |
155 | 155 | // entity needs an id |
156 | 156 | $id = $entity->getId(); |
157 | - if($id === null){ |
|
157 | + if ($id === null) { |
|
158 | 158 | throw new \InvalidArgumentException( |
159 | 159 | 'Entity which should be updated has no id'); |
160 | 160 | } |
@@ -169,15 +169,15 @@ discard block |
||
169 | 169 | |
170 | 170 | // build the fields |
171 | 171 | $i = 0; |
172 | - foreach($properties as $property => $updated) { |
|
172 | + foreach ($properties as $property => $updated) { |
|
173 | 173 | |
174 | 174 | $column = $entity->propertyToColumn($property); |
175 | - $getter = 'get' . ucfirst($property); |
|
175 | + $getter = 'get'.ucfirst($property); |
|
176 | 176 | |
177 | - $columns .= '`' . $column . '` = ?'; |
|
177 | + $columns .= '`'.$column.'` = ?'; |
|
178 | 178 | |
179 | 179 | // only append colon if there are more entries |
180 | - if($i < count($properties)-1){ |
|
180 | + if ($i < count($properties) - 1) { |
|
181 | 181 | $columns .= ','; |
182 | 182 | } |
183 | 183 | |
@@ -185,8 +185,8 @@ discard block |
||
185 | 185 | $i++; |
186 | 186 | } |
187 | 187 | |
188 | - $sql = 'UPDATE `' . $this->tableName . '` SET ' . |
|
189 | - $columns . ' WHERE `id` = ?'; |
|
188 | + $sql = 'UPDATE `'.$this->tableName.'` SET '. |
|
189 | + $columns.' WHERE `id` = ?'; |
|
190 | 190 | $params[] = $id; |
191 | 191 | |
192 | 192 | $stmt = $this->execute($sql, $params); |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | * @since 7.0.0 |
236 | 236 | * @deprecated 14.0.0 Move over to QBMapper |
237 | 237 | */ |
238 | - protected function execute($sql, array $params=[], $limit=null, $offset=null) { |
|
238 | + protected function execute($sql, array $params = [], $limit = null, $offset = null) { |
|
239 | 239 | $query = $this->db->prepare($sql, $limit, $offset); |
240 | 240 | |
241 | 241 | if ($this->isAssocArray($params)) { |
@@ -244,7 +244,7 @@ discard block |
||
244 | 244 | $query->bindValue($key, $param, $pdoConstant); |
245 | 245 | } |
246 | 246 | } else { |
247 | - $index = 1; // bindParam is 1 indexed |
|
247 | + $index = 1; // bindParam is 1 indexed |
|
248 | 248 | foreach ($params as $param) { |
249 | 249 | $pdoConstant = $this->getPDOType($param); |
250 | 250 | $query->bindValue($index, $param, $pdoConstant); |
@@ -271,11 +271,11 @@ discard block |
||
271 | 271 | * @since 7.0.0 |
272 | 272 | * @deprecated 14.0.0 Move over to QBMapper |
273 | 273 | */ |
274 | - protected function findOneQuery($sql, array $params=[], $limit=null, $offset=null) { |
|
274 | + protected function findOneQuery($sql, array $params = [], $limit = null, $offset = null) { |
|
275 | 275 | $stmt = $this->execute($sql, $params, $limit, $offset); |
276 | 276 | $row = $stmt->fetch(); |
277 | 277 | |
278 | - if($row === false || $row === null){ |
|
278 | + if ($row === false || $row === null) { |
|
279 | 279 | $stmt->closeCursor(); |
280 | 280 | $msg = $this->buildDebugMessage( |
281 | 281 | 'Did expect one result but found none when executing', $sql, $params, $limit, $offset |
@@ -285,7 +285,7 @@ discard block |
||
285 | 285 | $row2 = $stmt->fetch(); |
286 | 286 | $stmt->closeCursor(); |
287 | 287 | //MDB2 returns null, PDO and doctrine false when no row is available |
288 | - if( ! ($row2 === false || $row2 === null )) { |
|
288 | + if (!($row2 === false || $row2 === null)) { |
|
289 | 289 | $msg = $this->buildDebugMessage( |
290 | 290 | 'Did not expect more than one result when executing', $sql, $params, $limit, $offset |
291 | 291 | ); |
@@ -307,12 +307,12 @@ discard block |
||
307 | 307 | * @since 9.1.0 |
308 | 308 | * @deprecated 14.0.0 Move over to QBMapper |
309 | 309 | */ |
310 | - private function buildDebugMessage($msg, $sql, array $params=[], $limit=null, $offset=null) { |
|
311 | - return $msg . |
|
312 | - ': query "' . $sql . '"; ' . |
|
313 | - 'parameters ' . print_r($params, true) . '; ' . |
|
314 | - 'limit "' . $limit . '"; '. |
|
315 | - 'offset "' . $offset . '"'; |
|
310 | + private function buildDebugMessage($msg, $sql, array $params = [], $limit = null, $offset = null) { |
|
311 | + return $msg. |
|
312 | + ': query "'.$sql.'"; '. |
|
313 | + 'parameters '.print_r($params, true).'; '. |
|
314 | + 'limit "'.$limit.'"; '. |
|
315 | + 'offset "'.$offset.'"'; |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | |
@@ -325,7 +325,7 @@ discard block |
||
325 | 325 | * @deprecated 14.0.0 Move over to QBMapper |
326 | 326 | */ |
327 | 327 | protected function mapRowToEntity($row) { |
328 | - return call_user_func($this->entityClass .'::fromRow', $row); |
|
328 | + return call_user_func($this->entityClass.'::fromRow', $row); |
|
329 | 329 | } |
330 | 330 | |
331 | 331 | |
@@ -339,12 +339,12 @@ discard block |
||
339 | 339 | * @since 7.0.0 |
340 | 340 | * @deprecated 14.0.0 Move over to QBMapper |
341 | 341 | */ |
342 | - protected function findEntities($sql, array $params=[], $limit=null, $offset=null) { |
|
342 | + protected function findEntities($sql, array $params = [], $limit = null, $offset = null) { |
|
343 | 343 | $stmt = $this->execute($sql, $params, $limit, $offset); |
344 | 344 | |
345 | 345 | $entities = []; |
346 | 346 | |
347 | - while($row = $stmt->fetch()){ |
|
347 | + while ($row = $stmt->fetch()) { |
|
348 | 348 | $entities[] = $this->mapRowToEntity($row); |
349 | 349 | } |
350 | 350 | |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | * @since 7.0.0 |
368 | 368 | * @deprecated 14.0.0 Move over to QBMapper |
369 | 369 | */ |
370 | - protected function findEntity($sql, array $params=[], $limit=null, $offset=null) { |
|
370 | + protected function findEntity($sql, array $params = [], $limit = null, $offset = null) { |
|
371 | 371 | return $this->mapRowToEntity($this->findOneQuery($sql, $params, $limit, $offset)); |
372 | 372 | } |
373 | 373 |
@@ -34,13 +34,13 @@ |
||
34 | 34 | */ |
35 | 35 | class MultipleObjectsReturnedException extends \Exception implements IMapperException { |
36 | 36 | |
37 | - /** |
|
38 | - * Constructor |
|
39 | - * @param string $msg the error message |
|
40 | - * @since 7.0.0 |
|
41 | - */ |
|
42 | - public function __construct($msg) { |
|
43 | - parent::__construct($msg); |
|
44 | - } |
|
37 | + /** |
|
38 | + * Constructor |
|
39 | + * @param string $msg the error message |
|
40 | + * @since 7.0.0 |
|
41 | + */ |
|
42 | + public function __construct($msg) { |
|
43 | + parent::__construct($msg); |
|
44 | + } |
|
45 | 45 | |
46 | 46 | } |
@@ -42,294 +42,294 @@ |
||
42 | 42 | */ |
43 | 43 | abstract class QBMapper { |
44 | 44 | |
45 | - /** @var string */ |
|
46 | - protected $tableName; |
|
47 | - |
|
48 | - /** @var string */ |
|
49 | - protected $entityClass; |
|
50 | - |
|
51 | - /** @var IDBConnection */ |
|
52 | - protected $db; |
|
53 | - |
|
54 | - /** |
|
55 | - * @param IDBConnection $db Instance of the Db abstraction layer |
|
56 | - * @param string $tableName the name of the table. set this to allow entity |
|
57 | - * @param string $entityClass the name of the entity that the sql should be |
|
58 | - * mapped to queries without using sql |
|
59 | - * @since 14.0.0 |
|
60 | - */ |
|
61 | - public function __construct(IDBConnection $db, string $tableName, string $entityClass=null) { |
|
62 | - $this->db = $db; |
|
63 | - $this->tableName = $tableName; |
|
64 | - |
|
65 | - // if not given set the entity name to the class without the mapper part |
|
66 | - // cache it here for later use since reflection is slow |
|
67 | - if($entityClass === null) { |
|
68 | - $this->entityClass = str_replace('Mapper', '', \get_class($this)); |
|
69 | - } else { |
|
70 | - $this->entityClass = $entityClass; |
|
71 | - } |
|
72 | - } |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * @return string the table name |
|
77 | - * @since 14.0.0 |
|
78 | - */ |
|
79 | - public function getTableName(): string { |
|
80 | - return $this->tableName; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Deletes an entity from the table |
|
86 | - * @param Entity $entity the entity that should be deleted |
|
87 | - * @return Entity the deleted entity |
|
88 | - * @since 14.0.0 |
|
89 | - */ |
|
90 | - public function delete(Entity $entity): Entity { |
|
91 | - $qb = $this->db->getQueryBuilder(); |
|
92 | - |
|
93 | - $idType = $this->getParameterTypeForProperty($entity, 'id'); |
|
94 | - |
|
95 | - $qb->delete($this->tableName) |
|
96 | - ->where( |
|
97 | - $qb->expr()->eq('id', $qb->createNamedParameter($entity->getId(), $idType)) |
|
98 | - ); |
|
99 | - $qb->execute(); |
|
100 | - return $entity; |
|
101 | - } |
|
102 | - |
|
103 | - |
|
104 | - /** |
|
105 | - * Creates a new entry in the db from an entity |
|
106 | - * @param Entity $entity the entity that should be created |
|
107 | - * @return Entity the saved entity with the set id |
|
108 | - * @since 14.0.0 |
|
109 | - * @suppress SqlInjectionChecker |
|
110 | - */ |
|
111 | - public function insert(Entity $entity): Entity { |
|
112 | - // get updated fields to save, fields have to be set using a setter to |
|
113 | - // be saved |
|
114 | - $properties = $entity->getUpdatedFields(); |
|
115 | - |
|
116 | - $qb = $this->db->getQueryBuilder(); |
|
117 | - $qb->insert($this->tableName); |
|
118 | - |
|
119 | - // build the fields |
|
120 | - foreach($properties as $property => $updated) { |
|
121 | - $column = $entity->propertyToColumn($property); |
|
122 | - $getter = 'get' . ucfirst($property); |
|
123 | - $value = $entity->$getter(); |
|
124 | - |
|
125 | - $type = $this->getParameterTypeForProperty($entity, $property); |
|
126 | - $qb->setValue($column, $qb->createNamedParameter($value, $type)); |
|
127 | - } |
|
128 | - |
|
129 | - $qb->execute(); |
|
130 | - |
|
131 | - if($entity->id === null) { |
|
132 | - // When autoincrement is used id is always an int |
|
133 | - $entity->setId((int)$qb->getLastInsertId()); |
|
134 | - } |
|
135 | - |
|
136 | - return $entity; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Tries to creates a new entry in the db from an entity and |
|
141 | - * updates an existing entry if duplicate keys are detected |
|
142 | - * by the database |
|
143 | - * |
|
144 | - * @param Entity $entity the entity that should be created/updated |
|
145 | - * @return Entity the saved entity with the (new) id |
|
146 | - * @throws \InvalidArgumentException if entity has no id |
|
147 | - * @since 15.0.0 |
|
148 | - * @suppress SqlInjectionChecker |
|
149 | - */ |
|
150 | - public function insertOrUpdate(Entity $entity): Entity { |
|
151 | - try { |
|
152 | - return $this->insert($entity); |
|
153 | - } catch (UniqueConstraintViolationException $ex) { |
|
154 | - return $this->update($entity); |
|
155 | - } |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Updates an entry in the db from an entity |
|
160 | - * @throws \InvalidArgumentException if entity has no id |
|
161 | - * @param Entity $entity the entity that should be created |
|
162 | - * @return Entity the saved entity with the set id |
|
163 | - * @since 14.0.0 |
|
164 | - * @suppress SqlInjectionChecker |
|
165 | - */ |
|
166 | - public function update(Entity $entity): Entity { |
|
167 | - // if entity wasn't changed it makes no sense to run a db query |
|
168 | - $properties = $entity->getUpdatedFields(); |
|
169 | - if(\count($properties) === 0) { |
|
170 | - return $entity; |
|
171 | - } |
|
172 | - |
|
173 | - // entity needs an id |
|
174 | - $id = $entity->getId(); |
|
175 | - if($id === null){ |
|
176 | - throw new \InvalidArgumentException( |
|
177 | - 'Entity which should be updated has no id'); |
|
178 | - } |
|
179 | - |
|
180 | - // get updated fields to save, fields have to be set using a setter to |
|
181 | - // be saved |
|
182 | - // do not update the id field |
|
183 | - unset($properties['id']); |
|
184 | - |
|
185 | - $qb = $this->db->getQueryBuilder(); |
|
186 | - $qb->update($this->tableName); |
|
187 | - |
|
188 | - // build the fields |
|
189 | - foreach($properties as $property => $updated) { |
|
190 | - $column = $entity->propertyToColumn($property); |
|
191 | - $getter = 'get' . ucfirst($property); |
|
192 | - $value = $entity->$getter(); |
|
193 | - |
|
194 | - $type = $this->getParameterTypeForProperty($entity, $property); |
|
195 | - $qb->set($column, $qb->createNamedParameter($value, $type)); |
|
196 | - } |
|
197 | - |
|
198 | - $idType = $this->getParameterTypeForProperty($entity, 'id'); |
|
199 | - |
|
200 | - $qb->where( |
|
201 | - $qb->expr()->eq('id', $qb->createNamedParameter($id, $idType)) |
|
202 | - ); |
|
203 | - $qb->execute(); |
|
204 | - |
|
205 | - return $entity; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Returns the type parameter for the QueryBuilder for a specific property |
|
210 | - * of the $entity |
|
211 | - * |
|
212 | - * @param Entity $entity The entity to get the types from |
|
213 | - * @param string $property The property of $entity to get the type for |
|
214 | - * @return int |
|
215 | - * @since 16.0.0 |
|
216 | - */ |
|
217 | - protected function getParameterTypeForProperty(Entity $entity, string $property): int { |
|
218 | - $types = $entity->getFieldTypes(); |
|
219 | - |
|
220 | - if(!isset($types[ $property ])) { |
|
221 | - return IQueryBuilder::PARAM_STR; |
|
222 | - } |
|
223 | - |
|
224 | - switch($types[ $property ]) { |
|
225 | - case 'int': |
|
226 | - case 'integer': |
|
227 | - return IQueryBuilder::PARAM_INT; |
|
228 | - case 'string': |
|
229 | - return IQueryBuilder::PARAM_STR; |
|
230 | - case 'bool': |
|
231 | - case 'boolean': |
|
232 | - return IQueryBuilder::PARAM_BOOL; |
|
233 | - } |
|
234 | - |
|
235 | - return IQueryBuilder::PARAM_STR; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Returns an db result and throws exceptions when there are more or less |
|
240 | - * results |
|
241 | - * |
|
242 | - * @see findEntity |
|
243 | - * |
|
244 | - * @param IQueryBuilder $query |
|
245 | - * @throws DoesNotExistException if the item does not exist |
|
246 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
247 | - * @return array the result as row |
|
248 | - * @since 14.0.0 |
|
249 | - */ |
|
250 | - protected function findOneQuery(IQueryBuilder $query): array { |
|
251 | - $cursor = $query->execute(); |
|
252 | - |
|
253 | - $row = $cursor->fetch(); |
|
254 | - if($row === false) { |
|
255 | - $cursor->closeCursor(); |
|
256 | - $msg = $this->buildDebugMessage( |
|
257 | - 'Did expect one result but found none when executing', $query |
|
258 | - ); |
|
259 | - throw new DoesNotExistException($msg); |
|
260 | - } |
|
261 | - |
|
262 | - $row2 = $cursor->fetch(); |
|
263 | - $cursor->closeCursor(); |
|
264 | - if($row2 !== false ) { |
|
265 | - $msg = $this->buildDebugMessage( |
|
266 | - 'Did not expect more than one result when executing', $query |
|
267 | - ); |
|
268 | - throw new MultipleObjectsReturnedException($msg); |
|
269 | - } |
|
270 | - |
|
271 | - return $row; |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * @param string $msg |
|
276 | - * @param IQueryBuilder $sql |
|
277 | - * @return string |
|
278 | - * @since 14.0.0 |
|
279 | - */ |
|
280 | - private function buildDebugMessage(string $msg, IQueryBuilder $sql): string { |
|
281 | - return $msg . |
|
282 | - ': query "' . $sql->getSQL() . '"; '; |
|
283 | - } |
|
284 | - |
|
285 | - |
|
286 | - /** |
|
287 | - * Creates an entity from a row. Automatically determines the entity class |
|
288 | - * from the current mapper name (MyEntityMapper -> MyEntity) |
|
289 | - * |
|
290 | - * @param array $row the row which should be converted to an entity |
|
291 | - * @return Entity the entity |
|
292 | - * @since 14.0.0 |
|
293 | - */ |
|
294 | - protected function mapRowToEntity(array $row): Entity { |
|
295 | - return \call_user_func($this->entityClass .'::fromRow', $row); |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * Runs a sql query and returns an array of entities |
|
301 | - * |
|
302 | - * @param IQueryBuilder $query |
|
303 | - * @return Entity[] all fetched entities |
|
304 | - * @since 14.0.0 |
|
305 | - */ |
|
306 | - protected function findEntities(IQueryBuilder $query): array { |
|
307 | - $cursor = $query->execute(); |
|
308 | - |
|
309 | - $entities = []; |
|
310 | - |
|
311 | - while($row = $cursor->fetch()){ |
|
312 | - $entities[] = $this->mapRowToEntity($row); |
|
313 | - } |
|
314 | - |
|
315 | - $cursor->closeCursor(); |
|
316 | - |
|
317 | - return $entities; |
|
318 | - } |
|
319 | - |
|
320 | - |
|
321 | - /** |
|
322 | - * Returns an db result and throws exceptions when there are more or less |
|
323 | - * results |
|
324 | - * |
|
325 | - * @param IQueryBuilder $query |
|
326 | - * @throws DoesNotExistException if the item does not exist |
|
327 | - * @throws MultipleObjectsReturnedException if more than one item exist |
|
328 | - * @return Entity the entity |
|
329 | - * @since 14.0.0 |
|
330 | - */ |
|
331 | - protected function findEntity(IQueryBuilder $query): Entity { |
|
332 | - return $this->mapRowToEntity($this->findOneQuery($query)); |
|
333 | - } |
|
45 | + /** @var string */ |
|
46 | + protected $tableName; |
|
47 | + |
|
48 | + /** @var string */ |
|
49 | + protected $entityClass; |
|
50 | + |
|
51 | + /** @var IDBConnection */ |
|
52 | + protected $db; |
|
53 | + |
|
54 | + /** |
|
55 | + * @param IDBConnection $db Instance of the Db abstraction layer |
|
56 | + * @param string $tableName the name of the table. set this to allow entity |
|
57 | + * @param string $entityClass the name of the entity that the sql should be |
|
58 | + * mapped to queries without using sql |
|
59 | + * @since 14.0.0 |
|
60 | + */ |
|
61 | + public function __construct(IDBConnection $db, string $tableName, string $entityClass=null) { |
|
62 | + $this->db = $db; |
|
63 | + $this->tableName = $tableName; |
|
64 | + |
|
65 | + // if not given set the entity name to the class without the mapper part |
|
66 | + // cache it here for later use since reflection is slow |
|
67 | + if($entityClass === null) { |
|
68 | + $this->entityClass = str_replace('Mapper', '', \get_class($this)); |
|
69 | + } else { |
|
70 | + $this->entityClass = $entityClass; |
|
71 | + } |
|
72 | + } |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * @return string the table name |
|
77 | + * @since 14.0.0 |
|
78 | + */ |
|
79 | + public function getTableName(): string { |
|
80 | + return $this->tableName; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Deletes an entity from the table |
|
86 | + * @param Entity $entity the entity that should be deleted |
|
87 | + * @return Entity the deleted entity |
|
88 | + * @since 14.0.0 |
|
89 | + */ |
|
90 | + public function delete(Entity $entity): Entity { |
|
91 | + $qb = $this->db->getQueryBuilder(); |
|
92 | + |
|
93 | + $idType = $this->getParameterTypeForProperty($entity, 'id'); |
|
94 | + |
|
95 | + $qb->delete($this->tableName) |
|
96 | + ->where( |
|
97 | + $qb->expr()->eq('id', $qb->createNamedParameter($entity->getId(), $idType)) |
|
98 | + ); |
|
99 | + $qb->execute(); |
|
100 | + return $entity; |
|
101 | + } |
|
102 | + |
|
103 | + |
|
104 | + /** |
|
105 | + * Creates a new entry in the db from an entity |
|
106 | + * @param Entity $entity the entity that should be created |
|
107 | + * @return Entity the saved entity with the set id |
|
108 | + * @since 14.0.0 |
|
109 | + * @suppress SqlInjectionChecker |
|
110 | + */ |
|
111 | + public function insert(Entity $entity): Entity { |
|
112 | + // get updated fields to save, fields have to be set using a setter to |
|
113 | + // be saved |
|
114 | + $properties = $entity->getUpdatedFields(); |
|
115 | + |
|
116 | + $qb = $this->db->getQueryBuilder(); |
|
117 | + $qb->insert($this->tableName); |
|
118 | + |
|
119 | + // build the fields |
|
120 | + foreach($properties as $property => $updated) { |
|
121 | + $column = $entity->propertyToColumn($property); |
|
122 | + $getter = 'get' . ucfirst($property); |
|
123 | + $value = $entity->$getter(); |
|
124 | + |
|
125 | + $type = $this->getParameterTypeForProperty($entity, $property); |
|
126 | + $qb->setValue($column, $qb->createNamedParameter($value, $type)); |
|
127 | + } |
|
128 | + |
|
129 | + $qb->execute(); |
|
130 | + |
|
131 | + if($entity->id === null) { |
|
132 | + // When autoincrement is used id is always an int |
|
133 | + $entity->setId((int)$qb->getLastInsertId()); |
|
134 | + } |
|
135 | + |
|
136 | + return $entity; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Tries to creates a new entry in the db from an entity and |
|
141 | + * updates an existing entry if duplicate keys are detected |
|
142 | + * by the database |
|
143 | + * |
|
144 | + * @param Entity $entity the entity that should be created/updated |
|
145 | + * @return Entity the saved entity with the (new) id |
|
146 | + * @throws \InvalidArgumentException if entity has no id |
|
147 | + * @since 15.0.0 |
|
148 | + * @suppress SqlInjectionChecker |
|
149 | + */ |
|
150 | + public function insertOrUpdate(Entity $entity): Entity { |
|
151 | + try { |
|
152 | + return $this->insert($entity); |
|
153 | + } catch (UniqueConstraintViolationException $ex) { |
|
154 | + return $this->update($entity); |
|
155 | + } |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Updates an entry in the db from an entity |
|
160 | + * @throws \InvalidArgumentException if entity has no id |
|
161 | + * @param Entity $entity the entity that should be created |
|
162 | + * @return Entity the saved entity with the set id |
|
163 | + * @since 14.0.0 |
|
164 | + * @suppress SqlInjectionChecker |
|
165 | + */ |
|
166 | + public function update(Entity $entity): Entity { |
|
167 | + // if entity wasn't changed it makes no sense to run a db query |
|
168 | + $properties = $entity->getUpdatedFields(); |
|
169 | + if(\count($properties) === 0) { |
|
170 | + return $entity; |
|
171 | + } |
|
172 | + |
|
173 | + // entity needs an id |
|
174 | + $id = $entity->getId(); |
|
175 | + if($id === null){ |
|
176 | + throw new \InvalidArgumentException( |
|
177 | + 'Entity which should be updated has no id'); |
|
178 | + } |
|
179 | + |
|
180 | + // get updated fields to save, fields have to be set using a setter to |
|
181 | + // be saved |
|
182 | + // do not update the id field |
|
183 | + unset($properties['id']); |
|
184 | + |
|
185 | + $qb = $this->db->getQueryBuilder(); |
|
186 | + $qb->update($this->tableName); |
|
187 | + |
|
188 | + // build the fields |
|
189 | + foreach($properties as $property => $updated) { |
|
190 | + $column = $entity->propertyToColumn($property); |
|
191 | + $getter = 'get' . ucfirst($property); |
|
192 | + $value = $entity->$getter(); |
|
193 | + |
|
194 | + $type = $this->getParameterTypeForProperty($entity, $property); |
|
195 | + $qb->set($column, $qb->createNamedParameter($value, $type)); |
|
196 | + } |
|
197 | + |
|
198 | + $idType = $this->getParameterTypeForProperty($entity, 'id'); |
|
199 | + |
|
200 | + $qb->where( |
|
201 | + $qb->expr()->eq('id', $qb->createNamedParameter($id, $idType)) |
|
202 | + ); |
|
203 | + $qb->execute(); |
|
204 | + |
|
205 | + return $entity; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Returns the type parameter for the QueryBuilder for a specific property |
|
210 | + * of the $entity |
|
211 | + * |
|
212 | + * @param Entity $entity The entity to get the types from |
|
213 | + * @param string $property The property of $entity to get the type for |
|
214 | + * @return int |
|
215 | + * @since 16.0.0 |
|
216 | + */ |
|
217 | + protected function getParameterTypeForProperty(Entity $entity, string $property): int { |
|
218 | + $types = $entity->getFieldTypes(); |
|
219 | + |
|
220 | + if(!isset($types[ $property ])) { |
|
221 | + return IQueryBuilder::PARAM_STR; |
|
222 | + } |
|
223 | + |
|
224 | + switch($types[ $property ]) { |
|
225 | + case 'int': |
|
226 | + case 'integer': |
|
227 | + return IQueryBuilder::PARAM_INT; |
|
228 | + case 'string': |
|
229 | + return IQueryBuilder::PARAM_STR; |
|
230 | + case 'bool': |
|
231 | + case 'boolean': |
|
232 | + return IQueryBuilder::PARAM_BOOL; |
|
233 | + } |
|
234 | + |
|
235 | + return IQueryBuilder::PARAM_STR; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Returns an db result and throws exceptions when there are more or less |
|
240 | + * results |
|
241 | + * |
|
242 | + * @see findEntity |
|
243 | + * |
|
244 | + * @param IQueryBuilder $query |
|
245 | + * @throws DoesNotExistException if the item does not exist |
|
246 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
247 | + * @return array the result as row |
|
248 | + * @since 14.0.0 |
|
249 | + */ |
|
250 | + protected function findOneQuery(IQueryBuilder $query): array { |
|
251 | + $cursor = $query->execute(); |
|
252 | + |
|
253 | + $row = $cursor->fetch(); |
|
254 | + if($row === false) { |
|
255 | + $cursor->closeCursor(); |
|
256 | + $msg = $this->buildDebugMessage( |
|
257 | + 'Did expect one result but found none when executing', $query |
|
258 | + ); |
|
259 | + throw new DoesNotExistException($msg); |
|
260 | + } |
|
261 | + |
|
262 | + $row2 = $cursor->fetch(); |
|
263 | + $cursor->closeCursor(); |
|
264 | + if($row2 !== false ) { |
|
265 | + $msg = $this->buildDebugMessage( |
|
266 | + 'Did not expect more than one result when executing', $query |
|
267 | + ); |
|
268 | + throw new MultipleObjectsReturnedException($msg); |
|
269 | + } |
|
270 | + |
|
271 | + return $row; |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * @param string $msg |
|
276 | + * @param IQueryBuilder $sql |
|
277 | + * @return string |
|
278 | + * @since 14.0.0 |
|
279 | + */ |
|
280 | + private function buildDebugMessage(string $msg, IQueryBuilder $sql): string { |
|
281 | + return $msg . |
|
282 | + ': query "' . $sql->getSQL() . '"; '; |
|
283 | + } |
|
284 | + |
|
285 | + |
|
286 | + /** |
|
287 | + * Creates an entity from a row. Automatically determines the entity class |
|
288 | + * from the current mapper name (MyEntityMapper -> MyEntity) |
|
289 | + * |
|
290 | + * @param array $row the row which should be converted to an entity |
|
291 | + * @return Entity the entity |
|
292 | + * @since 14.0.0 |
|
293 | + */ |
|
294 | + protected function mapRowToEntity(array $row): Entity { |
|
295 | + return \call_user_func($this->entityClass .'::fromRow', $row); |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * Runs a sql query and returns an array of entities |
|
301 | + * |
|
302 | + * @param IQueryBuilder $query |
|
303 | + * @return Entity[] all fetched entities |
|
304 | + * @since 14.0.0 |
|
305 | + */ |
|
306 | + protected function findEntities(IQueryBuilder $query): array { |
|
307 | + $cursor = $query->execute(); |
|
308 | + |
|
309 | + $entities = []; |
|
310 | + |
|
311 | + while($row = $cursor->fetch()){ |
|
312 | + $entities[] = $this->mapRowToEntity($row); |
|
313 | + } |
|
314 | + |
|
315 | + $cursor->closeCursor(); |
|
316 | + |
|
317 | + return $entities; |
|
318 | + } |
|
319 | + |
|
320 | + |
|
321 | + /** |
|
322 | + * Returns an db result and throws exceptions when there are more or less |
|
323 | + * results |
|
324 | + * |
|
325 | + * @param IQueryBuilder $query |
|
326 | + * @throws DoesNotExistException if the item does not exist |
|
327 | + * @throws MultipleObjectsReturnedException if more than one item exist |
|
328 | + * @return Entity the entity |
|
329 | + * @since 14.0.0 |
|
330 | + */ |
|
331 | + protected function findEntity(IQueryBuilder $query): Entity { |
|
332 | + return $this->mapRowToEntity($this->findOneQuery($query)); |
|
333 | + } |
|
334 | 334 | |
335 | 335 | } |
@@ -58,13 +58,13 @@ discard block |
||
58 | 58 | * mapped to queries without using sql |
59 | 59 | * @since 14.0.0 |
60 | 60 | */ |
61 | - public function __construct(IDBConnection $db, string $tableName, string $entityClass=null) { |
|
61 | + public function __construct(IDBConnection $db, string $tableName, string $entityClass = null) { |
|
62 | 62 | $this->db = $db; |
63 | 63 | $this->tableName = $tableName; |
64 | 64 | |
65 | 65 | // if not given set the entity name to the class without the mapper part |
66 | 66 | // cache it here for later use since reflection is slow |
67 | - if($entityClass === null) { |
|
67 | + if ($entityClass === null) { |
|
68 | 68 | $this->entityClass = str_replace('Mapper', '', \get_class($this)); |
69 | 69 | } else { |
70 | 70 | $this->entityClass = $entityClass; |
@@ -117,9 +117,9 @@ discard block |
||
117 | 117 | $qb->insert($this->tableName); |
118 | 118 | |
119 | 119 | // build the fields |
120 | - foreach($properties as $property => $updated) { |
|
120 | + foreach ($properties as $property => $updated) { |
|
121 | 121 | $column = $entity->propertyToColumn($property); |
122 | - $getter = 'get' . ucfirst($property); |
|
122 | + $getter = 'get'.ucfirst($property); |
|
123 | 123 | $value = $entity->$getter(); |
124 | 124 | |
125 | 125 | $type = $this->getParameterTypeForProperty($entity, $property); |
@@ -128,9 +128,9 @@ discard block |
||
128 | 128 | |
129 | 129 | $qb->execute(); |
130 | 130 | |
131 | - if($entity->id === null) { |
|
131 | + if ($entity->id === null) { |
|
132 | 132 | // When autoincrement is used id is always an int |
133 | - $entity->setId((int)$qb->getLastInsertId()); |
|
133 | + $entity->setId((int) $qb->getLastInsertId()); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | return $entity; |
@@ -166,13 +166,13 @@ discard block |
||
166 | 166 | public function update(Entity $entity): Entity { |
167 | 167 | // if entity wasn't changed it makes no sense to run a db query |
168 | 168 | $properties = $entity->getUpdatedFields(); |
169 | - if(\count($properties) === 0) { |
|
169 | + if (\count($properties) === 0) { |
|
170 | 170 | return $entity; |
171 | 171 | } |
172 | 172 | |
173 | 173 | // entity needs an id |
174 | 174 | $id = $entity->getId(); |
175 | - if($id === null){ |
|
175 | + if ($id === null) { |
|
176 | 176 | throw new \InvalidArgumentException( |
177 | 177 | 'Entity which should be updated has no id'); |
178 | 178 | } |
@@ -186,9 +186,9 @@ discard block |
||
186 | 186 | $qb->update($this->tableName); |
187 | 187 | |
188 | 188 | // build the fields |
189 | - foreach($properties as $property => $updated) { |
|
189 | + foreach ($properties as $property => $updated) { |
|
190 | 190 | $column = $entity->propertyToColumn($property); |
191 | - $getter = 'get' . ucfirst($property); |
|
191 | + $getter = 'get'.ucfirst($property); |
|
192 | 192 | $value = $entity->$getter(); |
193 | 193 | |
194 | 194 | $type = $this->getParameterTypeForProperty($entity, $property); |
@@ -217,11 +217,11 @@ discard block |
||
217 | 217 | protected function getParameterTypeForProperty(Entity $entity, string $property): int { |
218 | 218 | $types = $entity->getFieldTypes(); |
219 | 219 | |
220 | - if(!isset($types[ $property ])) { |
|
220 | + if (!isset($types[$property])) { |
|
221 | 221 | return IQueryBuilder::PARAM_STR; |
222 | 222 | } |
223 | 223 | |
224 | - switch($types[ $property ]) { |
|
224 | + switch ($types[$property]) { |
|
225 | 225 | case 'int': |
226 | 226 | case 'integer': |
227 | 227 | return IQueryBuilder::PARAM_INT; |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | $cursor = $query->execute(); |
252 | 252 | |
253 | 253 | $row = $cursor->fetch(); |
254 | - if($row === false) { |
|
254 | + if ($row === false) { |
|
255 | 255 | $cursor->closeCursor(); |
256 | 256 | $msg = $this->buildDebugMessage( |
257 | 257 | 'Did expect one result but found none when executing', $query |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | |
262 | 262 | $row2 = $cursor->fetch(); |
263 | 263 | $cursor->closeCursor(); |
264 | - if($row2 !== false ) { |
|
264 | + if ($row2 !== false) { |
|
265 | 265 | $msg = $this->buildDebugMessage( |
266 | 266 | 'Did not expect more than one result when executing', $query |
267 | 267 | ); |
@@ -278,8 +278,8 @@ discard block |
||
278 | 278 | * @since 14.0.0 |
279 | 279 | */ |
280 | 280 | private function buildDebugMessage(string $msg, IQueryBuilder $sql): string { |
281 | - return $msg . |
|
282 | - ': query "' . $sql->getSQL() . '"; '; |
|
281 | + return $msg. |
|
282 | + ': query "'.$sql->getSQL().'"; '; |
|
283 | 283 | } |
284 | 284 | |
285 | 285 | |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | * @since 14.0.0 |
293 | 293 | */ |
294 | 294 | protected function mapRowToEntity(array $row): Entity { |
295 | - return \call_user_func($this->entityClass .'::fromRow', $row); |
|
295 | + return \call_user_func($this->entityClass.'::fromRow', $row); |
|
296 | 296 | } |
297 | 297 | |
298 | 298 | |
@@ -308,7 +308,7 @@ discard block |
||
308 | 308 | |
309 | 309 | $entities = []; |
310 | 310 | |
311 | - while($row = $cursor->fetch()){ |
|
311 | + while ($row = $cursor->fetch()) { |
|
312 | 312 | $entities[] = $this->mapRowToEntity($row); |
313 | 313 | } |
314 | 314 |
@@ -43,69 +43,69 @@ |
||
43 | 43 | abstract class Middleware { |
44 | 44 | |
45 | 45 | |
46 | - /** |
|
47 | - * This is being run in normal order before the controller is being |
|
48 | - * called which allows several modifications and checks |
|
49 | - * |
|
50 | - * @param Controller $controller the controller that is being called |
|
51 | - * @param string $methodName the name of the method that will be called on |
|
52 | - * the controller |
|
53 | - * @since 6.0.0 |
|
54 | - */ |
|
55 | - public function beforeController($controller, $methodName) { |
|
46 | + /** |
|
47 | + * This is being run in normal order before the controller is being |
|
48 | + * called which allows several modifications and checks |
|
49 | + * |
|
50 | + * @param Controller $controller the controller that is being called |
|
51 | + * @param string $methodName the name of the method that will be called on |
|
52 | + * the controller |
|
53 | + * @since 6.0.0 |
|
54 | + */ |
|
55 | + public function beforeController($controller, $methodName) { |
|
56 | 56 | |
57 | - } |
|
57 | + } |
|
58 | 58 | |
59 | 59 | |
60 | - /** |
|
61 | - * This is being run when either the beforeController method or the |
|
62 | - * controller method itself is throwing an exception. The middleware is |
|
63 | - * asked in reverse order to handle the exception and to return a response. |
|
64 | - * If the response is null, it is assumed that the exception could not be |
|
65 | - * handled and the error will be thrown again |
|
66 | - * |
|
67 | - * @param Controller $controller the controller that is being called |
|
68 | - * @param string $methodName the name of the method that will be called on |
|
69 | - * the controller |
|
70 | - * @param \Exception $exception the thrown exception |
|
71 | - * @throws \Exception the passed in exception if it can't handle it |
|
72 | - * @return Response a Response object in case that the exception was handled |
|
73 | - * @since 6.0.0 |
|
74 | - */ |
|
75 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
76 | - throw $exception; |
|
77 | - } |
|
60 | + /** |
|
61 | + * This is being run when either the beforeController method or the |
|
62 | + * controller method itself is throwing an exception. The middleware is |
|
63 | + * asked in reverse order to handle the exception and to return a response. |
|
64 | + * If the response is null, it is assumed that the exception could not be |
|
65 | + * handled and the error will be thrown again |
|
66 | + * |
|
67 | + * @param Controller $controller the controller that is being called |
|
68 | + * @param string $methodName the name of the method that will be called on |
|
69 | + * the controller |
|
70 | + * @param \Exception $exception the thrown exception |
|
71 | + * @throws \Exception the passed in exception if it can't handle it |
|
72 | + * @return Response a Response object in case that the exception was handled |
|
73 | + * @since 6.0.0 |
|
74 | + */ |
|
75 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
76 | + throw $exception; |
|
77 | + } |
|
78 | 78 | |
79 | 79 | |
80 | - /** |
|
81 | - * This is being run after a successful controllermethod call and allows |
|
82 | - * the manipulation of a Response object. The middleware is run in reverse order |
|
83 | - * |
|
84 | - * @param Controller $controller the controller that is being called |
|
85 | - * @param string $methodName the name of the method that will be called on |
|
86 | - * the controller |
|
87 | - * @param Response $response the generated response from the controller |
|
88 | - * @return Response a Response object |
|
89 | - * @since 6.0.0 |
|
90 | - */ |
|
91 | - public function afterController($controller, $methodName, Response $response) { |
|
92 | - return $response; |
|
93 | - } |
|
80 | + /** |
|
81 | + * This is being run after a successful controllermethod call and allows |
|
82 | + * the manipulation of a Response object. The middleware is run in reverse order |
|
83 | + * |
|
84 | + * @param Controller $controller the controller that is being called |
|
85 | + * @param string $methodName the name of the method that will be called on |
|
86 | + * the controller |
|
87 | + * @param Response $response the generated response from the controller |
|
88 | + * @return Response a Response object |
|
89 | + * @since 6.0.0 |
|
90 | + */ |
|
91 | + public function afterController($controller, $methodName, Response $response) { |
|
92 | + return $response; |
|
93 | + } |
|
94 | 94 | |
95 | 95 | |
96 | - /** |
|
97 | - * This is being run after the response object has been rendered and |
|
98 | - * allows the manipulation of the output. The middleware is run in reverse order |
|
99 | - * |
|
100 | - * @param Controller $controller the controller that is being called |
|
101 | - * @param string $methodName the name of the method that will be called on |
|
102 | - * the controller |
|
103 | - * @param string $output the generated output from a response |
|
104 | - * @return string the output that should be printed |
|
105 | - * @since 6.0.0 |
|
106 | - */ |
|
107 | - public function beforeOutput($controller, $methodName, $output) { |
|
108 | - return $output; |
|
109 | - } |
|
96 | + /** |
|
97 | + * This is being run after the response object has been rendered and |
|
98 | + * allows the manipulation of the output. The middleware is run in reverse order |
|
99 | + * |
|
100 | + * @param Controller $controller the controller that is being called |
|
101 | + * @param string $methodName the name of the method that will be called on |
|
102 | + * the controller |
|
103 | + * @param string $output the generated output from a response |
|
104 | + * @return string the output that should be printed |
|
105 | + * @since 6.0.0 |
|
106 | + */ |
|
107 | + public function beforeOutput($controller, $methodName, $output) { |
|
108 | + return $output; |
|
109 | + } |
|
110 | 110 | |
111 | 111 | } |