<commit_before><?php

declare(strict_types=1);

namespace ListSorter;

use Illuminate\Http\Request;

class ListSorter extends AbstractListSorter
{
    const DEFAULT_SORT_BY_KEY = 'by';
    const DEFAULT_SORT_DIR_KEY = 'dir';

    private $sortByKey;
    private $sortDirKey;
    private $defaultSortBy;
    private $defaultSortDir;

    /**
     * @param array $sortableItems
     *
     * @throws \Exception
     */
    public function setSortableItems(array $sortableItems)
    {
        // validate aliases
        $this->validateSortableItems($sortableItems);

        foreach ($sortableItems as $key => $sortableItem) {
            if (!$sortableItem instanceof SortableItem) {
                $sortableItems[$key] = new SortableItem($sortableItem);
            }
        }

        $this->sortableItems = $sortableItems;
    }

    /**
     * @param array $sortableItems
     *
     * @return bool
     * @throws \Exception
     */
    private function validateSortableItems(array $sortableItems)
    {
        if (empty($sortableItems)) {
            throw new \Exception('Sortable items can not be empty');
        }

        if ($this->areKeysUnique($this->extractKeys($sortableItems)) !== true) {
            throw new \Exception('Sortable item alias must be unique');
        }

        return true;
    }

    /**
     * @param array $sortableItems
     *
     * @return array
     */
    private function extractKeys(array $sortableItems)
    {
        $keys = [];
        foreach ($sortableItems as $sortableItem) {
            if (!$sortableItem instanceof SortableItem) {
                $keys[] = $sortableItem;
                continue;
            }

            $keys[] = $sortableItem->getKey();
        }

        return $keys;
    }

    /**
     * @param array $aliases
     *
     * @return bool
     */
    private function areKeysUnique(array $aliases)
    {
        if (count($aliases) === count(array_unique($aliases))) {
            return true;
        }

        return false;
    }

    /**
     * @return string
     */
    public function getSortByKey()
    {
        if (!isset($this->sortByKey)) {
            return self::DEFAULT_SORT_BY_KEY;
        }

        return $this->sortByKey;
    }

    /**
     * @param string $sortByKey
     */
    public function setSortByKey($sortByKey)
    {
        $this->sortByKey = $sortByKey;
    }
<commit_msg>Add abstract class<commit_after><?php

declare(strict_types=1);

namespace ListSorter;

use Illuminate\Http\Request;

class ListSorter extends AbstractListSorter
{
    const DEFAULT_SORT_BY_KEY = 'by';
    const DEFAULT_SORT_DIR_KEY = 'dir';

    private $sortByKey;
    private $sortDirKey;
    private $defaultSortBy;
    private $defaultSortDir;

    /**
     * @return string
     */
    public function getSortByKey()
    {
        if (!isset($this->sortByKey)) {
            return self::DEFAULT_SORT_BY_KEY;
        }

        return $this->sortByKey;
    }

    /**
     * @param string $sortByKey
     */
    public function setSortByKey($sortByKey)
    {
        $this->sortByKey = $sortByKey;
    }

{'fraction_non_alphanumeric': 0.11362169141381537, 'fraction_numerical': 0.0006455777921239509, 'mean_word_length': 2.074404761904762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '98225', 'n_tokens_mistral': 938, 'n_tokens_neox': 922, 'n_words': 189}