@param Closure $filter
@param array $data
@param string $idField

@throws \WebComplete\microDb\Exception
172976
public function update(Closure $filter, array $data, string $idField = 'id')
    {
        unset($data[$idField]);
        $collectionData = $this->storage->load(true);
        if (!isset($collectionData['items'])) {
            $collectionData['items'] = [];
        }
        $items = &$collectionData['items'];
        foreach ($items as $k => $item) {
            if ($filter($item)) {
                $items[$k] = \array_merge($item, $data);
            }
        }
        $this->storage->save($collectionData);
    }
754445
public static function doInsert(array $options=array(), $instance=null, $IDField=null) {
		self::prepareQuery($options, $instance, $IDField);
		return self::$instances[$instance]->insert($options);
	}
1225209
public function getCurrentIndex($idfield = 'id')
    {
        // TODO: think more to optimize this method
        if (is_array($this->data)) {
            return array_search(current($this->data), $this->data);
        }

        // else it is dsql dataset...
        return $this->current_row[$idfield];
    }
1179190
protected function registerClosureFilter(Closure $filter)
	{
		$this->getRouter()->filter($name = spl_object_hash($filter), $filter);

		return $name;
	}
376900
protected function _validateData($rule, $key, $data)
    {
        if (
            (is_callable($rule) && call_user_func_array($rule, [$key, $data, $this]))
            || @preg_match($rule, $data)
        ) {
            return true;
        }

        if ($rule instanceof Closure) {
            $reflection = new ReflectionFunction($rule);
            $rule       = $reflection->__toString();
        }

        $this->_errorsList[] = [
            'message'   => 'validation_mismatch',
            'key'       => $key,
            'data'      => $data,
            'rule'      => $rule,
        ];
        $this->_hasErrors = true;

        return false;
    }
1228360
public static function validateInteger($data, callable $filter=null) {
        if ( is_int($data) === false ) return false;
        return self::applyFilter($data, $filter);
    }
725645
public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
    {
        if (!\is_callable($this->getOption('callback'))) {
            throw new \RuntimeException(sprintf('Please provide a valid callback for option "callback" and field "%s"', $this->getName()));
        }

        $this->active = true === \call_user_func($this->getOption('callback'), $proxyQuery, $alias, $field, $data);
    }
897689
public function findObject($identifier, $data, $filter)
    {
        if ($objects = $this->findObjects($identifier, $data, $filter)) {
            
            if ($objects->exists()) {
                return $objects->first();
            }
            
        }
    }
862281
public static function extractRawData($objectData, $filter)
    {
        $filteredData = self::filterData($objectData, array($filter));

        //====================================================================//
        // Explode List Field Id
        $isList = self::isListField($filter);

        //====================================================================//
        // Simple Single Field
        if (!$isList) {
            if (isset($filteredData[$filter])) {
                return $filteredData[$filter];
            }

            //====================================================================//
        // List Field
        } else {
            //====================================================================//
            // Check List Exists
            if (!isset($filteredData[self::listName($filter)])) {
                return null;
            }

            //====================================================================//
            // Parse Raw List Data
            $result = array();
            foreach ($filteredData[self::listName($filter)] as $key => $item) {
                $result[$key] = $item[self::fieldName($filter)];
            }

            return $result;
        }

        //====================================================================//
        // Field Not Received or is Empty
        return null;
    }
198708
public function replaceData(array $data, $filter = null)
	{
		$this->clearData();

		return $this->set($data, $filter);
	}
974754
protected function loop($data, Closure $callable)
    {
        if (!is_array($data) && !is_object($data)) {
            return;
        }

        foreach ($data as $key => $value) {
            call_user_func_array($callable, [$key, $value]);
        }
    }
332102
protected function buildFilter($field, $type, $filter, $value)
    {
        if ((is_array($value) && count($value) != 2)
                || (!is_array($value) && ($value !== false && strlen($value) == 0))) {
            return false;
        }
        if (strlen($field) == 0)
            throw new \Exception("Empty 'field'");
        if (strlen($type) == 0)
            throw new \Exception("Empty 'type'");

        if ($type == Table::TYPE_DATETIME) {
            if ($filter == Table::FILTER_BETWEEN
                    && is_array($value) && count($value) == 2) {
                $value = [
                    $value[0] ? new MongoDate($value[0]) : null,
                    $value[1] ? new MongoDate($value[1]) : null,
                ];
            } else if ($filter != Table::FILTER_BETWEEN
                    && is_scalar($value)) {
                $value = new MongoDate($value);
            } else {
                return false;
            }
        } else {
            if ($filter == Table::FILTER_BETWEEN) {
                if (!is_array($value) || count($value) != 2)
                    return false;
            } else if (!is_scalar($value)) {
                return false;
            }
        }

        switch ($filter) {
            case Table::FILTER_LIKE:
                if (!(isset($this->andOps[$field])))
                    $this->andOps[$field] = [];
                $this->andOps[$field][] = [
                    'operator'  => 'equals',
                    'value'     => new MongoRegex('/.*' . preg_quote($value) . '.*/i'),
                ];
                break;
            case Table::FILTER_EQUAL:
                if (!(isset($this->andOps[$field])))
                    $this->andOps[$field] = [];
                $this->andOps[$field][] = [
                    'operator'  => 'equals',
                    'value'     => $value,
                ];
                break;
            case Table::FILTER_BETWEEN:
                if (!(isset($this->andOps[$field])))
                    $this->andOps[$field] = [];
                $this->andOps[$field][] = [
                    'operator'  => 'range',
                    'value1'    => $value[0],
                    'value2'    => $value[1],
                ];
                break;
            case Table::FILTER_NULL:
                if (!(isset($this->andOps[$field])))
                    $this->andOps[$field] = [];
                $this->andOps[$field][] = [
                    'operator'  => 'exists',
                    'value'     => false,
                ];
                break;
            default:
                throw new \Exception("Unknown filter: $filter");
        }

        return true;
    }
847823
public function load($field, $sourceUrl, $idField = 'id', $textField = 'text')
    {
        if (Str::contains($field, '.')) {
            $field = $this->formatName($field);
            $class = str_replace(['[', ']'], '_', $field);
        } else {
            $class = $field;
        }

        $script = <<<EOT

$(document).on('change', "{$this->getElementClassSelector()}", function () {
    var target = $(this).closest('.fields-group').find(".$class");
    $.get("$sourceUrl?q="+this.value, function (data) {
        target.find("option").remove();
        $(target).select2({
            data: $.map(data, function (d) {
                d.id = d.$idField;
                d.text = d.$textField;
                return d;
            })
        }).trigger('change');
    });
});
EOT;

        Admin::script($script);

        return $this;
    }
278584
public function filter(\Closure $fn)
    {
        $data = array_filter($this->data, $fn);
        $records = $this->db->init($this->table, array(), $this->isNew);
        $records->data = $data;
        $records->isColl = true;
        $records->loaded = $this->loaded;
        return $records;
    }
887611
function toArray(\Closure $filter = null, $recursive = false)
    {
        $arr = array();
        foreach($this->getIterator() as $key => $val)
        {
            if ($recursive && ( $val instanceof \Traversable || is_array($val) ) ) {
                // (A) At the end value can be empty by filter
                ## deep convert
                $val = StdTravers::of($val)->toArray($filter, $recursive);
            }

            // (B) So Filter Check Placed After Recursive Calls
            $flag = false;
            if ($filter !== null)
                $flag = $filter($val, $key);

            if ($flag) continue;


            if (! \Poirot\Std\isStringify($key) )
                ## some poirot Traversable is able to handle objects as key
                $key = \Poirot\Std\flatten($key);

            $arr[(string) $key] = $val;
        }

        return $arr;
    }
35668
public function find(array $filter = [], array $orderBy = [], $limit = 0,
        $offset = 0, array $associate = []
    ) {
        try {

            $query = $this->query()
                ->select()
                ->associate($associate)
                ->setFilter($filter);

            foreach ($orderBy as $orderByRule) {
                $query->orderBy($orderByRule[0], $orderByRule[1]);
            }

            return $query->limit($limit)->offset($offset)->run($this->connection);
        } catch (Exception\QueryException $e) {
            throw new Exception\RepositoryException($e->getMessage());
        }
    }
1001605
public function ajax($resourceUrl, $idField = 'id', $textField = 'text')
    {
        $configs = array_merge([
            'allowClear'         => true,
            'placeholder'        => trans('admin.choose'),
            'minimumInputLength' => 1,
        ], $this->config);

        $configs = json_encode($configs);
        $configs = substr($configs, 1, strlen($configs) - 2);

        $this->script = <<<EOT

$(".{$this->getElementClass()}").select2({
  ajax: {
    url: "$resourceUrl",
    dataType: 'json',
    delay: 250,
    data: function (params) {
      return {
        q: params.term,
        page: params.page
      };
    },
    processResults: function (data, params) {
      params.page = params.page || 1;

      return {
        results: $.map(data.data, function (d) {
                   d.id = d.$idField;
                   d.text = d.$textField;
                   return d;
                }),
        pagination: {
          more: data.next_page_url
        }
      };
    },
    cache: true
  },
  $configs,
  escapeMarkup: function (markup) {
      return markup;
  }
});

EOT;
    }