-
Notifications
You must be signed in to change notification settings - Fork 9
Allowing list files that contains a list of object (e.g. CSV) #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Tom32i
wants to merge
1
commit into
master
Choose a base branch
from
content-lists
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the "StenopePHP/Stenope" bundle. | ||
| * | ||
| * @author Thomas Jarrand <thomas.jarrand@gmail.com> | ||
| */ | ||
|
|
||
| namespace Stenope\Bundle; | ||
|
|
||
| use Stenope\Bundle\Exception\RuntimeException; | ||
|
|
||
| class ContentBag | ||
| { | ||
| private array $contents = []; | ||
| private bool $locked = false; | ||
| private string $type; | ||
|
|
||
| /** @var callable Sorting function */ | ||
| private $sorter = null; | ||
|
|
||
| /** @var callable Filter function */ | ||
| private $filter = null; | ||
|
|
||
| public function __construct(string $type, ?callable $sorter = null, ?callable $filter = null) | ||
| { | ||
| $this->type = $type; | ||
| $this->sorter = $sorter; | ||
| $this->filter = $filter; | ||
| } | ||
|
|
||
| public function add(string $identifier, object $object): void | ||
| { | ||
| if ($this->locked) { | ||
| throw new RuntimeException('Contents have already been filtered and sorted.', 1); | ||
| } | ||
|
|
||
| if (isset($this->contents[$identifier])) { | ||
| throw new RuntimeException(sprintf( | ||
| 'Found multiple contents of type "%s" with the same "%s" identifier.', | ||
| $this->type, | ||
| $identifier | ||
| )); | ||
| } | ||
|
|
||
| $this->contents[$identifier] = $object; | ||
| } | ||
|
|
||
| public function getContents(): array | ||
| { | ||
| try { | ||
| $this->applyFilter(); | ||
| } catch (\Throwable $exception) { | ||
| throw new RuntimeException(sprintf('There was a problem filtering %s.', $this->type), 0, $exception); | ||
| } | ||
|
|
||
| try { | ||
| $this->applySort(); | ||
| } catch (\Throwable $exception) { | ||
| throw new RuntimeException(sprintf('There was a problem sorting %s.', $this->type), 0, $exception); | ||
| } | ||
|
|
||
| $this->locked = true; | ||
|
|
||
| return $this->contents; | ||
| } | ||
|
|
||
| private function applyFilter(): void | ||
| { | ||
| if ($this->filter === null) { | ||
| return; | ||
| } | ||
|
|
||
| $this->contents = array_filter($this->contents, $this->filter); | ||
| } | ||
|
|
||
| private function applySort(): void | ||
| { | ||
| if ($this->sorter === null) { | ||
| return; | ||
| } | ||
|
|
||
| set_error_handler(static function (int $severity, string $message, ?string $file, ?int $line): void { | ||
| throw new \ErrorException($message, $severity, $severity, $file, $line); | ||
| }); | ||
|
|
||
| uasort($this->contents, $this->sorter); | ||
|
|
||
| restore_error_handler(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit anecdotic for the use-case but still: the second arg of trim is a list of chars to remove, not a string to remove.
Something like
trimSuffixshould be used instead: https://symfony.com/doc/current/components/string.html#methods-to-pad-and-trim