-
Notifications
You must be signed in to change notification settings - Fork 16
/
DKDBAPackage.php
200 lines (165 loc) · 5.99 KB
/
DKDBAPackage.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace lyquidity\dfb;
/**
* This is a DBA specific package handler for the years before 2016.
* Since 2016 the packages have followed the XBRL package specification.
*/
class DKDBAPackage extends \XBRL_SimplePackage
{
/**
* Notes about using this package instance
* @var string
*/
const notes = <<<EOT
This is a simple package implementation for Danish Business Authority taxonomies before 2016. Since 2016
taxonomy packages have followed the XBRL taxonomy specification. This package is hardcoded to use a prefix
of: http://archprod.service.eogs.dk/taxonomy\n\n
EOT;
const prefix = 'http://archprod.service.eogs.dk/taxonomy';
/**
* Returns an array of schema file names defined as entry points
*/
public function getSchemaEntryPoints()
{
$root = $this->contents[ $this->getFirstFolderName() ];
$yearName = key( $root );
$year = $root[ $yearName ];
$entryPoints = array_filter( $year , function( $item ) { return ! is_array( $item ) && strpos( $item, 'entry' ) === 0; } );
$entryPoints = array_map( function( $item ) use( $yearName ) { return join( '/', array( $this::prefix, $yearName, $item ) ); }, $entryPoints );
return $entryPoints;
}
/**
* Return the url for the 'all' entry point
*/
public function getAllEntryPoint()
{
$entryPoints = $this->getSchemaEntryPoints();
$alls = array_filter( $entryPoints, function( $entryPoint ) { return strpos( $entryPoint, "entryAll" ) !== false; } );
return $alls ? reset( $alls ) : false;
}
/**
* Overridden to make sure this class is not use unless the package DOES NOT have a META-INF folder
* {@inheritDoc}
* @see \XBRL_SimplePackage::isPackage()
*/
public function isPackage()
{
if ( isset( $this->contents[ $this->getFirstFolderName() ][ \XBRL_TaxonomyPackage::metaFolderName ] ) ) return false;
$found = false;
// Look for the full_ifrs folder
$this->traverseContents( function( $path, $name, $type ) use( &$found )
{
if ( $type != PATHINFO_DIRNAME ) return true;
$found = $name == 'full_ifrs';
return ! $found;
} );
if ( $found ) return false;
return parent::isPackage();
}
protected function getActualUri( $uri )
{
return $this->getFirstFolderName() . str_replace( self::prefix, '', $uri );
}
/**
* Workout which file is the schema file
* @return void
* @throws "tpe:schemaFileNotFound"
*/
protected function determineSchemaFile()
{
if ( $this->schemaFile && $this->schemaNamespace ) return;
$schemaFilesList = $this->getSchemaEntryPoints();
if ( count( $schemaFilesList ) == 0 )
{
throw \XBRL_TaxonomyPackageException::withError( "tpe:schemaFileNotFound", "The package does not contain any entry schema (.xsd) files" );
}
foreach ( $schemaFilesList as $schemaFile )
{
$actualUri = $this->getActualUri( $schemaFile );
$content = $this->getFile( $actualUri );
if ( $content )
{
$this->schemaNamespace = $this->getTargetNamespace( $schemaFile, $content );
$this->schemaFile = $schemaFile;
$this->setUrlMap();
}
}
}
/**
* Save the taxonomy from the package to the cache location
* @param string $cacheLocation
* @return boolean
*/
public function saveTaxonomy( $cacheLocation )
{
$getCommonRootFolder = function( $actualUri, $uri )
{
$uriParts = array_reverse( explode( "/", $uri ) );
$actualUriParts = array_reverse( explode( "/", $actualUri ) );
$count = min( array( count( $uriParts ), count( $actualUriParts ) ) );
for( $i = 0; $i < $count; $i++ )
{
if ( $uriParts[ $i ] != $actualUriParts[ $i ] ) break;
}
if ( $uriParts[ $i ] === '' && isset( $uriParts[ $i + 1 ] ) && preg_match_all( "/https?:/", $uriParts[ $i + 1 ] ) )
{
$i--;
}
return array(
// 'actual' => implode( "/", array_reverse( array_slice( $actualUriParts, $i -1 ) ) ),
// 'uri' => implode( "/", array_reverse( array_slice( $uriParts, $i -1 ) ) )
'actual' => implode( "/", array_reverse( array_slice( $actualUriParts, $i ) ) ),
'uri' => implode( "/", array_reverse( array_slice( $uriParts, $i ) ) )
);
};
// Initialize the context
$context = \XBRL_Global::getInstance();
$context->useCache = true;
$context->cacheLocation = $cacheLocation;
$context->initializeCache();
$this->determineSchemaFile();
if ( $context->findCachedFile( $this->schemaFile ) )
{
$this->errors[] = "The schema file '{$this->schemaFile}' already exists in the cache.";
return false;
}
// Look at the entry points and remap them to their location in the zip file
foreach ( $this->getSchemaEntryPoints() as $index => $uri )
{
// Is there a rewrite?
$actualUri = $this->getActualUri( $uri );
// Should call XBRL_Package::getCommonRootFolder()
$commonRootFolder = $getCommonRootFolder( $actualUri, $uri );
// Handle the relative case
// $folder = $this->getElementForPath( dirname( $actualUri ) );
$folder = $this->getElementForPath( $commonRootFolder['actual'] );
// Create a copy of all files and folder in the cache location
$copy = function( $folder, $path, $uri ) use( &$copy, &$context )
{
foreach ( $folder as $index => $item )
{
// if $index is numeric then $item is a file
// if ( is_numeric( $index ) && $index < 2000) // The index might be 2015 or 20161001
if ( ! is_array( $item ) )
{
$content = $this->getFile( "$path/$item" );
if ( ! $content ) continue;
if ( $context->findCachedFile( "$uri/$item" ) ) continue;
$context->saveCacheFile( "$uri/$item", $content );
}
else
{
if ( $index == "META-INF" ) continue;
// Handle any directories
$newUri = "$uri/$index";
$newPath = "$path/$index";
$copy( $item, $newPath, $newUri );
}
}
};
// $copy( $folder, dirname( $actualUri ), dirname( $uri ) );
$copy( $folder, $commonRootFolder['actual'], $commonRootFolder['uri'] );
}
return true;
}
}