bueltge/wp-rest-api-filter-items

View on GitHub
inc/Core/Filter.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php # -*- coding: utf-8 -*-

namespace RestApiFilterItems\Core;

class Filter implements FilterInterface {

    /**
     * @type string
     */
    private $request;

    /**
     * @type array
     */
    private $data;

    /**
     * Set var
     *
     * @param array $request
     * @param array $data
     */
    public function __construct( $request, array $data ) {

        $this->request = $request;
        $this->data    = $data;
    }

    /**
     * Filter data to get attributes items
     *
     * @return array
     */
    public function filter_data() {

        if ( empty( $this->request ) ) {
            return $this->data;
        }

        $items = explode( ',', $this->request );
        if ( 0 === count( $items ) ) {
            return $this->data;
        }

        $filtered_data = array();
        foreach ( $this->data as $key => $value ) {

            if ( in_array( $key, $items, TRUE ) ) {
                $filtered_data[ $key ] = $value;
            }
        }

        return $filtered_data;
    }
}