Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class JsonMapper
* Throw an exception when JSON data contain a property
* that is not defined in the PHP class
*
* @var boolean
* @var bool
*/
public $bExceptionOnUndefinedProperty = false;

/**
* Throw an exception if the JSON data miss a property
* that is marked with @required in the PHP class
*
* @var boolean
* @var bool
*/
public $bExceptionOnMissingData = false;

Expand All @@ -41,46 +41,46 @@ class JsonMapper
*
* json_decode($str, false)
*
* @var boolean
* @var bool
*/
public $bEnforceMapType = true;

/**
* Throw an exception when an object is expected but the JSON contains
* a non-object type.
*
* @var boolean
* @var bool
*/
public $bStrictObjectTypeChecking = true;

/**
* Throw an exception, if null value is found
* but the type of attribute does not allow nulls.
*
* @var boolean
* @var bool
*/
public $bStrictNullTypes = true;

/**
* Throw an exception if null value is found in an array
* but the type of attribute does not allow nulls.
*
* @var boolean
* @var bool
*/
public $bStrictNullTypesInArrays = true;

/**
* Allow mapping of private and protected properties.
*
* @var boolean
* @var bool
*/
public $bIgnoreVisibility = false;

/**
* Remove attributes that were not passed in JSON,
* to avoid confusion between them and NULL values.
*
* @var boolean
* @var bool
*/
public $bRemoveUndefinedAttributes = false;

Expand Down Expand Up @@ -370,7 +370,7 @@ public function map($json, $object)
*
* @return string|null Fully-qualified type name with namespace
*/
protected function getFullNamespace($type, $strNs)
protected function getFullNamespace($type, $strNs): ?string
{
if ($type === null || $type === '' || $type[0] === '\\' || $strNs === '') {
return $type;
Expand All @@ -394,7 +394,7 @@ protected function getFullNamespace($type, $strNs)
*
* @return void
*/
protected function checkMissingData($providedProperties, ReflectionClass $rc)
protected function checkMissingData($providedProperties, ReflectionClass $rc): void
{
foreach ($rc->getProperties() as $property) {
$rprop = $rc->getProperty($property->name);
Expand Down Expand Up @@ -423,7 +423,7 @@ protected function checkMissingData($providedProperties, ReflectionClass $rc)
*
* @return void
*/
protected function removeUndefinedAttributes($object, $providedProperties)
protected function removeUndefinedAttributes($object, $providedProperties): void
{
foreach (get_object_vars($object) as $propertyName => $dummy) {
if (!isset($providedProperties[$propertyName])) {
Expand Down Expand Up @@ -527,7 +527,7 @@ public function mapArray($json, $array, $class = null, $parent_key = '')
* Third value: type of the property
* Fourth value: if the property is nullable
*/
protected function inspectProperty(ReflectionClass $rc, $name)
protected function inspectProperty(ReflectionClass $rc, $name): array
{
//try setter method first
$setter = 'set' . $this->getCamelCaseName($name);
Expand Down Expand Up @@ -657,7 +657,7 @@ protected function inspectProperty(ReflectionClass $rc, $name)
*
* @return string CamelCasedVariableName
*/
protected function getCamelCaseName($name)
protected function getCamelCaseName($name): string
{
return str_replace(
' ', '', ucwords(str_replace(array('_', '-'), ' ', $name))
Expand All @@ -673,7 +673,7 @@ protected function getCamelCaseName($name)
*
* @return string Name without hyphen
*/
protected function getSafeName($name)
protected function getSafeName($name): string
{
if (strpos($name, '-') !== false) {
$name = $this->getCamelCaseName($name);
Expand All @@ -696,7 +696,7 @@ protected function getSafeName($name)
*/
protected function setProperty(
$object, $accessor, $value
) {
): void {
if (!$accessor->isPublic() && $this->bIgnoreVisibility) {
if (\PHP_VERSION_ID < 80100) {
$accessor->setAccessible(true);
Expand All @@ -718,9 +718,9 @@ protected function setProperty(
* This method exists to be overwritten in child classes,
* so you can do dependency injection or so.
*
* @param string $class Class name to instantiate
* @param boolean $useParameter Pass $parameter to the constructor or not
* @param mixed $jvalue Constructor parameter (the json value)
* @param string $class Class name to instantiate
* @param bool $useParameter Pass $parameter to the constructor or not
* @param mixed $jvalue Constructor parameter (the json value)
*
* @return object Freshly created object
*/
Expand Down Expand Up @@ -758,7 +758,7 @@ protected function createInstance(
*
* @return string|null The mapped type/class name
*/
protected function getMappedType($type, $jvalue = null)
protected function getMappedType($type, $jvalue = null): ?string
{
if (isset($this->classMap[$type ?? ''])) {
$target = $this->classMap[$type];
Expand All @@ -785,11 +785,11 @@ protected function getMappedType($type, $jvalue = null)
*
* @param string $type type name from gettype()
*
* @return boolean True if it is a simple PHP type
* @return bool True if it is a simple PHP type
*
* @see isFlatType()
*/
protected function isSimpleType($type)
protected function isSimpleType($type): bool
{
return $type == 'string'
|| $type == 'boolean' || $type == 'bool'
Expand All @@ -805,9 +805,9 @@ protected function isSimpleType($type)
* @param string $type class name of type being required
* @param mixed $value Some PHP value to be tested
*
* @return boolean True if $object has type of $type
* @return bool True if $object has type of $type
*/
protected function isObjectOfSameType($type, $value)
protected function isObjectOfSameType($type, $value): bool
{
if (false === is_object($value)) {
return false;
Expand All @@ -822,11 +822,11 @@ protected function isObjectOfSameType($type, $value)
*
* @param string $type type name from gettype()
*
* @return boolean True if it is a non-nested PHP type
* @return bool True if it is a non-nested PHP type
*
* @see isSimpleType()
*/
protected function isFlatType($type)
protected function isFlatType($type): bool
{
return $type == 'NULL'
|| $type == 'string'
Expand All @@ -843,7 +843,7 @@ protected function isFlatType($type)
*
* @return bool
*/
protected function isArrayOfType($strType)
protected function isArrayOfType($strType): bool
{
return substr($strType, -2) === '[]';
}
Expand All @@ -857,7 +857,7 @@ protected function isArrayOfType($strType)
*
* @return bool
*/
protected function hasVariadicArrayType($accessor)
protected function hasVariadicArrayType($accessor): bool
{
if (!$accessor instanceof ReflectionMethod) {
return false;
Expand All @@ -879,9 +879,9 @@ protected function hasVariadicArrayType($accessor)
*
* @param string $type type name from the phpdoc param
*
* @return boolean True if it is nullable
* @return bool True if it is nullable
*/
protected function isNullable($type)
protected function isNullable($type): bool
{
return stripos('|' . $type . '|', '|null|') !== false
|| strpos('|' . $type, '|?') !== false;
Expand All @@ -894,7 +894,7 @@ protected function isNullable($type)
*
* @return string|null The new type value
*/
protected function removeNullable($type)
protected function removeNullable($type): ?string
{
if ($type === null) {
return null;
Expand All @@ -913,7 +913,7 @@ protected function removeNullable($type)
*
* @return string "foo|bar"
*/
protected function stringifyReflectionType(ReflectionType $type)
protected function stringifyReflectionType(ReflectionType $type): string
{
if ($type instanceof ReflectionNamedType) {
return ($type->isBuiltin() ? '' : '\\') . $type->getName();
Expand All @@ -939,7 +939,7 @@ function (ReflectionNamedType $type) {
* Key is the "@"-name like "param",
* each value is an array of the rest of the @-lines
*/
protected static function parseAnnotations($docblock)
protected static function parseAnnotations($docblock): array
{
$annotations = array();
// Strip away the docblock header and footer
Expand Down Expand Up @@ -967,7 +967,7 @@ protected static function parseAnnotations($docblock)
*
* @return void
*/
protected function log($level, $message, array $context = array())
protected function log($level, $message, array $context = array()): void
{
if ($this->logger) {
$this->logger->log($level, $message, $context);
Expand All @@ -981,7 +981,7 @@ protected function log($level, $message, array $context = array())
*
* @return void
*/
public function setLogger($logger)
public function setLogger($logger): void
{
$this->logger = $logger;
}
Expand Down