-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSortedLoader.php
More file actions
125 lines (111 loc) · 3.58 KB
/
Copy pathSortedLoader.php
File metadata and controls
125 lines (111 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
* TechDivision\Import\Loaders\SortedFilesystemLoader
*
* PHP version 7
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2020 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
*/
namespace TechDivision\Import\Loaders;
use TechDivision\Import\Loaders\Sorters\UasortImpl;
use TechDivision\Import\Loaders\Sorters\SorterImplInterface;
/**
* Generic loader implementation that uses a glob compatible pattern
* to load files from a given directory.
*
* The loader uses the PHP function `uasort` to sort the files, so take
* into account, that the keys of the array will be kept after sorting.
*
* @author Tim Wagner <t.wagner@techdivision.com>
* @copyright 2020 TechDivision GmbH <info@techdivision.com>
* @license https://opensource.org/licenses/MIT
* @link https://github.com/techdivision/import
* @link http://www.techdivision.com
* @link https://www.php.net/uasort
*/
class SortedLoader implements SortedLoaderInterface
{
/**
* The parent loader instance.
*
* @var \TechDivision\Import\Loaders\LoaderInterface
*/
private $loader;
/**
* The sorter instance to use.
*
* @var \TechDivision\Import\Loaders\Sorters\SorterImplInterface
*/
private $sorterImpl;
/**
* Construct that initializes the loader with the parent loader instance.
*
* @param \TechDivision\Import\Loaders\LoaderInterface $loader The parent loader instance
* @param \TechDivision\Import\Loaders\Sorters\SorterImplInterface|null $sorterImpl The sorter instance to use
*/
public function __construct(LoaderInterface $loader, ?SorterImplInterface $sorterImpl = null)
{
$this->loader = $loader;
$this->sorterImpl = $sorterImpl ?? new UasortImpl();
}
/**
* Add's the passed sorter to the loader instance.
*
* @param callable $sorter The sorter to add
*
* @return void
*/
public function addSorter(callable $sorter) : void
{
$this->getSorterImpl()->addSorter($sorter);
}
/**
* Return's the array with the sorter callbacks.
*
* @return callable[] The sorter callbacks
*/
public function getSorters() : array
{
return $this->getSorterImpl()->getSorters();
}
/**
* Loads, sorts and returns the files by using the passed glob pattern.
*
* If no pattern will be passed to the `load()` method, the files of
* the actual directory using `getcwd()` will be returned.
*
* @param string|null $pattern The pattern to load the files from the filesystem
*
* @return \ArrayAccess The array with the data
*/
public function load(?string $pattern = null) : \ArrayAccess
{
$data = $this->getLoader()->load($pattern);
// sort the files loaded by the parent loader instance
$this->getSorterImpl()->sort($data);
// return the sorted files
return $data;
}
/**
* Return's the sorter instance to use.
*
* @return \TechDivision\Import\Loaders\Sorters\SorterImplInterface The sorter instance to use
*/
protected function getSorterImpl() : SorterImplInterface
{
return $this->sorterImpl;
}
/**
* Return's the parent loader instance.
*
* @return \TechDivision\Import\Loaders\LoaderInterface The parent loader instance
*/
protected function getLoader() : LoaderInterface
{
return $this->loader;
}
}