-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathDKIFRSPackage.php
247 lines (203 loc) · 7.17 KB
/
DKIFRSPackage.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
namespace lyquidity\dfb;
/**
* This is a package handler for DK IFRS Packages.
*/
class DKIFRSPackage extends \XBRL_SimplePackage
{
/**
* Notes about using this package instance
* @var string
*/
const notes = <<<EOT
This is a simple package implementation for Danish IFRS taxonomies.
DK IFRS do not follow the XBRL Taxonomy Packaging 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';
/**
* A reference to the root of the original contents
* This package will use a sub-element as the contents
* @var array
*/
private $originalContents = null;
/**
* The path from the original contents to the new contents
* @var string
*/
private $contentsPath = "";
/**
* The version of this package
* @var string
*/
private $version = "";
/**
* Returns an array of schema file names defined as entry points
*/
public function getSchemaEntryPoints()
{
$root = $this->contents[ $this->getFirstFolderName() ];
$yearName = key( $root );
$ifrs = $root[ $yearName ]['ifrs'];
$ifrsName = "$yearName/ifrs";
$entryPoints = array_filter( $ifrs, function( $item ) { return ! is_array( $item ) && strpos( $item, 'entry' ) === 0; } );
$entryPoints = array_map( function( $item ) use( $ifrsName ) { return join( '/', array( $this::prefix, $ifrsName, $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_dk folder
$this->traverseContents( function( $path, $name, $type ) use( &$found )
{
if ( $type != PATHINFO_DIRNAME ) return true;
$found = $name == 'full_ifrs_dk';
return ! $found;
} );
if ( ! $found ) return false;
if ( ! parent::isPackage() ) return false;
// Find the version number folder and make the contents start at this folder
$this->traverseContents( function( $path, $name, $type ) use( &$contents, &$found )
{
if ( $type != PATHINFO_DIRNAME ) return true;
$found = strlen( $name ) == 8 && is_numeric( $name );
if ( ! $found ) return true;
$this->contentsPath = $path;
$this->version = $name;
return ! $found;
} );
if ( ! $found ) return false;
$this->originalContents &= $this->contents;
$parts = array_filter( explode( "/", $this->contentsPath ) );
if ( count( $parts ) > 1 )
{
$this->contents =& $this->contents[ $this->getFirstFolderName() ];
}
array_pop( $parts );
$this->contentsPath = implode( "/", $parts );
if ( $this->contentsPath ) $this->contentsPath .= "/";
return true;
}
protected function getActualUri( $uri )
{
return $this->contentsPath . $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( substr( $commonRootFolder['actual'], strlen( $this->contentsPath ) ) );
// 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;
}
}