TestsLing /
easy-im
| 1 | <?php |
||
| 2 | |||
| 3 | namespace EasyIM\Kernel; |
||
| 4 | |||
| 5 | use EasyIM\Kernel\Contracts\ParameterInterface; |
||
| 6 | use EasyIM\Kernel\Traits\HasAttributes; |
||
| 7 | |||
| 8 | abstract class Parameter implements ParameterInterface |
||
| 9 | { |
||
| 10 | use HasAttributes; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 11 | |||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $properties = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Parameter constructor. |
||
| 19 | * |
||
| 20 | * @param array $attributes |
||
| 21 | */ |
||
| 22 | 7 | public function __construct(array $attributes = []) |
|
| 23 | { |
||
| 24 | 7 | $this->setAttributes($attributes); |
|
| 25 | 7 | } |
|
| 26 | |||
| 27 | /** |
||
| 28 | * |
||
| 29 | * @param array $appends |
||
| 30 | * |
||
| 31 | * @return array|array[] |
||
| 32 | * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
||
| 33 | */ |
||
| 34 | 6 | public function transformToArray(array $appends = []): array |
|
| 35 | { |
||
| 36 | 6 | return array_merge($this->propertiesToArray([]), $appends); |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | 1 | public function transformParameterToArray(): array |
|
| 44 | { |
||
| 45 | 1 | $attrs = []; |
|
| 46 | |||
| 47 | 1 | foreach ($this->attributes as $parameter) { |
|
| 48 | 1 | $attrs[] = $parameter->transformToArray(); |
|
| 49 | } |
||
| 50 | |||
| 51 | 1 | return $attrs; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * |
||
| 56 | * @param array $data |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
||
| 60 | */ |
||
| 61 | 6 | protected function propertiesToArray(array $data): array |
|
| 62 | { |
||
| 63 | 6 | $this->checkRequiredAttributes(); |
|
| 64 | |||
| 65 | 6 | foreach ($this->attributes as $property => $value) { |
|
| 66 | 6 | if (is_null($value) && !$this->isRequired($property)) { |
|
| 67 | continue; |
||
| 68 | } |
||
| 69 | |||
| 70 | 6 | $data[$property] = $this->get($property); |
|
| 71 | } |
||
| 72 | |||
| 73 | 6 | return $data; |
|
| 74 | } |
||
| 75 | } |
||
| 76 |