1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwaggerGen\Swagger; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Describes a Swagger Parameter object of the "body" variety. |
7
|
|
|
* |
8
|
|
|
* @package SwaggerGen |
9
|
|
|
* @author Martijn van der Lee <[email protected]> |
10
|
|
|
* @copyright 2014-2016 Martijn van der Lee |
11
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
12
|
|
|
*/ |
13
|
|
|
class BodyParameter extends AbstractObject implements IParameter |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
private $name = ''; |
17
|
|
|
private $description; |
18
|
|
|
private $required = false; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Schema |
22
|
|
|
*/ |
23
|
|
|
private $schema; |
24
|
|
|
|
25
|
|
|
public function __construct(AbstractObject $parent, $data, $required = false) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($parent); |
28
|
|
|
|
29
|
|
|
$type = self::wordShift($data); |
30
|
|
|
if (empty($type)) { |
31
|
|
|
throw new \SwaggerGen\Exception('No type definition for body parameter'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->name = self::wordShift($data); |
|
|
|
|
35
|
|
|
if (empty($this->name)) { |
36
|
|
|
throw new \SwaggerGen\Exception('No name for body parameter'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->description = $data; |
40
|
|
|
$this->required = (bool) $required; |
41
|
|
|
|
42
|
|
|
$this->schema = new Schema($this, $type); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function handleCommand($command, $data = null) |
46
|
|
|
{ |
47
|
|
|
// Pass through to Type |
48
|
|
|
$return = $this->schema->handleCommand($command, $data); |
49
|
|
|
if ($return) { |
50
|
|
|
return $return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return parent::handleCommand($command, $data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function toArray() |
57
|
|
|
{ |
58
|
|
|
return self::arrayFilterNull(array_merge(array( |
59
|
|
|
'name' => $this->name, |
60
|
|
|
'in' => 'body', |
61
|
|
|
'description' => empty($this->description) ? null : $this->description, |
62
|
|
|
'required' => $this->required ? true : null, |
63
|
|
|
'schema' => $this->schema->toArray(), |
64
|
|
|
), parent::toArray())); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function __toString() |
68
|
|
|
{ |
69
|
|
|
return __CLASS__ . ' ' . $this->name; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getName() |
73
|
|
|
{ |
74
|
|
|
return $this->name; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.