-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proxy for writing and reading object properties.
- Loading branch information
Showing
23 changed files
with
308 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Smoren\Schemator\Interfaces; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
interface ProxyInterface | ||
{ | ||
/** | ||
* @return mixed | ||
*/ | ||
public function getValue(); | ||
|
||
/** | ||
* @param mixed $value | ||
* @return void | ||
*/ | ||
public function setValue($value): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Smoren\Schemator\Structs; | ||
|
||
use Smoren\Schemator\Helpers\ObjectAccessHelper; | ||
use Smoren\Schemator\Interfaces\ProxyInterface; | ||
|
||
/** | ||
* @template T of object | ||
* @implements ProxyInterface<T> | ||
*/ | ||
class ObjectPropertyProxy implements ProxyInterface | ||
{ | ||
/** | ||
* @var T | ||
*/ | ||
protected object $object; | ||
protected string $propertyName; | ||
|
||
/** | ||
* @param T $object | ||
* @param string $propertyName | ||
*/ | ||
public function __construct(object $object, string $propertyName) | ||
{ | ||
$this->object = $object; | ||
$this->propertyName = $propertyName; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getValue() | ||
{ | ||
if (!ObjectAccessHelper::hasReadableProperty($this->object, $this->propertyName)) { | ||
$className = get_class($this->object); | ||
throw new \BadMethodCallException("Property '{$className}::{$this->propertyName}' is not readable"); | ||
} | ||
return ObjectAccessHelper::getPropertyValue($this->object, $this->propertyName); | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* @return void | ||
*/ | ||
public function setValue($value): void | ||
{ | ||
if (!ObjectAccessHelper::hasWritableProperty($this->object, $this->propertyName)) { | ||
$className = get_class($this->object); | ||
throw new \BadMethodCallException("Property '{$className}::{$this->propertyName}' is not writable"); | ||
} | ||
ObjectAccessHelper::setPropertyValue($this->object, $this->propertyName, $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.