|
10 | 10 | */ |
11 | 11 | trait AttributeTrait |
12 | 12 | { |
13 | | - /** |
14 | | - * @var array |
15 | | - */ |
16 | | - protected $data = []; |
| 13 | + /** |
| 14 | + * @var array |
| 15 | + */ |
| 16 | + protected $data = []; |
17 | 17 |
|
18 | | - /** |
19 | | - * Attribute setter |
20 | | - * |
21 | | - * @param string $name |
22 | | - * @param mixed $value |
23 | | - */ |
24 | | - final public function __set($name, $value) |
25 | | - { |
26 | | - switch ($name) |
| 18 | + /** |
| 19 | + * Attribute setter |
| 20 | + * |
| 21 | + * @param string $name |
| 22 | + * @param mixed $value |
| 23 | + */ |
| 24 | + final public function __set(string $name, $value) |
27 | 25 | { |
28 | | - case 'shape': |
29 | | - return $this->__construct( |
30 | | - $this->reshape($value)->data |
31 | | - ); |
32 | | - default: |
33 | | - throw new InvalidAttributeException(__CLASS__, $name); |
| 26 | + switch ($name) |
| 27 | + { |
| 28 | + case 'shape': |
| 29 | + return $this->__construct( |
| 30 | + $this->reshape($value)->data |
| 31 | + ); |
| 32 | + default: |
| 33 | + throw new InvalidAttributeException(__CLASS__, $name); |
| 34 | + } |
34 | 35 | } |
35 | | - } |
36 | 36 |
|
37 | | - /** |
38 | | - * Generic getter |
39 | | - * |
40 | | - * @param string $name |
41 | | - * @return mixed |
42 | | - * @throws \SciPhp\Exception\InvalidAttributeException |
43 | | - */ |
44 | | - final public function __get($name) |
45 | | - { |
46 | | - switch ($name) |
| 37 | + /** |
| 38 | + * Generic getter |
| 39 | + * |
| 40 | + * @param string $name |
| 41 | + * @return mixed |
| 42 | + * @throws \SciPhp\Exception\InvalidAttributeException |
| 43 | + */ |
| 44 | + final public function __get(string $name) |
47 | 45 | { |
48 | | - case 'data': |
49 | | - return $this->data; |
50 | | - case 'ndim': |
51 | | - return (int)$this->getNdim($this->data); |
52 | | - case 'size': |
53 | | - return $this->getSize(); |
54 | | - case 'shape': |
55 | | - return $this->getShape($this->data, []); |
56 | | - case 'T': |
57 | | - return np::transpose($this); |
58 | | - } |
| 46 | + switch ($name) |
| 47 | + { |
| 48 | + case 'data': |
| 49 | + return $this->data; |
| 50 | + case 'ndim': |
| 51 | + return (int)$this->getNdim($this->data); |
| 52 | + case 'size': |
| 53 | + return $this->getSize(); |
| 54 | + case 'shape': |
| 55 | + return $this->getShape($this->data, []); |
| 56 | + case 'T': |
| 57 | + return np::transpose($this); |
| 58 | + } |
59 | 59 |
|
60 | | - throw new InvalidAttributeException(__CLASS__, $name); |
61 | | - } |
| 60 | + throw new InvalidAttributeException(__CLASS__, $name); |
| 61 | + } |
62 | 62 |
|
63 | | - /** |
64 | | - * Gets NdArray rank |
65 | | - * |
66 | | - * @param array $data |
67 | | - * @return int |
68 | | - */ |
69 | | - final protected function getNdim(array $data) |
70 | | - { |
71 | | - if (isset($data[0])) { |
72 | | - return is_array($data[0]) |
73 | | - ? 1 + $this->getNdim($data[0]) |
74 | | - : 1; |
| 63 | + /** |
| 64 | + * Gets NdArray rank |
| 65 | + */ |
| 66 | + final protected function getNdim(array $data): int |
| 67 | + { |
| 68 | + if (isset($data[0])) { |
| 69 | + return is_array($data[0]) |
| 70 | + ? 1 + $this->getNdim($data[0]) |
| 71 | + : 1; |
| 72 | + } |
| 73 | + return 0; |
75 | 74 | } |
76 | | - } |
77 | 75 |
|
78 | | - /** |
79 | | - * Gets the total number of elements of the array |
80 | | - * |
81 | | - * @param int $count current count |
82 | | - * @return int |
83 | | - */ |
84 | | - final protected function getSize($count = 0) |
85 | | - { |
86 | | - array_walk_recursive( |
87 | | - $this->data, |
88 | | - function () use (&$count) { |
89 | | - $count++; |
90 | | - } |
91 | | - ); |
| 76 | + /** |
| 77 | + * Gets the total number of elements of the array |
| 78 | + */ |
| 79 | + final protected function getSize(int $count = 0): int |
| 80 | + { |
| 81 | + array_walk_recursive( |
| 82 | + $this->data, |
| 83 | + function () use (&$count) { |
| 84 | + $count++; |
| 85 | + } |
| 86 | + ); |
92 | 87 |
|
93 | | - return $count; |
94 | | - } |
| 88 | + return $count; |
| 89 | + } |
95 | 90 |
|
96 | | - protected abstract function getShape($data, $shape); |
| 91 | + protected abstract function getShape($data, array $shape): array; |
97 | 92 | } |
0 commit comments