-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAPIResource.php
More file actions
executable file
·156 lines (125 loc) · 4.01 KB
/
APIResource.php
File metadata and controls
executable file
·156 lines (125 loc) · 4.01 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
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
<?php
class NFe_APIResource extends NFe_Object {
private static $_apiRequester = null;
public static function convertClassToObjectType() {
$object_type = str_replace('NFe_', '', get_called_class());
$object_type = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $object_type));
return strtolower($object_type);
}
public static function objectBaseURI() {
$object_type = self::convertClassToObjectType();
switch($object_type) { // Add Exceptions as needed
case 'company':
return 'companies';
case 'legal_person':
return 'legalPeople';
case 'natural_person':
return 'naturalPeople';
case 'service_invoice':
return 'serviceInvoices';
case 'webhook':
return 'webhooks';
default:
return $object_type . 's';
}
}
public static function API() {
if ( NFe_APIResource::$_apiRequester == null ) {
NFe_APIResource::$_apiRequester = new NFe_APIRequest();
}
return NFe_APIResource::$_apiRequester;
}
public static function endpointAPI( $object = null, $uri_path = '' ) {
$path = '';
if ( is_string($object) || is_integer($object) ) {
$path = '/' . $object;
}
elseif (is_array($object) && isset($object['company_id'])) {
$uri_path = '/companies/' . $object['company_id'];
}
elseif (is_object($object) && isset($object->provider) && isset($object->provider->id) ) {
$uri_path = '/companies/' . $object->provider->id;
}
if (isset($object['id'])) {
$path = '/' . $object['id'];
}
return strtolower( NFe_io::getBaseURI() . $uri_path . '/' . self::objectBaseURI() . $path );
}
public static function url( $object = null ) {
return self::endpointAPI( $object );
}
protected static function createFromResponse($response) {
return NFe_Utilities::createFromResponse( self::convertClassToObjectType(), $response );
}
protected static function createAPI( $attributes = array() ) {
return self::createFromResponse(
self::API()->request( 'POST', self::endpointAPI( $attributes ), $attributes ) );
}
protected function deleteAPI() {
// if ( $this['id'] == null ) { // $this['id']
// return false;
// }
try {
$response = self::API()->request( 'DELETE', static::url($this) );
if ( isset($response->errors) ) {
throw NFeException();
}
}
catch (Exception $e) {
return false;
}
return true;
}
protected static function searchAPI( $options = array() ) {
try {
$response = self::API()->request( 'GET', static::url($options), $options );
return self::createFromResponse($response);
}
catch (Exception $e) {}
return array();
}
protected static function fetchAPI($key) {
try {
$response = self::API()->request( 'GET', static::url($key) );
return self::createFromResponse($response);
}
catch ( NFeObjectNotFound $e ) {
throw new NFeObjectNotFound( self::convertClassToObjectType( get_called_class() ) . ':' . ' não encontrado.');
}
}
protected function refreshAPI() {
if ( $this->is_new() ) {
return false;
}
try {
$response = self::API()->request( 'GET', static::url($this) );
if ( isset($response->errors) ) {
throw NFeObjectNotFound();
}
$type = self::objectBaseURI();
$new_object = self::createFromResponse($response->$type);
$this->copy( $new_object );
$this->resetStates();
}
catch (Exception $e) {
return false;
}
return true;
}
protected function saveAPI() {
try {
$response = self::API()->request( $this->is_new() ? 'POST' : 'PUT', static::url($this), $this->getAttributes() );
$type = self::objectBaseURI();
$new_object = self::createFromResponse($response->$type);
$this->copy( $new_object );
$this->resetStates();
if ( isset($response->errors) ) {
throw new NFeException();
}
}
catch (Exception $e) {
return false;
}
return true;
}
}