This repository was archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathArrayMapperFactory.php
More file actions
62 lines (54 loc) · 1.97 KB
/
ArrayMapperFactory.php
File metadata and controls
62 lines (54 loc) · 1.97 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
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
*/
namespace StatusLib;
use DomainException;
use Zend\Config\Writer\PhpArray as ConfigWriter;
use ZF\Configuration\ConfigResource;
/**
* Service factory for the ArrayMapper
*
* Requires the Config service in the service locator, and a
* statuslib.array_mapper_path subkey within the configuration that points
* to a valid filesystem path of a PHP file that will return an array.
*
* Passes the data from the file, the path to the file, and a PhpArray config
* writer to a ZF\Configuration\ConfigResource instance, and passes the data
* and the ConfigResource instance to the ArrayMapper.
*/
class ArrayMapperFactory
{
public function __invoke($services)
{
if (! $services->has('config')) {
throw new DomainException('Cannot create StatusLib\ArrayMapper; missing config dependency');
}
$config = $services->get('config');
if (! isset($config['statuslib']['array_mapper_path'])) {
throw new DomainException(sprintf(
'Cannot create %s; missing statuslib.array_mapper_path configuration',
ArrayMapper::class
));
}
$path = $config['statuslib']['array_mapper_path'];
if (! file_exists($path)) {
throw new DomainException(sprintf(
'Cannot create %s; path "%s" does not exist',
ArrayMapper::class,
$path
));
}
$data = include $path;
if (! is_array($data)) {
throw new DomainException(sprintf(
'Cannot create %s; file "%s" does not return an array',
ArrayMapper::class,
$path
));
}
$configResource = new ConfigResource($data, realpath($path), new ConfigWriter());
return new ArrayMapper($data, $configResource);
}
}