1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\BusinessEntityBundle\Converter; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
6
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessProperty; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Parameter Converter |
10
|
|
|
* ref: victoire_business_entity.converter.parameter_converter. |
11
|
|
|
*/ |
12
|
|
|
class ParameterConverter |
13
|
|
|
{ |
14
|
|
|
/** |
|
|
|
|
15
|
|
|
* Replace the code string with the value of the entity attribute. |
16
|
|
|
* |
17
|
|
|
* @param BusinessProperty $businessProperty |
|
|
|
|
18
|
|
|
* @param object $entity |
|
|
|
|
19
|
|
|
* |
20
|
|
|
* @throws \Exception |
21
|
|
|
* |
22
|
|
|
* @return string The updated string |
23
|
|
|
*/ |
24
|
|
|
public function convertFromEntity($url, BusinessProperty $businessProperty, $entity) |
25
|
|
|
{ |
26
|
|
|
//test parameters |
27
|
|
|
if ($entity === null) { |
28
|
|
|
throw new \Exception('The parameter entity can not be null'); |
29
|
|
|
} |
30
|
|
|
//the attribute to set |
31
|
|
|
$entityProperty = $businessProperty->getName(); |
32
|
|
|
|
33
|
|
|
//the value of the attribute |
34
|
|
|
$accessor = new PropertyAccessor(); |
35
|
|
|
$attributeValue = $accessor->getValue($entity, $entityProperty); |
36
|
|
|
|
37
|
|
|
return $this->convert($url, 'item.'.$entityProperty, $attributeValue); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
|
|
|
|
41
|
|
|
* Replace the code string with the value of the entity attribute. |
42
|
|
|
* |
43
|
|
|
* @param string $entityProperty |
|
|
|
|
44
|
|
|
* @param string $attributeValue |
|
|
|
|
45
|
|
|
* |
46
|
|
|
* @throws \Exception |
47
|
|
|
* |
48
|
|
|
* @return string The updated string |
49
|
|
|
*/ |
50
|
|
|
public function convert($url, $entityProperty, $attributeValue) |
51
|
|
|
{ |
52
|
|
|
//the string to replace |
53
|
|
|
$stringToReplace = '{{'.$entityProperty.'}}'; |
54
|
|
|
|
55
|
|
|
//we provide a default value |
56
|
|
|
if ($attributeValue === null) { |
57
|
|
|
$attributeValue = ''; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
//we replace the string |
61
|
|
|
return str_replace($stringToReplace, $attributeValue, $url); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|