Skip to content

Commit

Permalink
add new features 'addKeyUseItem,modify,addKeys,removeKeys,sortByKeys'
Browse files Browse the repository at this point in the history
  • Loading branch information
BMTmohammedtaha committed Nov 25, 2023
1 parent a6d437a commit edf86a7
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 213 deletions.
207 changes: 0 additions & 207 deletions src/Contracts/DataRules.php

This file was deleted.

45 changes: 45 additions & 0 deletions src/Contracts/DataRulesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Effectra\DataOptimizer\Contracts;

use Closure;

/**
* interface DataRulesInterface
*
Expand Down Expand Up @@ -210,4 +212,47 @@ public function renameKey(string $key, string $new_name): self;
* @return self Returns the current instance of the class.
*/
public function stripTags(string $key, $allowed_tags = null): self;

/**
* Modify a rule value using a custom closure.
*
* @param string $key The rule key.
* @param Closure $p The closure to modify the value.
* @return self Returns the current instance of the class.
*/
public function modify(string $key, Closure $p): self;

/**
* Remove specified keys from the data.
*
* @param array $keys An array of keys to remove.
* @return self Returns the current instance of the class.
*/
public function removeKeys(array $keys): self;

/**
* Add new keys to the data.
*
* @param array $keys An array of keys to add.
* @return self Returns the current instance of the class.
* @throws DataValidatorException If keys are not an associative array.
*/
public function addKeys(array $keys): self;

/**
* Add a new key using a callback function.
*
* @param string $key The key to add.
* @param Closure $callback The callback function to generate the value for the new key.
* @return self Returns the current instance of the class.
*/
public function addKeyUseItem(string $key, Closure $callback): self;

/**
* Sort the data based on specified keys.
*
* @param array $sortKeys The keys to use for sorting.
* @return self Returns the current instance of the class.
*/
public function sortByKeys(array $sortKeys): self;
}
56 changes: 52 additions & 4 deletions src/DataOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public function applyRule(string $rule, string $key, $value): Result

$removed = null;

if ($rule === 'rename') {
$removed = $key;
$key = $this->data_rule->getAttribute('new_key_name_' . $key);
if ($rule === 'modify_' . $key) {
$callback = $this->data_rule->getAttribute('callback_function_' . $key);
$value = call_user_func($callback, $key, $value);
}

return new Result($key, $value, $removed);
Expand Down Expand Up @@ -154,12 +154,60 @@ public function optimize(DataRulesInterface $rules): array
unset($item[$result->getRemoved()]);
}
}
if ($this->data_rule->hasAttribute('rename_' . $key)) {
$item[$this->data_rule->getAttribute('new_key_name_' . $key)] = $value;
unset($item[$this->data_rule->getAttribute('rename_' . $key)]);
}
}

if ($this->data_rule->hasAttribute('add_keys')) { {
foreach ($this->data_rule->getAttribute('add_keys') as $k => $v) {
$item[$k] = $v;
}
}
}

$data[] = $item;
for ($i = 1; $i < $this->data_rule->getIncrement() + 1; $i++) {
if ($this->data_rule->hasAttribute('add_keys_by_using_item_' . $i)) {
$v = call_user_func($this->data_rule->getAttribute('add_keys_by_using_item_callback_' . $i), $item);
$item[$this->data_rule->getAttribute('add_keys_by_using_item_' . $i)] = $v;
}
}

if ($this->data_rule->hasAttribute('remove_keys')) {
foreach ($this->data_rule->getAttribute('remove_keys') as $k) {
unset($item[$k]);
}
}
if ($this->data_rule->hasAttribute('sort')) {
$data[] = $this->sortByKeys($item, $this->data_rule->getAttribute('sort'));
} else {
$data[] = $item;
}
}
}

return $data;
}

/**
* sort keys of array using list of keys
* @param array $array array you want sort
* @param array $keysToSortBy list of keys
* @return array sorted array
*/
private function sortByKeys(array $array, array $keysToSortBy): array
{
$keyPositions = array_flip($keysToSortBy);

// Sort the array based on key positions
uksort($array, function ($key1, $key2) use ($keyPositions) {
$position1 = $keyPositions[$key1] ?? PHP_INT_MAX;
$position2 = $keyPositions[$key2] ?? PHP_INT_MAX;

return $position1 - $position2;
});

return $array;
}
}
Loading

0 comments on commit edf86a7

Please sign in to comment.