-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
412 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
|
||
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess; | ||
|
||
use ReflectionClass; | ||
use ReflectionMethod; | ||
|
||
/** | ||
* ObjectAccess instance that only works on objects that implement SelfObjectAccessInterface | ||
* | ||
* @see SelfObjectAccessInterface | ||
*/ | ||
class SelfObjectAccess implements ObjectAccessSupportedInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getGetterFields(ReflectionClass $reflectionClass): array | ||
{ | ||
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); | ||
return $method->invoke(null); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getSetterFields(ReflectionClass $reflectionClass): array | ||
{ | ||
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); | ||
return $method->invoke(null); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getGetterTypes(ReflectionClass $reflectionClass, string $fieldName): array | ||
{ | ||
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); | ||
return $method->invoke(null, $fieldName); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getSetterTypes(ReflectionClass $reflectionClass, string $fieldName): array | ||
{ | ||
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); | ||
return $method->invoke(null, $fieldName); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getConstructorArguments(ReflectionClass $reflectionClass): array | ||
{ | ||
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); | ||
return $method->invoke(null); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getMethodArguments(ReflectionMethod $method, ?ReflectionClass $reflectionClass = null): array | ||
{ | ||
return (new ObjectAccess())->getMethodArguments($method, $reflectionClass); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getValue(object $instance, string $fieldName) | ||
{ | ||
$method = new ReflectionMethod($instance, 'getFieldNameValue'); | ||
return $method->invoke($instance, $fieldName); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setValue(object $instance, string $fieldName, $value) | ||
{ | ||
$method = new ReflectionMethod($instance, 'setFieldNameValue'); | ||
return $method->invoke($instance, $fieldName, $value); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function instantiate(ReflectionClass $reflectionClass, array $constructorArgs): object | ||
{ | ||
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); | ||
return $method->invoke(null, $constructorArgs); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getDescription(ReflectionClass $reflectionClass, string $fieldName, bool $preferGetters): ?string | ||
{ | ||
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); | ||
return $method->invoke(null, $fieldName, $preferGetters); | ||
} | ||
|
||
/** | ||
* Returns true if class is supported for this ObjectAccess instance. | ||
* | ||
* @param ReflectionClass $reflectionClass | ||
* @return bool | ||
*/ | ||
public function isSupported(ReflectionClass $reflectionClass): bool | ||
{ | ||
return $reflectionClass->implementsInterface(SelfObjectAccessInterface::class); | ||
} | ||
} |
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,85 @@ | ||
<?php | ||
|
||
|
||
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess; | ||
|
||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* A class implementing this interface can provide its own field information. | ||
* | ||
* @see SelfObjectAccessInterface | ||
*/ | ||
interface SelfObjectAccessInterface | ||
{ | ||
/** | ||
* Returns all property names that can be retrieved. | ||
* | ||
* @return string[] | ||
*/ | ||
public static function getGetterFields(): array; | ||
|
||
/** | ||
* Returns all property names that can be changed. | ||
* | ||
* @return string[] | ||
*/ | ||
public static function getSetterFields(): array; | ||
|
||
/** | ||
* Returns all types that can be returned. | ||
* | ||
* @param string $fieldName | ||
* @return Type[] | ||
*/ | ||
public static function getGetterTypes(string $fieldName): array; | ||
|
||
/** | ||
* Returns all types that can be used for changing a value. | ||
* | ||
* @param string $fieldName | ||
* @return array | ||
*/ | ||
public static function getSetterTypes(string $fieldName): array; | ||
|
||
/** | ||
* Returns all types to instantiate an object. | ||
* | ||
* @return Type[] | ||
*/ | ||
public static function getConstructorArguments(): array; | ||
|
||
/** | ||
* Gets a property value. | ||
* | ||
* @param string $fieldName | ||
* @return mixed | ||
*/ | ||
public function getFieldNameValue(string $fieldName); | ||
|
||
/** | ||
* Sets a property instance value. | ||
* | ||
* @param string $fieldName | ||
* @param mixed $value | ||
* @return mixed | ||
*/ | ||
public function setFieldNameValue(string $fieldName, $value); | ||
|
||
/** | ||
* Instantiate the object with the arguments provided here. | ||
* | ||
* @param array $constructorArgs | ||
* @return object | ||
*/ | ||
public static function instantiate(array $constructorArgs): object; | ||
|
||
/** | ||
* Returns a string description of the property. | ||
* | ||
* @param string $fieldName | ||
* @param bool $preferGetters | ||
* @return string|null | ||
*/ | ||
public static function getDescription(string $fieldName, bool $preferGetters): ?string; | ||
} |
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,133 @@ | ||
<?php | ||
|
||
|
||
namespace W2w\Test\ApieObjectAccessNormalizer\Mocks; | ||
|
||
|
||
use InvalidArgumentException; | ||
use Symfony\Component\PropertyInfo\Type; | ||
use W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess\SelfObjectAccessInterface; | ||
|
||
class ClassWithSelfObjectAccess implements SelfObjectAccessInterface | ||
{ | ||
const VALID_KEYS = ['one', 'two', 'three']; | ||
|
||
private $input = [ | ||
'one' => 1, | ||
'two' => 2, | ||
'three' => 3, | ||
]; | ||
|
||
public function __construct(array $input) | ||
{ | ||
foreach ($input as $key => $value) { | ||
$this->$key = $value; | ||
} | ||
} | ||
|
||
public function __set($key, int $value) | ||
{ | ||
if (!in_array($key, self::VALID_KEYS)) { | ||
throw new InvalidArgumentException('$key should be one, two, or three'); | ||
} | ||
$this->input[$key] = $value; | ||
} | ||
|
||
public function __get($key): int | ||
{ | ||
if (!in_array($key, self::VALID_KEYS)) { | ||
throw new InvalidArgumentException('$key should be one, two, or three'); | ||
} | ||
return $this->input[$key]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function getGetterFields(): array | ||
{ | ||
return self::VALID_KEYS; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function getSetterFields(): array | ||
{ | ||
return self::VALID_KEYS; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function getGetterTypes(string $fieldName): array | ||
{ | ||
if (!in_array($fieldName, self::VALID_KEYS)) { | ||
throw new InvalidArgumentException('$key should be one, two, or three'); | ||
} | ||
return [new Type(Type::BUILTIN_TYPE_INT, false)]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function getSetterTypes(string $fieldName): array | ||
{ | ||
if (!in_array($fieldName, self::VALID_KEYS)) { | ||
throw new InvalidArgumentException('$key should be one, two, or three'); | ||
} | ||
return [new Type(Type::BUILTIN_TYPE_INT, false)]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function getConstructorArguments(): array | ||
{ | ||
return [ | ||
'input' => new Type( | ||
Type::BUILTIN_TYPE_ARRAY, | ||
false, | ||
null, | ||
true, | ||
new Type(Type::BUILTIN_TYPE_STRING, false), | ||
new Type(Type::BUILTIN_TYPE_INT, false) | ||
) | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function getFieldNameValue(string $fieldName) | ||
{ | ||
return $this->$fieldName; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function setFieldNameValue(string $fieldName, $value) | ||
{ | ||
$this->$fieldName = $value; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function instantiate(array $constructorArgs): object | ||
{ | ||
return new self($constructorArgs['input'] ?? []); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function getDescription(string $fieldName, bool $preferGetters): ?string | ||
{ | ||
if (!in_array($fieldName, self::VALID_KEYS)) { | ||
throw new InvalidArgumentException('$key should be one, two, or three'); | ||
} | ||
return $fieldName; | ||
} | ||
} |
Oops, something went wrong.