1<?php
2
3declare(strict_types=1);
4
5namespace JMS\Serializer\Naming;
6
7use JMS\Serializer\Metadata\PropertyMetadata;
8
9/**
10 * Generic naming strategy which translates a camel-cased property name.
11 *
12 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
13 */
14final class CamelCaseNamingStrategy implements PropertyNamingStrategyInterface
15{
16    /**
17     * @var string
18     */
19    private $separator;
20
21    /**
22     * @var bool
23     */
24    private $lowerCase;
25
26    public function __construct(string $separator = '_', bool $lowerCase = true)
27    {
28        $this->separator = $separator;
29        $this->lowerCase = $lowerCase;
30    }
31
32    /**
33     * {@inheritDoc}
34     */
35    public function translateName(PropertyMetadata $property): string
36    {
37        $name = preg_replace('/[A-Z]+/', $this->separator . '\\0', $property->name);
38
39        if ($this->lowerCase) {
40            return strtolower($name);
41        }
42
43        return ucfirst($name);
44    }
45}
46