-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Added array map naming strategy #6197
Added array map naming strategy #6197
Conversation
ojhaujjwal
commented
Apr 26, 2014
@ojhaujjwal could you also see if you can write documentation for this feature? |
@ojhaujjwal yeap |
@Ocramius Consider it done 😄 |
@Ocramius But, there is no any docs for Naming Strategy. |
Please restart the build. See #6205 (comment). |
@ojhaujjwal correct, and we need docs, at least minimal ones. |
@Ocramius I am thinking of adding something. Hydration and extraction are quite symmetrical as @bakura10 said. So, I think that extraction and hydration map can be exactly opposite. class User
{
protected $name;
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
$hydrator = new Zend\Stdlib\Hydrator\ClassMethods;
$strategy = new Zend\Stdlib\Hydrator\NamingStrategy\ArrayMapNamingStrategy();
$strategy->setTwoWayMap(array('name' => 'real_name')); // this should be added
$hydrator->setNamingStrategy($strategy);
$user = new User();
$user->setName('Brad');
echo $hydrator->extract($user)['real_name']; // outputs Brad
$user = new User();
$hydrator->hydrate(['real_name' => 'Harry']);
echo $user->getName(); // outputs Harry |
{ | ||
$this->setExtractionMap($extractionMap); | ||
$this->setHydrationMap($hydrationMap); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For bi-directional mapping:
/**
* @param array $namingMap [string source => string destination] (hydration-oriented)
* @param bool $biDirectional reverse the namingMap for extraction
*/
public function __construct(array $namingMap, $biDirectional = false)
{
$this->hydrationMap = $namingMap;
$this->extractionMap = ($biDirectional) ? array_flip($namingMap) : $namingMap;
}
What's the use-case for 2 maps? // this doesn't make any sense to me
$strategy = new ArrayMapNamingStrategy(['foo' => 'bar'], ['baz' => 'bat']); The use-case I can think of for this class is for migrating old or external data. In which case, it's primarily one-directional. A single map suffices. If data is indeed moving both directions, the same single map is sufficient because it only needs to be flipped for the reverse direction. |
Flexibility for the sake of flexibility is unnecessary complexity. Only code what is necessary.
|
Okay I will remove it if there are more people who think the same. |
class ArrayMapNamingStrategy implements NamingStrategyInterface | ||
{ | ||
/** | ||
* @var array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string[]
@ojhaujjwal what is the status of this? I opened an issue on |
@ojhaujjwal I agree with the suggestion to use a single map rather than separate hydration and extraction maps; IMO the hydrate and extract methods should be complimentary, so if you hydrate an object and then extract it the result of the extract should be the same as the initial input to hydrate. My only suggestion there would be a better name than |
@Ocramius @weierophinney What do you think about the use of single map rather than separate hydration and extraction maps? |
e9ed390
to
2157715
Compare
Ok, I created a single map. I realized that extracting and hydrating are meant to be exactly opposite. |
Please do comment. :D |
@ojhaujjwal I am still thinking about all these new strategies, as I am very defensive against new features in general. The diff itself is quite clean. |
/** | ||
* @var string[] | ||
*/ | ||
protected $extractionMap = array(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private
IMO
@ojhaujjwal I've merged this manually into
|
…required to be exposed `private`
…ng-strategy' into develop Close zendframework/zendframework#6197