1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace BestServedCold\PhalueObjects;
|
4
|
|
|
|
5
|
|
|
use BestServedCold\PhalueObjects\Contract\Diffable;
|
6
|
|
|
use BestServedCold\PhalueObjects\Contract\VOStringable;
|
7
|
|
|
use BestServedCold\PhalueObjects\Exception\InvalidTypeException;
|
8
|
|
|
use BestServedCold\PhalueObjects\Contract\ValueObject as ValueObjectInterface;
|
9
|
|
|
use BestServedCold\PhalueObjects\VOString\Mixin;
|
10
|
|
|
|
11
|
|
|
/**
|
12
|
|
|
* Class String
|
13
|
|
|
*
|
14
|
|
|
* @package BestServedCold\PhalueObjects
|
15
|
|
|
* @author Adam Lewis <[email protected]>
|
16
|
|
|
* @copyright Copyright (c) 2015 Best Served Cold Media Limited
|
17
|
|
|
* @license http://http://opensource.org/licenses/GPL-3.0 GPL License
|
18
|
|
|
* @link http://bestservedcold.com
|
19
|
|
|
* @since 0.0.1-alpha
|
20
|
|
|
* @version 0.0.2-alpha
|
21
|
|
|
*/
|
22
|
|
|
class VOString extends ValueObject implements VOStringable, \Countable, Diffable
|
23
|
|
|
{
|
24
|
|
|
use Mixin;
|
25
|
|
|
|
26
|
|
|
/**
|
27
|
|
|
* VOString constructor.
|
28
|
|
|
*
|
29
|
|
|
* @param $value
|
30
|
|
|
* @throws InvalidTypeException
|
31
|
|
|
*/
|
32
|
61 |
|
public function __construct($value)
|
33
|
|
|
{
|
34
|
61 |
|
if (! is_string($value)) {
|
35
|
1 |
|
throw new InvalidTypeException($value, ['string']);
|
36
|
|
|
}
|
37
|
|
|
|
38
|
61 |
|
parent::__construct($value);
|
39
|
61 |
|
}
|
40
|
|
|
|
41
|
|
|
/**
|
42
|
|
|
* @return string
|
43
|
|
|
*/
|
44
|
56 |
|
public function getValue()
|
45
|
|
|
{
|
46
|
56 |
|
return (string) parent::getValue();
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* @return string
|
51
|
|
|
*/
|
52
|
10 |
|
public function toString()
|
53
|
|
|
{
|
54
|
10 |
|
return $this->getValue();
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
/**
|
58
|
|
|
* @param string $string
|
59
|
|
|
* @return static
|
60
|
|
|
*/
|
61
|
30 |
|
public static function fromString($string)
|
62
|
|
|
{
|
63
|
30 |
|
return new static($string);
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
/**
|
67
|
|
|
* @return int
|
68
|
|
|
*/
|
69
|
|
|
public function count()
|
70
|
|
|
{
|
71
|
|
|
return strlen($this->getValue());
|
72
|
|
|
}
|
73
|
|
|
|
74
|
|
|
/**
|
75
|
|
|
* @param ValueObjectInterface $object
|
76
|
|
|
* @return static
|
77
|
|
|
*/
|
78
|
|
|
public function diff(ValueObjectInterface $object)
|
79
|
|
|
{
|
80
|
|
|
return new static(str_replace($object->getValue(), '', $this->getValue()));
|
81
|
|
|
}
|
82
|
|
|
|
83
|
|
|
/**
|
84
|
|
|
* @return string
|
85
|
|
|
*/
|
86
|
3 |
|
public function getNumbers()
|
87
|
|
|
{
|
88
|
3 |
|
return preg_replace('/[^0-9]+/', '', $this->getValue());
|
89
|
|
|
}
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* @return string
|
93
|
|
|
*/
|
94
|
3 |
|
public function getLetters()
|
95
|
|
|
{
|
96
|
3 |
|
return preg_replace('/[^A-Za-z]+/', '', $this->getValue());
|
97
|
|
|
}
|
98
|
|
|
}
|
99
|
|
|
|