Issues (4)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Transformer.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Arcanedev\LaravelApiHelper;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Illuminate\Contracts\Support\Jsonable;
5
use Illuminate\Http\JsonResponse;
6
use Illuminate\Pagination\AbstractPaginator;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * Class     Transformer
12
 *
13
 * @package  Arcanedev\LaravelApiHelper
14
 * @author   ARCANEDEV <[email protected]>
15
 *
16
 * @deprecated Since v2.2, use \Arcanedev\LaravelApiHelper\Http\Resource instead.
17
 */
18
abstract class Transformer implements Arrayable, Jsonable
19
{
20
    /* -----------------------------------------------------------------
21
     |  Properties
22
     | -----------------------------------------------------------------
23
     */
24
25
    /** @var  mixed */
26
    protected $resource;
27
28
    /** @var array */
29
    protected $extras = [];
30
31
    /* -----------------------------------------------------------------
32
     |  Constructor
33
     | -----------------------------------------------------------------
34
     */
35
36
    /**
37
     * Transformer constructor.
38
     *
39
     * @param  mixed  $resource
40
     */
41 40
    public function __construct($resource)
42
    {
43 40
        $this->resource = $resource;
44 40
    }
45
46
    /* -----------------------------------------------------------------
47
     |  Getters & Setters
48
     | -----------------------------------------------------------------
49
     */
50
51
    /**
52
     * Add extras data to the response.
53
     *
54
     * @param  array  $extras
55
     *
56
     * @return $this
57
     */
58 4
    public function withExtras(array $extras)
59
    {
60 4
        $this->extras = $extras;
61
62 4
        return $this;
63
    }
64
65
    /**
66
     * Add meta data to be associated with the response.
67
     *
68
     * @param  array  $data
69
     *
70
     * @return $this
71
     */
72 12
    public function withMeta(array $data)
73
    {
74 12
        return $this->setExtra('meta', Arr::sortRecursive($data));
75
    }
76
77
    /**
78
     * Get the value from the extra data.
79
     *
80
     * @param  string  $key
81
     * @param  mixed   $default
82
     *
83
     * @return mixed
84
     */
85 8
    public function getExtra($key, $default = null)
86
    {
87 8
        return Arr::get($this->extras, $key, $default);
88
    }
89
90
    /**
91
     * Set an extra data to the response.
92
     *
93
     * @param  string  $key
94
     * @param  mixed   $value
95
     *
96
     * @return $this
97
     */
98 16
    public function setExtra($key, $value)
99
    {
100 16
        $this->extras[$key] = $value;
101
102 16
        return $this;
103
    }
104
105
    /* -----------------------------------------------------------------
106
     |  Main Methods
107
     | -----------------------------------------------------------------
108
     */
109
110
    /**
111
     * Create a new instance of the response.
112
     *
113
     * @param  mixed  $resource
114
     *
115
     * @return static
116
     */
117 28
    public static function with($resource)
118
    {
119 28
        return new static($resource);
0 ignored issues
show
Deprecated Code introduced by
The class Arcanedev\LaravelApiHelper\Transformer has been deprecated with message: Since v2.2, use \Arcanedev\LaravelApiHelper\Http\Resource instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
120
    }
121
122
    /**
123
     * Get a displayable API output for the given object.
124
     *
125
     * @return array
126
     */
127 40
    public function transform()
128
    {
129 40
        $object = $this->resource;
130
131 40
        $data = $object instanceof Collection || $object instanceof AbstractPaginator
132 16
            ? $object->map([$this, 'transformResource'])->toArray()
133 40
            : $this->transformResource($object);
134
135 40
        if ($object instanceof AbstractPaginator) {
136 8
            $this->withMeta(array_merge(
137 8
                $this->getExtra('meta', []), Arr::except($object->toArray(), ['data'])
138
            ));
139
        }
140
141 40
        $data = array_filter(compact('data') + $this->extras);
142 40
        ksort($data);
143
144 40
        return $data;
145
    }
146
147
    /**
148
     * Get a displayable API output for the given sub-resource object.
149
     *
150
     * @param  mixed  $resource
151
     *
152
     * @return array
153
     */
154 4
    public static function subResource($resource)
155
    {
156 4
        return Arr::get(static::with($resource)->toArray(), 'data');
157
    }
158
159
    /**
160
     * Transform the given resource for the API output.
161
     *
162
     * @param  mixed  $resource
163
     *
164
     * @return array
165
     */
166
    abstract public function transformResource($resource);
167
168
    /**
169
     * Get the instance as a json response object.
170
     *
171
     * @param  int    $status
172
     * @param  array  $headers
173
     * @param  int    $options
174
     *
175
     * @return \Illuminate\Http\JsonResponse
176
     */
177 4
    public function toResponse($status = 200, array $headers = [], $options = 0)
178
    {
179 4
        return new JsonResponse($this->transform(), $status, $headers, $options);
180
    }
181
182
    /**
183
     * Get the instance as an array.
184
     *
185
     * @return array
186
     */
187 28
    public function toArray()
188
    {
189 28
        return $this->transform();
190
    }
191
192
    /**
193
     * Get the instance as an array.
194
     *
195
     * @param  int  $options
196
     *
197
     * @return string
198
     */
199 4
    public function toJson($options = 0)
200
    {
201 4
        return json_encode($this->toArray(), $options);
202
    }
203
}
204