Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
68.75% |
11 / 16 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
| BorshDeserializable | |
68.75% |
11 / 16 |
|
60.00% |
3 / 5 |
14.69 | |
0.00% |
0 / 1 |
| borshConstructor | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __set | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| __isset | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| __unset | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| isPrivateProperty | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Attestto\SolanaPhpSdk\Borsh; |
| 3 | use ReflectionClass; |
| 4 | trait BorshDeserializable |
| 5 | { |
| 6 | /** |
| 7 | * Create a new instance of this object. |
| 8 | * |
| 9 | * Note: must override when the default constructor required parameters! |
| 10 | * |
| 11 | * @return $this |
| 12 | */ |
| 13 | public static function borshConstructor() |
| 14 | { |
| 15 | return new static(); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Magic setter to dynamically set properties. |
| 20 | * |
| 21 | * @param string $name |
| 22 | * @param mixed $value |
| 23 | */ |
| 24 | public function __set(string $name, mixed $value) |
| 25 | { |
| 26 | // Set the value in the dynamic properties if it's not private |
| 27 | if (!$this->isPrivateProperty($name)) { |
| 28 | $this->fields[$name] = $value; |
| 29 | } |
| 30 | |
| 31 | // Check if the property exists as a private property |
| 32 | if ($this->isPrivateProperty($name)) { |
| 33 | // Use reflection to set the value of the private property |
| 34 | $reflectionClass = new ReflectionClass($this); |
| 35 | $property = $reflectionClass->getProperty($name); |
| 36 | $property->setAccessible(true); |
| 37 | $property->setValue($this, $value); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Magic isset to check if dynamically set property is set. |
| 43 | * |
| 44 | * @param string $name |
| 45 | * |
| 46 | * @return bool |
| 47 | */ |
| 48 | public function __isset(string $name) |
| 49 | { |
| 50 | return isset($this->fields[$name]) || isset($this->private[$name]); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Magic unset to unset dynamically set property. |
| 55 | * |
| 56 | * @param string $name |
| 57 | */ |
| 58 | public function __unset(string $name) |
| 59 | { |
| 60 | if (isset($this->fields[$name])) { |
| 61 | unset($this->fields[$name]); |
| 62 | } elseif ( isset($this->private[$name])) { |
| 63 | unset($this->privateProperties[$name]); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Determine if a property is considered private. |
| 69 | * |
| 70 | * @param string $name |
| 71 | * |
| 72 | * @return bool |
| 73 | */ |
| 74 | private function isPrivateProperty(string $name): bool |
| 75 | { |
| 76 | // Get the class name ( whatever class is implementing this trait, e.g. Any Schema/Struct based object |
| 77 | $className = static::class; |
| 78 | |
| 79 | // Create a ReflectionClass instance for the class |
| 80 | $reflectionClass = new ReflectionClass($className); |
| 81 | |
| 82 | // Check if the property is declared in the class and is private |
| 83 | return $reflectionClass->hasProperty($name) && $reflectionClass->getProperty($name)->isPrivate(); |
| 84 | } |
| 85 | } |