<?php

namespace app\common\arw\adjfut\src;

use ArrayAccess;

class ArrayFilter implements ArrayAccess
{
    /**
     * 数据
     *
     * @var array
     * @date 2023-01-13
     * @example
     * @author arw
     * @since 1.0.0
     */
    private $data = [];

    /**
     * 初始化
     *
     * @param array $data
     * @date 2023-01-13
     * @example
     * @author arw
     * @since 1.0.0
     */
    public function __construct(array $data)
    {
        $this->data = $data;
    }

    /**
     * 二维数组排除字段
     *
     * @param array $array
     * @param array $except
     * @return array
     * @date 2023-01-13
     * @example
     * @author arw
     * @since 1.0.0
     */
    public static function exceptFields(array $array, array $except): array
    {
        foreach ($array as &$value) {
            $self = new self($value);
            $value = $self->except($except)->toArray();
            unset($value, $self);
        }
        return $array;
    }

    /**
     * 二维数组指定字段
     *
     * @param array $array
     * @param array $only
     * @return array
     * @date 2023-01-13
     * @example
     * @author arw
     * @since 1.0.0
     */
    public static function onlyFields(array $array, array $only): array
    {
        foreach ($array as &$value) {
            $self = new self($value);
            $value = $self->only($only)->toArray();
            unset($value, $self);
        }
        return $array;
    }

    /**
     * 排除某些变量
     *
     * @param array $fields
     * @return self
     * @date 2023-01-13
     * @example
     * @author arw
     * @since 1.0.0
     */
    public function except(array $fields): self
    {
        $data = $this->data;
        // $fields = [id,name]
        foreach ($fields as $field) {
            if (isset($data[$field])) {
                unset($data[$field]);
            }
        }
        return new self($data);
    }

    /**
     * 获取部分变量
     *
     * @param array $fields
     * @return self
     * @date 2023-01-13
     * @example
     * @author arw
     * @since 1.0.0
     */
    public function only(array $fields): self
    {
        $data = $this->data;
        $result = [];
        // $fields = [id,name]
        // $fields = [id=>0,name=>asdasd]
        foreach ($fields as $key => $value) {
            if (is_int($key)) {
                if (isset($data[$value])) {
                    $result[$value] = $data[$value];
                }
            } else {
                $result[$key] = isset($data[$key]) ? $data[$key] : $value;
            }
        }
        return new self($result);
    }

    /**
     * 获取数据
     *
     * @return array
     * @date 2023-01-13
     * @example
     * @author arw
     * @since 1.0.0
     */
    public function toArray(): array
    {
        return $this->data;
    }


    public function __get($name)
    {
        return $this->offsetGet($name);
    }

    public function __set($name, $value)
    {
        return $this->offsetSet($name, $value);
    }

    public function offsetExists($offset): bool
    {
        return isset($this->data[$offset]);
    }

    public function offsetGet($offset)
    {
        return $this->data[$offset];
    }

    public function offsetSet($offset, $value): void
    {
        $this->data[$offset] = $value;
    }

    public function offsetUnset($offset): void
    {
        unset($this->data[$offset]);
    }
}
