1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symplify\PHP7_Sculpin\Contrib\Taxonomy; |
13
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderInterface; |
15
|
|
|
use Symplify\PHP7_Sculpin\Core\DataProvider\DataProviderManager; |
16
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents; |
17
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent; |
18
|
|
|
use Symplify\PHP7_Sculpin\Core\Sculpin; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
20
|
|
|
|
21
|
|
|
final class ProxySourceTaxonomyDataProvider implements DataProviderInterface, EventSubscriberInterface |
22
|
|
|
{ |
23
|
|
|
private $taxons = []; |
24
|
|
|
private $dataProviderManager; |
25
|
|
|
private $dataProviderName; |
26
|
|
|
private $taxonomyKey; |
27
|
|
|
|
28
|
|
|
public function __construct( |
29
|
|
|
DataProviderManager $dataProviderManager, |
30
|
|
|
string $dataProviderName, |
31
|
|
|
string $taxonomyKey |
32
|
|
|
) { |
33
|
|
|
$this->dataProviderManager = $dataProviderManager; |
34
|
|
|
$this->dataProviderName = $dataProviderName; |
35
|
|
|
$this->taxonomyKey = $taxonomyKey; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function provideData() : array |
39
|
|
|
{ |
40
|
|
|
return $this->taxons; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function getSubscribedEvents() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
SculpinEvents::EVENT_BEFORE_RUN => 'beforeRun', |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function beforeRun(SourceSetEvent $sourceSetEvent) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
$taxons = []; |
53
|
|
|
$dataProvider = $this->dataProviderManager->dataProvider($this->dataProviderName); |
54
|
|
|
|
55
|
|
|
foreach ($dataProvider->provideData() as $item) { |
56
|
|
|
if ($itemTaxons = $item->data()->get($this->taxonomyKey)) { |
57
|
|
|
$normalizedItemTaxons = []; |
58
|
|
|
foreach ((array) $itemTaxons as $itemTaxon) { |
59
|
|
|
$normalizedItemTaxon = trim($itemTaxon); |
60
|
|
|
$taxons[$normalizedItemTaxon][] = $item; |
61
|
|
|
$normalizedItemTaxons[] = $normalizedItemTaxon; |
62
|
|
|
} |
63
|
|
|
$item->data()->set($this->taxonomyKey, $normalizedItemTaxons); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->taxons = $taxons; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.