-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcodes.R
More file actions
311 lines (280 loc) · 9.47 KB
/
codes.R
File metadata and controls
311 lines (280 loc) · 9.47 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
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
# Helper function
is_numeric <- function(x){
is.numeric(tryCatch(as.numeric(x), warning = function(w){FALSE}))
}
#' Create the number for _sumario_
#'
#' Creates the id for a _sumario_ by the date it was published.
#' @param date Date of the _sumario_
#' @inheritParams retrieve_sumario
#' @return A character vector
#' @seealso The id is different from the CVE, which can be created with [sumario_cve()].
#' @family code generators
#' @export
#' @importFrom methods is
#' @examples
#' sumario_nbo(Sys.Date())
#' sumario_nbo(format(as.Date("2009/01/01", "%Y/%m/%d"), "%Y%m%d"), journal = "BORME")
sumario_nbo <- function(date, journal = "BOE") {
if (is(date, "Date")) {
date <- format(date, "%Y%m%d")
}
check_date(date)
journal <- match.arg(journal, c("BOE", "BORME"))
paste(journal, "S", date, sep = "-")
}
#' @describeIn sumario_nbo For compatibility with previous version
#' @export
sumario_xml <- function(date) {
if (is(date, "Date")) {
date <- format(date, "%Y%m%d")
}
check_date(date)
date
}
#' Create the number of the _sumario_
#'
#' @param year Character or numeric value of the year of the summary in YYYY format.
#' @param number Number of the summary in NNN format.
#' @inheritParams retrieve_sumario
#' @return A character vector with the CVE of the _sumario_.
#' @seealso [sumario_nbo()] if you want to retrieve the _sumario_ by date and don't know the CVE.
#' @family code generators
#' @export
#' @examples
#' sumario_cve(2019, 242)
sumario_cve <- function(year, number, journal = "BOE") {
# There are some sumarios from before 2009
if (as.numeric(number) < 0 || as.numeric(number) > 1000) {
stop("The number should be the in numeric format above 1.",
call. = FALSE)
}
journal <- match.arg(journal, c("BOE", "BORME"))
code <- paste(journal, "S", year, number, sep = "-")
check_code(code)
code
}
#' Elements: _disposición_ and _anuncio_
#'
#' Functions to create CVE codes for the documents published on the BOE.
#' @name element
#' @param year Character or numeric value of the year of the element in YYYY format.
#' @param number Character or numeric value of the element.
#' @return A character vector
#' @family code generators
NULL
elemento <- function(item = c("B", "A"), year, number) {
item <- match.arg(item, c("B", "A"))
if (nchar(year) != 4 && !is_numeric(year)) {
stop("The year should be in numeric YYYY format", call. = FALSE)
}
if (nchar(number) > 3 && !is_numeric(number)) {
stop("The number should be in numeric XXX format", call. = FALSE)
}
paste("BOE", item, year, number, sep = "-")
}
#' @describeIn element Create the CVE of the diposicion.
#' @export
#' @examples
#' disposicion_cve(2019, 242)
disposicion_cve <- function(year, number) {
elemento(item = "A", year = year, number = number)
}
#' @describeIn element For compatibility with previous version
#' @export
disposicion <- disposicion_cve
#' @describeIn element Create the CVE of the anuncio.
#' @export
#' @examples
#' anuncio_cve(2019, 242)
anuncio_cve <- function(year, number) {
elemento(item = "B", year = year, number = number)
}
#' @describeIn sumario_cve For compatibility with previous version
#' @export
sumario <- sumario_cve
#' @describeIn element For compatibility with previous version
#' @export
anuncio <- anuncio_cve
#' Check id of documents
#'
#' Given an id check if it is valid.
#' @param id ID or CVE of the document (character).
#' @return A logical value if correct, errors if something is not right.
#' @export
#' @examples
#' check_code("BOE-S-20141006") # Normal way
#' check_code("BOE-S-2014-242") # Also accepted but not documented
#' # Will fail:
#' # check_code("BOE-S-2014")
#' valid_code("BOE-S-2014")
check_code <- function(id) {
if (length(id) > 1) {
stop("This function is not vectorized. Check the code one by one.",
call. = FALSE)
}
ids <- unlist(strsplit(id, "-", fixed = TRUE), FALSE, FALSE)
if (!ids[1] %in% c("BORME", "BOE")) {
stop("Journal does not match: got ", ids[1],
" should be either BORME or BOE.", call. = FALSE)
}
if (ids[1] == "BOE" && length(ids) > 4) {
stop("The code should have at most 3 '-' got more.", call. = FALSE)
}
if (ids[1] == "BOE" && !ids[[2]] %in% c("B", "A", "S")) {
stop("The type of document does not match: got ", ids[2],
" should be either A, B or S.", call. = FALSE)
}
if (ids[1] == "BORME" && !ids[[2]] %in% c("B", "A", "C", "S")) {
stop("The type of document does not match: got ", ids[2],
" should be either A, B, C or S.", call. = FALSE)
}
# BORME-S-2017-188 and BORME-S-20171002 are equivalent
if (length(ids) < 4L && ids[[2]] != "S") {
stop("The code should have three 3 '-'.", call. = FALSE)
}
if (length(ids) < 4L && ids[[2]] == "S" && nchar(ids[[3]]) < 8L) {
stop("The last number should be a date of format YYYYMMDD.",
call. = FALSE)
}
if (length(ids) < 4L && ids[[2]] == "S" && nchar(ids[[3]]) >= 8L) {
check_date(ids[3])
}
if (!is_numeric(paste0(ids[3L:length(ids)], collapse = ""))) {
stop("After journal and type of document there only should be numbers.",
call. = FALSE)
}
# if (ids[2] == "S" && nchar(ids[3]) > 4) {
# stop("Got ", ids[3], " should be a year in numerical form.",
# call. = FALSE)
# }
if (ids[[2]] == "A" && ids[[1]] == "BORME" && length(ids) > 5L) {
stop("Got ", ids[[3]], " should be a numerical year.", call. = FALSE)
}
TRUE
}
#' @param sumario Logical value if sumarios should be accepted.
#' @param BORME Logical value if BORME journal should be accepted.
#' @export
#' @describeIn check_code Returns logic values
valid_code <- function(id, sumario = TRUE, BORME = FALSE) {
stopifnot(is.character(id))
stopifnot(is_logical(sumario))
stopifnot(is_logical(BORME))
if (length(id) > 1L) {
stop("This function is not vectorized. Check the code one by one.",
call. = FALSE)
}
ids <- unlist(strsplit(id, "-", fixed = TRUE), FALSE, FALSE)
type <- if (BORME) "BORME" else "BOE"
if (!ids[[1]] %in% type) {
return(FALSE)
}
if (ids[[1]] == "BOE" && length(ids) > 4L) {
stop("The code should have at most 3 '-' got more.", call. = FALSE)
}
if (type == "BOE" && !ids[[2]] %in% c("B", "A", "S")) {
return(FALSE)
}
if (type == "BORME" && !ids[[2]] %in% c("B", "A", "C", "S")) {
return(FALSE)
}
if (ids[[2]] == "S" && sumario) {
return(FALSE)
}
# BORME-S-2017-188 and BORME-S-20171002 are equivalent
if (length(ids) < 4 && ids[[2]] != "S") {
return(FALSE)
}
if (length(ids) < 4L && ids[[2]] == "S" && nchar(ids[[3]]) < 8L) {
return(FALSE)
}
if (length(ids) < 4L && ids[[2]] == "S" && nchar(ids[[3]]) >= 8L) {
return(valid_date(ids[[3]]))
}
if (!is_numeric(paste0(ids[3:length(ids)], collapse = ""))) {
return(FALSE)
}
# if (ids[2] == "S" && nchar(ids[3]) > 4) {
# stop("Got ", ids[3], " should be a year in numerical form.",
# call. = FALSE)
# }
if (ids[[2]] == "A" && ids[[1]] == "BORME" && length(ids) > 5L) {
return(FALSE)
}
TRUE
}
check_date <- function(x) {
if (is.numeric(x)) {
x <- as.character(x)
}
if (!is.na(as.Date(x, format = "%Y%m%d"))) {
return(TRUE)
}
y <- strsplit(x, "")[[1]]
month <- as.numeric(paste0(y[5L:6L], collapse = ""))
day <- as.numeric(paste0(y[7L:8L], collapse = ""))
if (month >= 13) {
stop("The month is greater than 12.", call. = FALSE)
}
if (day >= 31L) {
stop("The day is greater than 31.", call. = FALSE)
}
stop("That date does not exists, check the day of the month.",
call. = FALSE)
}
valid_date <- function(date) {
if (is.numeric(date)) {
date <- as.character(date)
}
if (!is.na(as.Date(date, format = "%Y%m%d"))) {
return(TRUE)
}
y <- strsplit(date, "", fixed = TRUE)[[1]]
month <- as.numeric(paste0(y[5:6], collapse = ""))
day <- as.numeric(paste0(y[7:8], collapse = ""))
if (month >= 13) {
return(FALSE)
}
if (day >= 31) {
return(FALSE)
}
return(FALSE)
}
#' Supplementary summaries
#'
#' Creates the CVE of a summary of the supplements, either the judicial or notifications.
#' These are only available for 3 months.
#' @inheritParams suplemento_cve
#'
#' @returns A CVE of the document
#' @export
#' @examples
#' sumario_suplementos(2023, 1)
sumario_suplementos <- function(year, number, type = "N") {
type <- match.arg(type, c("J", "N"))
if (as.numeric(number) < 0 || as.numeric(number) > 1000) {
stop("The number should be the in numeric format above 1.",
call. = FALSE)
}
p <- paste("BOE", "S", year, "N", sep = "-")
paste0(p, number)
}
#' Supplement CVE
#'
#' Creates the CVE of a supplement, either the judicial or notifications.
#' These are only available for 3 months.
#' @inheritParams sumario_cve
#' @param type Either J or N. J for **judicial** or N for **notificaciones**
#' @returns A CVE.
#' @export
#' @examples
#' suplemento_cve(number = 1)
suplemento_cve <- function(year = 2023, number, type = "J") {
type <- match.arg(type, c("J", "N"))
if (as.numeric(number) < 0) {
stop("The number should be the in numeric format above 1.",
call. = FALSE)
}
paste("BOE", type, year, number, sep = "-")
}