1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Util |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf\Util; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Identifier util for unified resource generation. |
13
|
|
|
* |
14
|
|
|
* <code> |
15
|
|
|
* $identifier = new class Identifier(1, '23', 123, 'ZZ-TOP', 'Some_Class_name'); |
16
|
|
|
* |
17
|
|
|
* print $identifier; // --> '1_23_123_zz_top_some_class_name' |
18
|
|
|
* |
19
|
|
|
* $identifier->setDelimiter('/'); |
20
|
|
|
* |
21
|
|
|
* print $identifier->generate(); // --> '1/23/123/zz/top/some/class/name' |
22
|
|
|
* </code> |
23
|
|
|
* |
24
|
|
|
* @package Util |
25
|
|
|
* @author Gjero Krsteski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Identifier |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected static $delimiter = '_'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $args = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new Cache Identifier based on the given parameters. |
41
|
|
|
* Integer and string but not array and objects. |
42
|
|
|
* |
43
|
|
|
* @throws \BadMethodCallException If no identifiers received. |
44
|
|
|
*/ |
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
$args = func_get_args(); |
48
|
|
|
|
49
|
|
|
if (!count($args) || !implode('', $args)) { |
50
|
|
|
throw new \BadMethodCallException('No identifiers received'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->args = $args; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return String representation of this Cache Identifier. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function __toString() |
62
|
|
|
{ |
63
|
|
|
return $this->generate(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function generate() |
70
|
|
|
{ |
71
|
|
|
return (string)$this->slag(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Slags the identifier. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
protected function slag() |
80
|
|
|
{ |
81
|
|
|
$ident = str_replace('-', '_', implode(self::getDelimiter(), $this->args)); |
82
|
|
|
$ident = str_replace('_', self::getDelimiter(), $ident); |
83
|
|
|
$ident = trim($ident); |
84
|
|
|
$ident = str_replace(' ', '', $ident); |
85
|
|
|
|
86
|
|
|
return strip_tags(strtolower($ident)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set the delimiter used to create a Cache Identifier. |
91
|
|
|
* |
92
|
|
|
* @param string $delimiter The delimiter character. |
93
|
|
|
*/ |
94
|
|
|
public function setDelimiter($delimiter) |
95
|
|
|
{ |
96
|
|
|
self::$delimiter = $delimiter; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the delimiter used to create a Cache Identifier. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getDelimiter() |
105
|
|
|
{ |
106
|
|
|
return self::$delimiter; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|