File Coverage

blib/lib/STIX/Object.pm
Criterion Covered Total %
statement 91 95 95.7
branch 26 30 86.6
condition 13 21 61.9
subroutine 21 22 95.4
pod 5 6 83.3
total 156 174 89.6


line stmt bran cond sub pod time code
1             package STIX::Object;
2              
3 26     26   16576 use 5.010001;
  26         112  
4 26     26   157 use strict;
  26         49  
  26         703  
5 26     26   124 use warnings;
  26         44  
  26         1468  
6 26     26   152 use utf8;
  26         50  
  26         225  
7              
8 26     26   1504 use overload '""' => \&to_string, fallback => 1;
  26         90  
  26         302  
9              
10 26     26   2480 use Carp;
  26         51  
  26         2410  
11 26     26   27250 use Cpanel::JSON::XS;
  26         115103  
  26         2153  
12 26     26   11920 use STIX::Schema;
  26         184  
  26         2058  
13 26     26   227 use Types::Standard qw(Str);
  26         87  
  26         630  
14 26     26   105894 use UUID::Tiny qw(:std);
  26         198271  
  26         6901  
15              
16 26     26   255 use Moo;
  26         58  
  26         295  
17 26     26   17464 use namespace::autoclean;
  26         83  
  26         325  
18              
19 26     26   4050 use constant PROPERTIES => qw();
  26         113  
  26         2096  
20              
21 26     26   202 use constant STIX_OBJECT => undef;
  26         57  
  26         1852  
22 26     26   189 use constant STIX_OBJECT_TYPE => undef;
  26         55  
  26         32960  
23              
24             sub generate_id {
25              
26 38     38 1 131 my ($self, $ns, $name) = @_;
27              
28 38         2144 my $type = $self->STIX_OBJECT_TYPE;
29 38 50 33     301 my $uuid_version = ($ns || $name) ? UUID_V5 : UUID_V4;
30              
31 38 50       182 Carp::carp 'Unknown object type' unless $type;
32              
33 38         1925 return $self->generate_id_for_type($type, $ns, $name);
34              
35             }
36              
37             sub generate_id_for_type {
38              
39 38     38 0 140 my ($self, $type, $ns, $name) = @_;
40 38 50 33     276 my $uuid_version = ($ns || $name) ? UUID_V5 : UUID_V4;
41 38         281 return sprintf('%s--%s', $type, create_uuid_as_string($uuid_version, $ns, $name));
42              
43             }
44              
45 155     155 1 33185 sub validate { STIX::Schema->new(object => shift)->validate }
46              
47             sub to_string {
48              
49 5822     5822 1 5335819 my $self = shift;
50              
51 5822         68402 my $json = Cpanel::JSON::XS->new->utf8->canonical->allow_nonref->allow_unknown->allow_blessed->convert_blessed
52             ->stringify_infnan->escape_slash(0)->allow_dupkeys->pretty;
53              
54 5822         20735 return $json->encode($self->TO_JSON);
55              
56             }
57              
58             sub to_hash {
59              
60 0     0 1 0 my $self = shift;
61              
62 0         0 my $json = $self->to_string;
63 0         0 return Cpanel::JSON::XS->new->decode($json);
64              
65             }
66              
67             sub _render_object_ref {
68              
69 4677     4677   7823 my $object = shift;
70              
71 4677 100       10842 if (ref($object) eq 'STIX::Common::Identifier') {
72 4614         14251 return $object->to_string;
73             }
74              
75 63         1122 return $object->id;
76              
77             }
78              
79             sub TO_JSON {
80              
81 5953     5953 1 100493 my $self = shift;
82              
83 5953         11384 my $json = {};
84              
85 5953         31364 foreach my $property ($self->PROPERTIES()) {
86              
87 73256 100       266995 if ($self->can($property)) {
88              
89 73251         1387397 my $value = $self->$property;
90 73251 100       494911 next unless defined $value;
91              
92 47644 100 100     279562 if (ref($value) && $property =~ /_ref$/) {
    100 100        
    100          
93 2644         6006 $json->{$property} = _render_object_ref($value);
94             }
95             elsif ($property eq 'extensions') {
96              
97 81         317 $json->{extensions} = {};
98              
99 81 100 66     488 if (ref $value eq 'ARRAY' || ref($value) eq 'STIX::Common::List') {
100              
101 21         37 foreach my $extension (@{$value}) {
  21         60  
102 21 50 33     180 if (ref $extension && $extension->EXTENSION_TYPE()) {
103 21         99 $json->{extensions}->{$extension->EXTENSION_TYPE()} = $extension;
104             }
105             }
106              
107             }
108              
109 81 100       315 if (ref $value eq 'HASH') {
110 60         263 $json->{extensions} = $value;
111             }
112              
113             }
114             elsif (ref($value) eq 'ARRAY' || ref($value) eq 'STIX::Common::List') {
115              
116 16433 100       23259 if (@{$value}) {
  16433         58503  
117              
118 1761         6619 $json->{$property} = [];
119              
120 1761         2893 foreach my $item (@{$value}) {
  1761         5448  
121 5515 100 66     32637 if ($property =~ /_refs$/ && ref($item)) {
122 2033         3140 push @{$json->{$property}}, _render_object_ref($item);
  2033         4820  
123             }
124             else {
125 3482         4979 push @{$json->{$property}}, $item;
  3482         11847  
126             }
127             }
128              
129             }
130              
131             }
132             else {
133 28486         101712 $json->{$property} = $value;
134             }
135              
136             }
137              
138             }
139              
140             # Add custom properties
141 5953 100       26129 if ($self->can('custom_properties')) {
142 2949         4744 foreach my $custom_property (keys %{$self->custom_properties}) {
  2949         74427  
143 0         0 $json->{$custom_property} = $self->custom_properties->{$custom_property};
144             }
145             }
146              
147 5953         94295 return $json;
148              
149             }
150              
151             1;
152              
153             =encoding utf-8
154              
155             =head1 NAME
156              
157             STIX::Object - Base class for STIX Objects
158              
159             =head2 HELPERS
160              
161             =over
162              
163             =item $object->generate_id ( [ $ns, $name | $name ] )
164              
165             Generate STIX Identifier
166              
167             # Generate identifier (Object Type + UUIDv4)
168             $id = $object->generate_id('CAPEC-1');
169              
170             # Generate identifier (Object Type + UUIDv5)
171             $id = $object->generate_id($org_namespace, 'CAPEC-1');
172              
173             =item $object->TO_JSON
174              
175             Encode the object in JSON.
176              
177             =item $object->to_hash
178              
179             Return the object HASH.
180              
181             =item $object->to_string
182              
183             Encode the object in JSON.
184              
185             =item $object->validate
186              
187             Validate the object using JSON Schema
188             (see L).
189              
190             =back
191              
192              
193             =head1 SUPPORT
194              
195             =head2 Bugs / Feature Requests
196              
197             Please report any bugs or feature requests through the issue tracker
198             at L.
199             You will be notified automatically of any progress on your issue.
200              
201             =head2 Source Code
202              
203             This is open source software. The code repository is available for
204             public review and contribution under the terms of the license.
205              
206             L
207              
208             git clone https://github.com/giterlizzi/perl-STIX.git
209              
210              
211             =head1 AUTHOR
212              
213             =over 4
214              
215             =item * Giuseppe Di Terlizzi
216              
217             =back
218              
219              
220             =head1 LICENSE AND COPYRIGHT
221              
222             This software is copyright (c) 2024 by Giuseppe Di Terlizzi.
223              
224             This is free software; you can redistribute it and/or modify it under
225             the same terms as the Perl 5 programming language system itself.
226              
227             =cut