Skip to content
This repository has been archived by the owner on May 24, 2018. It is now read-only.

Commit

Permalink
Added Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy doc
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanS authored and NathanS committed May 6, 2015
1 parent 17bccb5 commit 80dcf76
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/src/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ Programmer's Reference Guide of Zend Framework 2
modules/zend.stdlib.hydrator.filter
modules/zend.stdlib.hydrator.strategy
modules/zend.stdlib.hydrator.aggregate
modules/zend.stdlib.hydrator.namingstrategy.underscorenamingstrategy
modules/zend.tag.introduction
modules/zend.tag.cloud
modules/zend.test.introduction
Expand Down Expand Up @@ -896,6 +897,11 @@ Zend\\Stdlib
* :doc:`modules/zend.stdlib.hydrator.strategy`
* :doc:`modules/zend.stdlib.hydrator.aggregate`

Zend\\Stdlib\\Hydrator\\NamingStrategy
^^^^^^^^^^^^

* :doc:`modules/zend.stdlib.hydrator.namingstrategy.underscorenamingstrategy`

.. _zend.tag:

Zend\\Tag
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.. _zend.stdlib.hydrator.namingstrategy.underscorenamingstrategy:

UnderscoreNamingStrategy
========================

``Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy`` Converts snake case strings (e.g. foo_bar_baz) to studly case strings (e.g. fooBarBaz) and vice versa.

Example Usage
-------------

.. code:: php
<?php
$namingStrategy = new \Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy();
echo $namingStrategy->extract('foo_bar'); // outputs: foo_bar
echo $namingStrategy->extract('Foo_Bar'); // outputs: foo_bar
echo $namingStrategy->extract('FooBar'); // outputs: foo_bar
echo $namingStrategy->hydrate('fooBar'); // outputs: fooBar
echo $namingStrategy->hydrate('FooBar'); // outputs: fooBar
echo $namingStrategy->hydrate('Foo_Bar'); // outputs: fooBar
?>
This strategy can be used in hydrators to dictate how keys should be mapped.

.. code:: php
<?php
class Foo {
public $fooBar;
}
$hydrator = new \Zend\Stdlib\Hydrator\ObjectProperty();
$hydrator->setNamingStrategy(new \Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy());
$foo = new Foo();
$hydrator->hydrate(['foo_bar' => 123],$foo);
print_r($foo); // Foo Object ( [fooBar] => 123 )
print_r($hydrator->extract($foo)); // Array ( [foo_bar] => 123 )
?>

0 comments on commit 80dcf76

Please sign in to comment.