ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
Sorter.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
26 
33 class Sorter
34 {
35  protected const SORT_ASC = SORT_ASC;
36  protected const SORT_DESC = SORT_DESC;
37 
38  protected int $sort_direction = self::SORT_ASC;
39  private bool $sort_and_save = false;
43 
44  public function __construct(
45  ResourceBuilder $resource_builder,
46  CollectionBuilder $collection_builder,
47  ResourceCollection $collection
48  ) {
49  $this->resource_builder = $resource_builder;
50  $this->collection_builder = $collection_builder;
51  $this->collection = $collection;
52  }
53 
54  public function andSave(): self
55  {
56  $this->sort_and_save = true;
57  return $this;
58  }
59 
60  public function asc(): self
61  {
62  $this->sort_direction = self::SORT_ASC;
63  return $this;
64  }
65 
66  public function desc(): self
67  {
68  $this->sort_direction = self::SORT_DESC;
69  return $this;
70  }
71 
72 
73  public function byTitle(): ResourceCollection
74  {
75  return $this->custom(new ByTitle($this->resource_builder, $this->sort_direction));
76  }
77 
78  public function bySize(): ResourceCollection
79  {
80  return $this->custom(new BySize($this->resource_builder, $this->sort_direction));
81  }
82 
83 
85  {
86  return $this->custom(new ByCreationDate($this->resource_builder, $this->sort_direction));
87  }
88 
89  public function custom(CollectionSorter $sorter): ResourceCollection
90  {
91  $collection = $sorter->sort($this->collection);
92  if ($this->sort_and_save) {
93  $this->collection_builder->store($collection);
94  }
95  return $collection;
96  }
97 }
custom(CollectionSorter $sorter)
Definition: Sorter.php:89
__construct(ResourceBuilder $resource_builder, CollectionBuilder $collection_builder, ResourceCollection $collection)
Definition: Sorter.php:44