ModuleWithItemsFromConfig.php
1.83 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
<?php
namespace WPML\PB\SiteOrigin\Modules;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
class ModuleWithItemsFromConfig extends ModuleWithItems {
/** @var array $fieldDefinitions */
private $fieldDefinitions = [];
/** @var string $itemsField */
private $itemsField;
/**
* @param string $itemsField
* @param array $config
*/
public function __construct( $itemsField, array $config ) {
$this->itemsField = $itemsField;
$this->init( $config );
}
private function init( array $config ) {
$keyByField = Fns::converge( Lst::zipObj(), [ Lst::pluck( 'field' ), Fns::identity() ] );
$this->fieldDefinitions = $keyByField( $config );
}
private function getFieldData( $field, $key ) {
return Obj::path( [ $field, $key ], $this->fieldDefinitions );
}
/**
* @inheritDoc
*/
public function get_title( $field ) {
return $this->getFieldData( $field, 'type' );
}
/**
* @inheritDoc
*/
public function get_fields() {
return array_keys( $this->fieldDefinitions );
}
/**
* @inheritDoc
*/
public function get_editor_type( $field ) {
return $this->getFieldData( $field, 'editor_type' );
}
/**
* @inheritDoc
*/
public function get_items_field() {
return $this->itemsField;
}
/**
* @param array $settings
*
* @return array
*/
public function get_items( $settings ) {
$path = $this->get_items_field();
$pathInFlatField = explode( '>', $path );
$items = Obj::path( $pathInFlatField, $settings );
if ( is_null( $items ) ) {
list( $parent, $path ) = explode( '>', $path, 2 );
$settings = $settings[ $parent ];
$items = [];
foreach ( $settings as $x => $setting ) {
foreach ( $setting[ $path ] as $y => $item ) {
$items = Obj::assocPath( [ $x . '>' . $y ], $item, $items );
}
}
}
return $items;
}
}