This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5364 from ronan-gloo/develop
Add Naming strategy for Hydrators
- Loading branch information
Showing
8 changed files
with
262 additions
and
37 deletions.
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
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
37 changes: 37 additions & 0 deletions
37
library/Zend/Stdlib/Hydrator/NamingStrategy/NamingStrategyInterface.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,37 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Stdlib\Hydrator\NamingStrategy; | ||
|
||
/** | ||
* Allow property extraction / hydration for hydrator | ||
* | ||
* Interface PropertyStrategyInterface | ||
* @package Zend\Stdlib\Hydrator\NamingStrategy | ||
*/ | ||
interface NamingStrategyInterface | ||
{ | ||
/** | ||
* Converts the given name so that it can be extracted by the hydrator. | ||
* | ||
* @param string $name The original name | ||
* @param object $object (optional) The original object for context. | ||
* @return mixed The hydrated name | ||
*/ | ||
public function hydrate($name); | ||
|
||
/** | ||
* Converts the given name so that it can be hydrated by the hydrator. | ||
* | ||
* @param string $name The original name | ||
* @param array $data (optional) The original data for context. | ||
* @return mixed The extracted name | ||
*/ | ||
public function extract($name); | ||
} |
47 changes: 47 additions & 0 deletions
47
library/Zend/Stdlib/Hydrator/NamingStrategy/UnderscoreNamingStrategy.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,47 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Stdlib\Hydrator\NamingStrategy; | ||
|
||
/** | ||
* Class UnderscoreNamingStrategy | ||
* @package Zend\Stdlib\Hydrator\NamingStrategy | ||
*/ | ||
class UnderscoreNamingStrategy implements NamingStrategyInterface | ||
{ | ||
/** | ||
* Remove underscores and capitalize letters | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public function hydrate($name) | ||
{ | ||
return preg_replace_callback('/(_[a-z])/i', function($letters) { | ||
$letter = substr(array_shift($letters), 1, 1); | ||
|
||
return ucfirst($letter); | ||
}, $name); | ||
} | ||
|
||
/** | ||
* Remove capitalized letters and prepend underscores. | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
public function extract($name) | ||
{ | ||
return preg_replace_callback('/([A-Z])/', function($letters) { | ||
$letter = array_shift($letters); | ||
|
||
return '_' . strtolower($letter); | ||
}, $name); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
library/Zend/Stdlib/Hydrator/NamingStrategyEnabledInterface.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,44 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Stdlib\Hydrator; | ||
|
||
use Zend\Stdlib\Hydrator\NamingStrategy\NamingStrategyInterface; | ||
|
||
interface NamingStrategyEnabledInterface | ||
{ | ||
/** | ||
* Adds the given naming strategy | ||
* | ||
* @param NamingStrategyInterface $strategy The naming to register. | ||
* @return NamingStrategyEnabledInterface | ||
*/ | ||
public function setNamingStrategy(NamingStrategyInterface $strategy); | ||
|
||
/** | ||
* Gets the naming strategy. | ||
* | ||
* @return NamingStrategyInterface | ||
*/ | ||
public function getNamingStrategy(); | ||
|
||
/** | ||
* Checks if a naming strategy exists. | ||
* | ||
* @return bool | ||
*/ | ||
public function hasNamingStrategy(); | ||
|
||
/** | ||
* Removes the naming with the given name. | ||
* | ||
* @return NamingStrategyEnabledInterface | ||
*/ | ||
public function removeNamingStrategy(); | ||
} |
Oops, something went wrong.