@@ -13,10 +13,18 @@ trait ApiListTrait
1313{
1414 use MutationTrait;
1515
16- const API_COMBO_FIELD = '__combo ' ;
17- const API_ORDER_FIELD = '__order ' ;
18- const API_PAGE_FIELD = '__page ' ;
19- const API_LIMIT_FIELD = '__limit ' ;
16+ /**
17+ * Contract: provided by ApiTrait.
18+ */
19+ abstract protected function prepareQuery ();
20+
21+ /**
22+ * Contract: provided by ApiTrait.
23+ *
24+ * @param ModelCriteria $query
25+ * @return mixed
26+ */
27+ abstract protected function checkReturnFields (ModelCriteria &$ query );
2028
2129 /**
2230 * @var array
@@ -41,7 +49,7 @@ protected function hydrateOrders()
4149 if (count ($ this ->query )) {
4250 Logger::log (static ::class . ' gathering query string ' , LOG_DEBUG );
4351 foreach ($ this ->query as $ key => $ value ) {
44- if ($ key === self :: API_ORDER_FIELD ) {
52+ if ($ key === $ this -> apiOrderField () ) {
4553 $ orders = json_decode ($ value , true );
4654 foreach ($ orders as $ field => $ direction ) {
4755 $ this ->order ->addOrder ($ field , $ direction );
@@ -57,8 +65,8 @@ protected function hydrateOrders()
5765 protected function extractPagination ()
5866 {
5967 Logger::log (static ::class . ' extract pagination start ' , LOG_DEBUG );
60- $ page = array_key_exists (self :: API_PAGE_FIELD , $ this ->query ) ? $ this ->query [self :: API_PAGE_FIELD ] : 1 ;
61- $ limit = array_key_exists (self :: API_LIMIT_FIELD , $ this ->query ) ? $ this ->query [self :: API_LIMIT_FIELD ] : 100 ;
68+ $ page = array_key_exists ($ this -> apiPageField () , $ this ->query ) ? $ this ->query [$ this -> apiPageField () ] : 1 ;
69+ $ limit = array_key_exists ($ this -> apiLimitField () , $ this ->query ) ? $ this ->query [$ this -> apiLimitField () ] : 100 ;
6270 Logger::log (static ::class . ' extract pagination end ' , LOG_DEBUG );
6371 return array ($ page , (int )$ limit );
6472 }
@@ -100,7 +108,7 @@ protected function addFilters(ModelCriteria &$query)
100108 if (!empty ($ this ->query )) {
101109 $ tableMap = $ this ->getTableMap ();
102110 foreach ($ this ->query as $ field => $ value ) {
103- if (self :: API_COMBO_FIELD === $ field ) {
111+ if ($ this -> apiComboField () === $ field ) {
104112 ApiHelper::composerComboField ($ tableMap , $ query , $ this ->extraColumns , $ value );
105113 } elseif (!preg_match ('/^__/ ' , $ field )) {
106114 ApiHelper::addModelField ($ tableMap , $ query , $ field , $ value );
@@ -129,4 +137,24 @@ protected function paginate()
129137 }
130138 }
131139
140+ private function apiComboField (): string
141+ {
142+ return '__combo ' ;
143+ }
144+
145+ private function apiOrderField (): string
146+ {
147+ return '__order ' ;
148+ }
149+
150+ private function apiPageField (): string
151+ {
152+ return '__page ' ;
153+ }
154+
155+ private function apiLimitField (): string
156+ {
157+ return '__limit ' ;
158+ }
159+
132160}
0 commit comments