Skip to content

Commit

Permalink
improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmcarvalho committed Feb 22, 2024
1 parent 653610f commit 06cedd0
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/Support/SelectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

class SelectCollection extends Collection
{
protected static string $keyField = 'key';
protected static string $valueField = 'value';
protected static string $extraField = 'extra';

public static function primeVueFormat(): void
{
self::$keyField = 'code';
self::$valueField = 'name';
}

protected static function getArrayType(array $items): array
{
$hasArrayValue = null;
Expand All @@ -20,15 +30,15 @@ protected static function getArrayType(array $items): array
throw new \InvalidArgumentException('Consistency problem. All items value must have same structure.');
};
foreach ($items as $value) {
if (($hasArrayValue === true && empty($value['value'])) || $hasArrayValue === false && ! empty($value['value'])) {
if (($hasArrayValue === true && empty($value[self::$valueField])) || $hasArrayValue === false && ! empty($value[self::$valueField])) {
$failed();
}
$hasArrayValue = ! empty($value['value']);
$hasArrayValue = ! empty($value[$value]);

if (($hasArrayExtra === true && ! isset($value['extra'])) || $hasArrayExtra === false && isset($value['extra'])) {
if (($hasArrayExtra === true && ! isset($value[self::$valueField])) || $hasArrayExtra === false && isset($value[self::$extraField])) {
$failed();
}
$hasArrayExtra = isset($value['extra']);
$hasArrayExtra = isset($value[self::$extraField]);

if (! is_bool($value) && ! is_numeric($value) && ! is_string($value)) {
$differentTypes = true;
Expand All @@ -50,11 +60,11 @@ public static function fromArray(array $items): SelectCollection
$collection = new SelectCollection();
if ($arrayType['hasArrayValue']) {
foreach ($items as $key => $value) {
$collection->push(['key' => strval($key), 'value' => strval($value['value']), 'extra' => $value['extra']]);
$collection->push([self::$keyField => strval($key), self::$valueField => strval($value[self::$valueField]), self::$extraField => $value[self::$extraField]]);
}
} else {
foreach ($items as $key => $value) {
$collection->push(['key' => strval($key), 'value' => strval($value)]);
$collection->push([self::$keyField => strval($key), self::$valueField => strval($value)]);
}
}

Expand Down Expand Up @@ -101,8 +111,8 @@ public static function fromPluck(
$key = empty($key) ? $index : (is_object($item) ? $item->{$key} : $item[$key]);

return [$key => [
'value' => is_object($item) ? $item->{$value} : $item[$value],
'extra' => is_object($item) ? $item->{$extra} : $item[$extra],
self::$keyField => is_object($item) ? $item->{$value} : $item[$value],
self::$extraField => is_object($item) ? $item->{$extra} : $item[$extra],
]];
});
$collection = self::fromArray($items->toArray());
Expand All @@ -113,22 +123,22 @@ public static function fromPluck(

public function sortByKey(int $options = null, $descending = false): SelectCollection
{
return $this->sortChooser('key', $options, $descending);
return $this->sortChooser(self::$keyField, $options, $descending)->values();
}

public function sortByKeyDesc(int $options = null): SelectCollection
{
return $this->sortByKey($options, true);
return $this->sortByKey($options, true)->values();
}

public function sortByValue(int $options = null, $descending = false): SelectCollection
{
return $this->sortChooser('value', $options, $descending);
return $this->sortChooser(self::$valueField, $options, $descending)->values();
}

public function sortByValueDesc(int $options = null): SelectCollection
{
return $this->sortByValue($options, true);
return $this->sortByValue($options, true)->values();
}

protected function sortChooser(string $field, ?int $options, $descending): SelectCollection
Expand Down

0 comments on commit 06cedd0

Please sign in to comment.