-
Notifications
You must be signed in to change notification settings - Fork 16
/
XBRL-SEC-JSON-Package.php
325 lines (279 loc) · 7.56 KB
/
XBRL-SEC-JSON-Package.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* XBRL SEC JSON Package handler
*
* @author Bill Seddon
* @version 0.9
* @Copyright (C) 2018 Lyquidity Solutions Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Implements functions to read a taxonomy package
*/
class XBRL_SEC_JSON_Package extends XBRL_Package
{
/**
* Notes about using this package instance
* @var string
*/
const notes = <<<EOT
The SEC implementations use the namespace of the schema document to create a path to use in the cache.
The namespace format appears to be <domain>/<period> where <period> is the value of the EDGAR period.
A path is not used in the instance document <schemaRef> so the instance document expects to be in the
same folder as the taxonomy. It is likely that a mapped URL will need to be used so the correct
schema can be accessed from the cache.\n\n
EOT;
/**
* The EDGAR file number
* @var string
*/
public $fileNumber;
/**
* The EDGAR period
* @var string
*/
public $period;
/**
* The EDGAR accession number
* @var string
*/
public $accessionNumber;
/**
* The EDGAR publication date
* @var string
*/
public $pubDate;
/**
* The EDGAR acceptance Datetime
* @var string
*/
public $acceptanceDatetime;
/**
* The EDGAR compay name
* @var string
*/
public $companyName;
/**
* The EDGAR filing date
* @var string
*/
public $filingDate;
/**
* The EDGAR form type
* @var string
*/
public $formType;
/**
* The EDGAR fiscal year end
* @var string
*/
public $fiscalYearEnd;
/**
* The EDGAR company address
* zip
* state
* street1
* phone
* city
* @var string
*/
public $address;
/**
* The EDGAR package description
* @var string
*/
public $description;
/**
* The EDGAR assigned SIC
* @var string
*/
public $assignedSic;
/**
* The EDGAR cik number
* @var string
*/
public $cikNumber;
/**
* The list of package files
* @var string[]
*/
public $files = array();
/**
* Default constructor
* @param ZipArchive $zipArchive
*/
public function __construct( ZipArchive $zipArchive )
{
parent::__construct( $zipArchive );
}
/**
* Returns true if the zip file represents an SEC package
* {@inheritDoc}
* @see XBRL_Package::isPackage()
*/
public function isPackage()
{
// A SEC JSON package has a file called <ciknumber>-manifest.xml
try
{
$this->traverseContents( function( $path, $name, $type )
{
if ( $type == PATHINFO_DIRNAME ) return true;
$extension = pathinfo( $name, PATHINFO_EXTENSION );
if ( $extension != 'json' ) return true;
$this->metaFilename = $name;
return false;
} );
if ( empty( $this->metaFilename ) )
{
throw XBRL_TaxonomyPackageException::withError( "tpe:metadataFileNotFound", "The package does not contain a .json file" );
}
return $this->isValidMetaFile();
}
catch ( Exception $ex )
{
$code = $ex instanceof XBRL_TaxonomyPackageException ? $ex->error : $ex->getCode();
$this->errors[ $code ] = $ex->getMessage();
}
return false;
}
/**
* Returns the name of the class implementing the XBRL instance implied by this taxonomy
* return string
*/
public function getXBRLClassname()
{
return "XBRL_US_GAAP_2015";
}
/**
* Workout which file is the schema file
* @return void
* @throws "tpe:schemaFileNotFound"
*/
protected function determineSchemaFile()
{
if ( is_string( $this->schemaFile ) && ! empty( $this->schemaFile ) ) return;
$schemaFileList = array_filter( $this->files, function( $item ) {
return XBRL::endsWith( $item, '.xsd' );
} );
if ( count( $schemaFileList ) != 1 )
{
throw XBRL_TaxonomyPackageException::withError( "tpe:schemaFileNotFound", "The package does not contain just one schema (.xsd) file" );
}
$this->schemaFile = reset( $schemaFileList );
$content = $this->getFile( $this->schemaFile );
$this->schemaNamespace = $this->getTargetNamespace( $this->schemaFile, $content );
// The schema file should always be a fully qualified URL
$part = parse_url( $this->schemaFile, PHP_URL_SCHEME );
$prefix = empty( $part ) ? $this->schemaNamespace . "/" : "";
$this->schemaFile = "$prefix{$this->schemaFile}";
}
/**
* Return the contents of a file given a path
* @param string $path
* @return string
* @throws Exception if the requested file does not exist
*/
public function getFile( $path )
{
return parent::getFile( $this->getActualUri( $path ) );
}
/**
* Returns a localized version of the schema file path
* @param string $uri
* @return string
*/
public function getActualUri( $uri )
{
return basename( $uri );
}
/**
* Check the meta file is valid
*/
private function isValidMetaFile()
{
if ( $this->metaFile ) return true;
try
{
$content = $this->getFile( $this->metaFilename );
// SEC seems to use UCS-2 LE
$json = mb_convert_encoding( $content, 'UTF-8', 'UTF-16' );
$this->metaFile = json_decode( $json, true );
foreach ( $this->metaFile as $name => $value )
{
if ( ! property_exists( $this, $name ) ) continue;
if ( $name == 'filingDate' )
{
if ( preg_match( "!Date\((?'timestamp'.*?)\)!", $value, $matches ) )
{
$value = date( "y-m-d", $matches['timestamp'] );
}
}
$this->$name = $value;
}
$this->determineSchemaFile();
}
catch ( XBRL_TaxonomyPackageException $ex )
{
throw $ex;
}
catch ( Exception $ex )
{
throw XBRL_TaxonomyPackageException::withError( "tpe:invalidMetaDataFile", $ex->getMessage() );
}
return true;
}
/**
* Save the taxonomy from the package to the cache location
* @param string $cacheLocation
* @return boolean
*/
public function saveTaxonomy( $cacheLocation )
{
// Initialize the context
$context = XBRL_Global::getInstance();
$context->useCache = true;
$context->cacheLocation = $cacheLocation;
$context->initializeCache();
$this->determineSchemaFile();
// Find the schema document
$content = trim( $this->getFile( $this->schemaFile ) );
$result = $this->processSchemaDocument( $context, $content, false );
$this->setUrlMap();
if ( ! $result )
{
$this->errors[] = "Unable to process the schema document";
return false;
}
// Look at the entry points and remap them to their location in the zip file
foreach ( $this->files as $index => $file )
{
$extension = pathinfo( $file, PATHINFO_EXTENSION );
if ( ! in_array( $extension, array( 'xsd', 'xml' ) ) ) continue;
$content = $this->getFile( $file );
if ( $file != $this->schemaFile )
{
$xml = simplexml_load_string( $content );
if ( $xml->getName() == 'xbrl' )
{
$this->instanceDocument = $file;
}
}
$context->saveCacheFile( "{$this->schemaNamespace}/$file", $content );
}
return true;
}
}