This repository has been archived by the owner on Feb 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCharacterSet.php
124 lines (109 loc) · 2.84 KB
/
PCharacterSet.php
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 Poundation;
require_once 'PObject.php';
require_once 'PSet.php';
require_once 'PString.php';
use Poundation\PObject;
/**
* @abstract A character set holds single characters used for splitting, comparing and trimming and is strongly related to strings.
* @author danielkbx
*
*/
class PCharacterSet extends PObject {
/**
* @var PSet
*/
private $_characters;
function __construct() {
$this->_characters = new PSet();
}
function __toString() {
return PString::stringWithArray($this->_characters,'')->__toString();
}
/**
* Returns a new character set pre-setup with whitespace characters.
* @return \Poundation\PCharacterSet
*/
static function whitespaceCharacterSet() {
$set = new PCharacterSet();
$set->addCharacter(' ');
$set->addCharacter("\n");
$set->addCharacter("\t");
$set->addCharacter("\r");
$set->addCharacter("\0");
$set->addCharacter("\x0B");
return $set;
}
/**
* Creates a new character set with all characters from the given string.
* @param $string
* @return PCharacterSet
*/
static function characterSetWithCharactersFromString($string) {
$set = new self();
$set->addCharactersFromString($string);
return $set;
}
/**
* Adds a single character to the character set.
* @param string $char
* @return $this
* @throws \Exception
*/
function addCharacter($char) {
$string = '';
if (is_string($char)) {
$string = $char;
} else if (is_object($char)) {
if ($char instanceof PString) {
$string = $char->__toString();
}
} else {
throw new \Exception('Only string values accepted.',108,null);
}
$length = strlen($string);
if ($length == 0) {
throw new \Exception('Cannot add empty string to character set.',109,null);
} else if ($length > 1) {
throw new \Exception('Can only add single characters to character set.',109,null);
} else {
$this->_characters->add($string);
}
return $this;
}
/**
* Adds all characters in a string to the character set.
* @param string $string
* @return $this
* @throws \Exception
*/
function addCharactersFromString($string) {
if (is_string($string)) {
$characters = PString::createFromString($string);
} else if (is_object($string)) {
if ($string instanceof PString) {
$characters = $string;
}
} else {
throw new \Exception('Only string values accepted.',108,null);
}
$length = $characters->length();
if ($length == 0) {
throw new \Exception('Cannot add empty string to character set.',109,null);
} else {
$_this = $this;
$characters->iterateByCharacter(function($char) use (&$_this) {
$_this->addCharacter($char);
});
}
return $this;
}
/**
* Returns the set of characters.
* @return PSet
*/
function set() {
return PSet::create($this->_characters);
}
}
?>