forked from bshiluk/rest-api-data-localizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.php
77 lines (58 loc) · 1.4 KB
/
store.php
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
<?php
namespace RADL;
use RADL\Store\Key\Value;
class Store
{
/**
* Handle for script where Store will be localized
* @var string
*/
public $script_handle;
/**
* Single state tree for all requested data - initialized from $schema
* @var array
*/
private $state;
/**
* Will be name of Store variable in script
* @var string
*/
public $name;
public function __construct( $name, $script_handle, array $schema )
{
$this->name = $name;
$this->script_handle = $script_handle;
$this->state = $schema;
}
public function get( $key_path, $args )
{
$value = $this->key_value( $key_path );
if ( $value instanceof Value ) {
$value = $value->get( $args );
}
return $value;
}
public function rendered()
{
$this->render_values();
return $this->state;
}
private function key_value( $key_path )
{
$value = $this->state;
foreach ( explode( '.', $key_path ) as $key ) {
if ( $key !== '' ) {
$value = $value[$key];
}
}
return $value;
}
private function render_values()
{
array_walk_recursive( $this->state, function ( &$item ) {
if ( $item instanceof Value ) {
$item = $item->render();
}
} );
}
}