-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. moved information on properties outside of the HydratorMethodsVisitor into the HydratorGenerator 2. moved exception code into it's own class InvalidOptionException 3. moved option parsing from HydratorMethodsVisitor to value object AllowedPropertiesOption 4. small fix where a null would not be hydrated (in case the object itself has another default value)
- Loading branch information
Showing
5 changed files
with
314 additions
and
154 deletions.
There are no files selected for viewing
155 changes: 155 additions & 0 deletions
155
src/GeneratedHydrator/ClassGenerator/AllowedPropertiesOption.php
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,155 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the MIT license. | ||
*/ | ||
|
||
namespace GeneratedHydrator\ClassGenerator; | ||
|
||
use GeneratedHydrator\Exception\InvalidOptionException; | ||
use ReflectionClass; | ||
|
||
/** | ||
* Generator for highly performing {@see \Zend\Stdlib\Hydrator\HydratorInterface} | ||
* for objects | ||
* | ||
* {@inheritDoc} | ||
* | ||
* @author Marco Pivetta <[email protected]> | ||
* @license MIT | ||
*/ | ||
class AllowedPropertiesOption { | ||
/** | ||
* When this option is passed, only the properties in the given array are | ||
* hydrated and extracted. | ||
*/ | ||
const OPTION_ALLOWED_PROPERTIES = 'allowedProperties'; | ||
|
||
/** | ||
* @var PropertyAccessor[] | ||
*/ | ||
private $propertyNames; | ||
|
||
/** | ||
* @var array Holds configuration for the object properties. | ||
*/ | ||
private $allowedProperties; | ||
|
||
public function __construct(ReflectionClass $reflectedClass, array $options) { | ||
$this->propertyNames = array_map(function($prop) { | ||
return $prop->name; | ||
}, $reflectedClass->getProperties()); | ||
|
||
$this->allowedProperties = $this->expandAllowedProperties($options); | ||
} | ||
|
||
/** | ||
* Returns an array with properties as keys and hydrate/extract information | ||
* as values. | ||
* | ||
* @param array $options | ||
*/ | ||
private function expandAllowedProperties($options) | ||
{ | ||
$allowedProperties = []; | ||
|
||
// Option was not given | ||
if (! isset($options[static::OPTION_ALLOWED_PROPERTIES])) { | ||
foreach ($this->propertyNames as $propertyName) { | ||
$allowedProperties[$propertyName] = [ | ||
'extract' => true, | ||
'hydrate' => true | ||
]; | ||
} | ||
|
||
return $allowedProperties; | ||
} | ||
|
||
if (! is_array($options[static::OPTION_ALLOWED_PROPERTIES])) { | ||
throw InvalidOptionException::valueNotArray(gettype($options[static::OPTION_ALLOWED_PROPERTIES])); | ||
} | ||
|
||
// Option was given | ||
foreach ($options[static::OPTION_ALLOWED_PROPERTIES] as $k => $v) { | ||
// simple format | ||
if (is_int($k)) { | ||
$this->makeSimpleFormat($k, $v, $allowedProperties); | ||
|
||
continue; | ||
} | ||
|
||
// advanced format | ||
if (is_string($k)) { | ||
$this->makeAdvancedFormat($k, $v, $allowedProperties); | ||
} | ||
} | ||
|
||
// Disable all properties which are not specified in the allowedProperties | ||
foreach ($this->propertyNames as $propertyName) { | ||
if (! in_array($propertyName, array_keys($allowedProperties))) { | ||
$allowedProperties[$propertyName] = [ | ||
'extract' => false, | ||
'hydrate' => false | ||
]; | ||
} | ||
} | ||
|
||
return $allowedProperties; | ||
} | ||
|
||
private function makeSimpleFormat($key, $value, &$allowedProperties) { | ||
if (! is_string($value)) { | ||
throw InvalidOptionException::invalidValueExpectedString(gettype($value), $key); | ||
} | ||
|
||
if (in_array($value, array_keys($allowedProperties))) { | ||
throw InvalidOptionException::doubleProperty($value); | ||
} | ||
|
||
$allowedProperties[$value] = [ | ||
'extract' => true, | ||
'hydrate' => true | ||
]; | ||
} | ||
|
||
private function makeAdvancedFormat($key, $value, &$allowedProperties) { | ||
if (! is_array($value)) { | ||
throw InvalidOptionException::arrayExpected($k, gettype($value)); | ||
} | ||
|
||
if (in_array($key, $allowedProperties)) { | ||
throw InvalidOptionException::doubleProperty($value); | ||
} | ||
|
||
$validateOptionConfigurationKey = function($property, $array, $key) { | ||
if (! isset($array[$key])) { | ||
throw InvalidOptionException::missingKey($property, $key); | ||
} | ||
|
||
if (! in_array($array[$key], [true, false, 'optional'])) { | ||
throw InvalidOptionException::invalidValue($property, $key); | ||
} | ||
}; | ||
|
||
$validateOptionConfigurationKey($key, $value, 'extract'); | ||
$validateOptionConfigurationKey($key, $value, 'hydrate'); | ||
|
||
$allowedProperties[$key] = $value; | ||
} | ||
|
||
public function getAllowedProperties() { | ||
return $this->allowedProperties; | ||
} | ||
} |
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.