-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringTransformer.php
More file actions
executable file
·124 lines (110 loc) · 2.83 KB
/
StringTransformer.php
File metadata and controls
executable file
·124 lines (110 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php namespace WebDev\Conventional;
use Exception;
use WebDev\Conventional\Exception\ResolverException;
/**
* String Transformer
*
* @author Josiah <josiah@web-dev.com.au>
*/
class StringTransformer
{
public function __construct($string,$object)
{
$this->string = $string;
$this->object = $object;
}
public function __toString()
{
try
{
return $this();
}
catch(Exception $exception)
{
return $this->string;
}
}
/**
* Invokes the string resolver transformation
*
* @return string Resolt of the transformation
*/
public function __invoke()
{
$resolver = new Resolver();
$pos = 0;
$result = "";
do
{
// Start Delmiter
$start = strpos($this->string,$this->startDelimiter,$pos);
if($start === false)
{
$result .= substr($this->string,$pos);
$pos = false;
continue;
}
// End Delmiter
$end = strpos($this->string,$this->endDelmiter,$start);
if($end === false)
{
$result .= substr($this->string,$pos);
$pos = false;
continue;
}
// Resolve captured string
$result.= substr($this->string,$pos,$start-$pos);
$length = $end - $start;
$path = substr($this->string,$start+1,$length-1);
if($this->getThrowExceptions())
{
$result .= $resolver->get($this->object,$path);
}
else
{
try
{
$result .= $resolver->get($this->object,$path);
}
catch(ResolverException $exception)
{
$result .= substr($this->string,$start,$length+1);
}
}
$pos = $end+1;
} while($pos !== false);
return $result;
}
/**
* Delmiter that indicates the start of a transformation path
*
* @var string
*/
protected $startDelimiter = "{";
/**
* Delmiter that indicates the end of a transformation path
*
* @var string
*/
protected $endDelmiter = "}";
/**
* String to resolve using the object
*
* @var string
*/
protected $string;
/**
* Object to use when resolving the string
*
* @var mixed
*/
protected $object;
/**
* Indicates whether resolver exceptions should be thrown
*
* @var bool
*/
protected $throwExceptions;
public function setThrowExceptions($value){ $this->throwExceptions = $value; }
public function getThrowExceptions(){ return $this->throwExceptions; }
}