This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Spatie\DataTransferObject; |
||
| 6 | |||
| 7 | use ReflectionClass; |
||
| 8 | use ReflectionProperty; |
||
| 9 | |||
| 10 | abstract class DataTransferObject |
||
| 11 | { |
||
| 12 | protected bool $ignoreMissing = false; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 13 | |||
| 14 | protected array $exceptKeys = []; |
||
| 15 | |||
| 16 | protected array $onlyKeys = []; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param array $parameters |
||
| 20 | * |
||
| 21 | * @return \Spatie\DataTransferObject\ImmutableDataTransferObject|static |
||
| 22 | */ |
||
| 23 | public static function immutable(array $parameters = []): ImmutableDataTransferObject |
||
| 24 | { |
||
| 25 | return new ImmutableDataTransferObject(new static($parameters)); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @param array $arrayOfParameters |
||
| 30 | * |
||
| 31 | * @return \Spatie\DataTransferObject\ImmutableDataTransferObject[]|static[] |
||
| 32 | */ |
||
| 33 | public static function arrayOf(array $arrayOfParameters): array |
||
| 34 | { |
||
| 35 | return array_map( |
||
| 36 | function ($parameters) { |
||
| 37 | return new static($parameters); |
||
| 38 | }, |
||
| 39 | $arrayOfParameters |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | |||
| 43 | public function __construct(array $parameters = []) |
||
| 44 | { |
||
| 45 | $validators = $this->getFieldValidators(); |
||
| 46 | |||
| 47 | $valueCaster = $this->getValueCaster(); |
||
| 48 | |||
| 49 | /** string[] */ |
||
| 50 | $invalidTypes = []; |
||
| 51 | |||
| 52 | foreach ($validators as $field => $validator) { |
||
| 53 | if ( |
||
| 54 | ! isset($parameters[$field]) |
||
| 55 | && ! $validator->hasDefaultValue |
||
| 56 | && ! $validator->isNullable |
||
| 57 | ) { |
||
| 58 | throw DataTransferObjectError::uninitialized( |
||
| 59 | static::class, |
||
| 60 | $field |
||
| 61 | ); |
||
| 62 | } |
||
| 63 | |||
| 64 | $value = $parameters[$field] ?? $this->{$field} ?? null; |
||
| 65 | |||
| 66 | $value = $this->castValue($valueCaster, $validator, $value); |
||
| 67 | |||
| 68 | if (! $validator->isValidType($value)) { |
||
| 69 | $invalidTypes[] = DataTransferObjectError::invalidTypeMessage( |
||
| 70 | static::class, |
||
| 71 | $field, |
||
| 72 | $validator->allowedTypes, |
||
| 73 | $value |
||
| 74 | ); |
||
| 75 | |||
| 76 | continue; |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->{$field} = $value; |
||
| 80 | |||
| 81 | unset($parameters[$field]); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ($invalidTypes) { |
||
| 85 | DataTransferObjectError::invalidTypes($invalidTypes); |
||
| 86 | } |
||
| 87 | |||
| 88 | if (! $this->ignoreMissing && count($parameters)) { |
||
| 89 | throw DataTransferObjectError::unknownProperties(array_keys($parameters), static::class); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | public function all(): array |
||
| 94 | { |
||
| 95 | $data = []; |
||
| 96 | |||
| 97 | $class = new ReflectionClass(static::class); |
||
| 98 | |||
| 99 | $properties = $class->getProperties(ReflectionProperty::IS_PUBLIC); |
||
| 100 | |||
| 101 | foreach ($properties as $reflectionProperty) { |
||
| 102 | // Skip static properties |
||
| 103 | if ($reflectionProperty->isStatic()) { |
||
| 104 | continue; |
||
| 105 | } |
||
| 106 | |||
| 107 | $data[$reflectionProperty->getName()] = $reflectionProperty->getValue($this); |
||
| 108 | } |
||
| 109 | |||
| 110 | return $data; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string ...$keys |
||
| 115 | * |
||
| 116 | * @return static |
||
| 117 | */ |
||
| 118 | public function only(string ...$keys): DataTransferObject |
||
| 119 | { |
||
| 120 | $dataTransferObject = clone $this; |
||
| 121 | |||
| 122 | $dataTransferObject->onlyKeys = [...$this->onlyKeys, ...$keys]; |
||
| 123 | |||
| 124 | return $dataTransferObject; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string ...$keys |
||
| 129 | * |
||
| 130 | * @return static |
||
| 131 | */ |
||
| 132 | public function except(string ...$keys): DataTransferObject |
||
| 133 | { |
||
| 134 | $dataTransferObject = clone $this; |
||
| 135 | |||
| 136 | $dataTransferObject->exceptKeys = [...$this->exceptKeys, ...$keys]; |
||
| 137 | |||
| 138 | return $dataTransferObject; |
||
| 139 | } |
||
| 140 | |||
| 141 | public function toArray(): array |
||
| 142 | { |
||
| 143 | if (count($this->onlyKeys)) { |
||
| 144 | $array = Arr::only($this->all(), $this->onlyKeys); |
||
| 145 | } else { |
||
| 146 | $array = Arr::except($this->all(), $this->exceptKeys); |
||
| 147 | } |
||
| 148 | |||
| 149 | $array = $this->parseArray($array); |
||
| 150 | |||
| 151 | return $array; |
||
| 152 | } |
||
| 153 | |||
| 154 | protected function parseArray(array $array): array |
||
| 155 | { |
||
| 156 | foreach ($array as $key => $value) { |
||
| 157 | if ( |
||
| 158 | $value instanceof DataTransferObject |
||
| 159 | || $value instanceof DataTransferObjectCollection |
||
| 160 | ) { |
||
| 161 | $array[$key] = $value->toArray(); |
||
| 162 | |||
| 163 | continue; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (! is_array($value)) { |
||
| 167 | continue; |
||
| 168 | } |
||
| 169 | |||
| 170 | $array[$key] = $this->parseArray($value); |
||
| 171 | } |
||
| 172 | |||
| 173 | return $array; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param \ReflectionClass $class |
||
| 178 | * |
||
| 179 | * @return \Spatie\DataTransferObject\FieldValidator[] |
||
| 180 | */ |
||
| 181 | protected function getFieldValidators(): array |
||
| 182 | { |
||
| 183 | return DTOCache::resolve(static::class, function () { |
||
| 184 | $class = new ReflectionClass(static::class); |
||
| 185 | |||
| 186 | $properties = []; |
||
| 187 | |||
| 188 | foreach ($class->getProperties(ReflectionProperty::IS_PUBLIC) as $reflectionProperty) { |
||
| 189 | // Skip static properties |
||
| 190 | if ($reflectionProperty->isStatic()) { |
||
| 191 | continue; |
||
| 192 | } |
||
| 193 | |||
| 194 | $field = $reflectionProperty->getName(); |
||
| 195 | |||
| 196 | $properties[$field] = FieldValidator::fromReflection($reflectionProperty); |
||
| 197 | } |
||
| 198 | |||
| 199 | return $properties; |
||
| 200 | }); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @param \Spatie\DataTransferObject\ValueCaster $valueCaster |
||
| 205 | * @param \Spatie\DataTransferObject\FieldValidator $fieldValidator |
||
| 206 | * @param mixed $value |
||
| 207 | * |
||
| 208 | * @return mixed |
||
| 209 | */ |
||
| 210 | protected function castValue(ValueCaster $valueCaster, FieldValidator $fieldValidator, $value) |
||
| 211 | { |
||
| 212 | if (is_array($value)) { |
||
| 213 | return $valueCaster->cast($value, $fieldValidator); |
||
| 214 | } |
||
| 215 | |||
| 216 | return $value; |
||
| 217 | } |
||
| 218 | |||
| 219 | protected function getValueCaster(): ValueCaster |
||
| 220 | { |
||
| 221 | return new ValueCaster(); |
||
| 222 | } |
||
| 223 | } |
||
| 224 |