Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Added array map naming strategy #6197

Closed

Conversation

ojhaujjwal
Copy link
Contributor

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(array('name' => 'real_name'));
$hydrator->setNamingStrategy($strategy);

$user = new User();
$user->setName('Brad');

echo $hydrate->extract($user)['real_name']; // outputs Brad

@Ocramius
Copy link
Member

@ojhaujjwal could you also see if you can write documentation for this feature?

@ojhaujjwal
Copy link
Contributor Author

@Ocramius You mean here?

@Ocramius
Copy link
Member

@ojhaujjwal yeap

@ojhaujjwal
Copy link
Contributor Author

@Ocramius Consider it done 😄

@ojhaujjwal
Copy link
Contributor Author

@Ocramius But, there is no any docs for Naming Strategy.
There is one for Strategy, but not for Naming Strategy.

@localheinz
Copy link
Member

Please restart the build.

See #6205 (comment).

@Ocramius
Copy link
Member

Ocramius commented May 5, 2014

@ojhaujjwal correct, and we need docs, at least minimal ones.

@Ocramius Ocramius added this to the 2.4.0 milestone May 5, 2014
@ojhaujjwal
Copy link
Contributor Author

@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

@ojhaujjwal
Copy link
Contributor Author

@bakura @Ocramius The code I wrote in the last comment does not works because it not supported. The PR only supports two methods setExtractionMap and setHydrationMap. The above comment is RFC for adding a new method setTwoWayMap.

{
$this->setExtractionMap($extractionMap);
$this->setHydrationMap($hydrationMap);
}
Copy link
Contributor

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;
}

@texdc
Copy link
Contributor

texdc commented May 27, 2014

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.

@ojhaujjwal
Copy link
Contributor Author

@texdc If there are more people who think that it does not make any sense, then, i will remove that feature. That is there for more flexibility.

ping @Ocramius, @bakura10.

@texdc
Copy link
Contributor

texdc commented May 28, 2014

Flexibility for the sake of flexibility is unnecessary complexity. Only code what is necessary.

On May 28, 2014, at 1:06 AM, Ujjwal Ojha [email protected] wrote:

@texdc If there are more people who think that it does not make any sense, then, i will remove that feature. That is there for more flexibility.

ping @Ocramius, @bakura10.


Reply to this email directly or view it on GitHub.

@ojhaujjwal
Copy link
Contributor Author

Okay I will remove it if there are more people who think the same.

class ArrayMapNamingStrategy implements NamingStrategyInterface
{
/**
* @var array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string[]

@adamlundrigan
Copy link
Contributor

@ojhaujjwal what is the status of this?

I opened an issue on zf2-documentation regarding the lack of documentation for NamingStrategy (here)

@adamlundrigan
Copy link
Contributor

@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 setTwoWayMap....setFieldMapping, maybe? or just setFieldMap ?

@ojhaujjwal
Copy link
Contributor Author

@Ocramius @weierophinney What do you think about the use of single map rather than separate hydration and extraction maps?

@ojhaujjwal ojhaujjwal force-pushed the feature/array-map-strategy branch from e9ed390 to 2157715 Compare November 22, 2014 13:01
@ojhaujjwal
Copy link
Contributor Author

Ok, I created a single map. I realized that extracting and hydrating are meant to be exactly opposite.

@ojhaujjwal
Copy link
Contributor Author

Please do comment. :D

@Ocramius
Copy link
Member

@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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private IMO

@Ocramius Ocramius self-assigned this Dec 17, 2014
Ocramius added a commit that referenced this pull request Dec 28, 2014
Ocramius added a commit that referenced this pull request Dec 28, 2014
@Ocramius
Copy link
Member

@ojhaujjwal I've merged this manually into develop after rebasing and making the newly introduced class final, thanks!

develop: 59ed600

@Ocramius Ocramius closed this Dec 28, 2014
gianarb pushed a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
gianarb pushed a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants