Documentation

UnderscoreNamingStrategy — Zend Framework 2 2.4.9 documentation

Zend\Stdlib

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.

Basic Usage

1
2
3
4
5
6
7
8
9
 $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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 class Foo
 {
     public $fooBar;
 }

 $hydrator = new Zend\Stdlib\Hydrator\ObjectProperty();
 $hydrator->setNamingStrategy(new Zend\Stdlib\Hydrator\NamingStrategy\UnderscoreNamingStrategy());

 $foo = new Foo();
 $hydrator->hydrate(array('foo_bar' => 123), $foo);

 print_r($foo); // Foo Object ( [fooBar] => 123 )
 print_r($hydrator->extract($foo)); // Array ( [foo_bar] => 123 )

Copyright

© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts