M
M
magary42016-03-03 18:33:03
symfony
magary4, 2016-03-03 18:33:03

How to make "in a smart way" processing of request parameters and their transformation?

controller:

foreach( $request as $k => $v ) {
            if( in_array( $k, ['limit', 'page' ] ) ) {
                continue;
            }
            if( is_array( $v ) ) {
                if( isset($v['type']) ) {
                    switch($v['type']) {
                        case "parent":
                            $criterias[] = new Criterion\ParentLocationId($v['val']);
                            break;
                    }
                } if( isset( $v['like'] ) ) {
                    switch( $v['like'] ) {
                        case 0:
                            $criterias[] = new Criterion\FullText( $v['val'] );
                            break;
                        case 1:
                            $criterias[] = new Criterion\Field( $v['name'], Criterion\Operator::LIKE, $v['val'] . "%" );
                            break;
                        case 2:
                            $criterias[] = new Criterion\Field( $v['name'], Criterion\Operator::LIKE, "%" . $v['val'] . "%" );
                            break;
                        case 3:
                            $criterias[] = new Criterion\Field( $v['name'], Criterion\Operator::EQ, $v['val'] );
                            break;
                    }
                } else {
                    $criterias[] = new Criterion\Field( $k, Criterion\Operator::IN, $v );
                }
            } else {
                $criterias[] = new Criterion\Field( $k, Criterion\Operator::EQ, $v);
            }
        }
        if( count( $criterias ) > 0 ) {
            $query->filter = new Criterion\LogicalAnd( $criterias );
        }

this "noodle" checks each request
value if the text type value adds
$criterias[] = new Criterion\Field( $k, Criterion\Operator::EQ, $v);
those. comparison
if simple array then
$criterias[] = new Criterion\Field( $k, Criterion\Operator::IN, $v );
if the array has a type key then
$criterias[] = new Criterion\ParentLocationId($v['val']);
further this noodle will only grow, as I understand it, it is necessary to switch to objects;
further, it is necessary to provide for another programmer to define his type in his other bundle
switch($v['type']) {
            case "parent":
                  $criterias[] = new Criterion\ParentLocationId($v['val']);

and your custom class of Criterion type that needs to be applied in the incoming request parameter
in OOP is not very strong, tell me how to do it correctly

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question