Skip to content

Commit f6034a8

Browse files
committed
Initial commit
0 parents  commit f6034a8

File tree

7 files changed

+259
-0
lines changed

7 files changed

+259
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vendor/
2+
bin/
3+
composer.lock
4+
.DS_Store
5+
.idea

.travis.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: php
2+
3+
php:
4+
- 5.3
5+
- 5.4
6+
- 5.5
7+
8+
before_script:
9+
- curl -s http://getcomposer.org/installer | php
10+
- php composer.phar --dev install
11+
12+
script: phpspec r -v

LICENCE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2014 Michal Dabrowski, Norbert Orzechowicz
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# PHP To String
2+
3+
Simple library that allows you to cast any php value into string
4+
5+
##Installation
6+
7+
Add to your composer.json
8+
9+
```
10+
require: {
11+
"coduo/php-to-string": "dev-master"
12+
}
13+
```
14+
15+
##Usage
16+
17+
Supported types:
18+
19+
* string
20+
* integer
21+
* float/double
22+
* objec
23+
* array
24+
* resource
25+
26+
```php
27+
$double = new \Coduo\ToString\String(1.12312);
28+
echo $double; // "1.12312"
29+
30+
$datetime = new \Coduo\ToString\String(new \DateTime());
31+
echo $datetime; // "\DateTime"
32+
33+
$array = new \Coduo\ToString\String(array('foo', 'bar', 'baz'));
34+
echo $array; // "Array(3)"
35+
36+
$res = fopen(sys_get_temp_dir() . "/foo", "w");
37+
$resource = new \Coduo\ToString\String($res);
38+
echo $resource; // "Resource(stream)"
39+
40+
```

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "coduo/php-to-string",
3+
"type": "library",
4+
"keywords": ["string", "php", "to string", "to"],
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Michał Dąbrowski",
9+
"email": "dabrowski@brillante.pl"
10+
},
11+
{
12+
"name": "Norbert Orzechowicz",
13+
"email": "norbert@orzechowicz.pl"
14+
}
15+
],
16+
"require": {
17+
"php": ">=5.3.0"
18+
},
19+
"require-dev": {
20+
"phpspec/phpspec": "2.0.*"
21+
},
22+
"autoload": {
23+
"psr-0": {"" : "src"}
24+
},
25+
"config": {
26+
"bin-dir": "bin"
27+
},
28+
"extra": {
29+
"branch-alias": {
30+
"dev-master": "1.0-dev"
31+
}
32+
}
33+
}

spec/Coduo/ToString/StringSpec.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace spec\Coduo\ToString;
4+
5+
use PhpSpec\ObjectBehavior;
6+
7+
class Foo
8+
{
9+
public function __toString()
10+
{
11+
return 'This is Foo';
12+
}
13+
}
14+
15+
class StringSpec extends ObjectBehavior
16+
{
17+
function it_convert_double_to_string()
18+
{
19+
$this->beConstructedWith(1.1);
20+
$this->__toString()->shouldReturn('1.1');
21+
}
22+
23+
function it_convert_double_to_string_for_specific_locale()
24+
{
25+
$this->beConstructedWith(1.1, 'pl');
26+
$this->__toString()->shouldReturn('1,1');
27+
}
28+
29+
function it_convert_integer_to_string()
30+
{
31+
$this->beConstructedWith(20);
32+
$this->__toString()->shouldReturn('20');
33+
}
34+
35+
function it_convert_boolean_to_string()
36+
{
37+
$this->beConstructedWith(true);
38+
$this->__toString()->shouldReturn('true');
39+
}
40+
41+
function it_convert_object_to_string()
42+
{
43+
$this->beConstructedWith(new \stdClass());
44+
$this->__toString()->shouldReturn('\stdClass');
45+
}
46+
47+
function it_convert_object_with_toString_method_to_string()
48+
{
49+
$this->beConstructedWith(new Foo());
50+
$this->__toString()->shouldReturn('This is Foo');
51+
}
52+
53+
function it_convert_array_to_string()
54+
{
55+
$this->beConstructedWith(array('foo', 'bar'));
56+
$this->__toString()->shouldReturn('Array(2)');
57+
}
58+
59+
function it_convert_resource_to_string()
60+
{
61+
$resource = fopen(sys_get_temp_dir() . "/foo", "w");
62+
$this->beConstructedWith($resource);
63+
$this->__toString()->shouldReturn('Resource(stream)');
64+
fclose($resource);
65+
unlink(sys_get_temp_dir() . "/foo");
66+
}
67+
68+
function it_convert_callback_string()
69+
{
70+
$func = function() {return 'test';};
71+
$this->beConstructedWith($func);
72+
$this->__toString()->shouldReturn('\Closure');
73+
}
74+
}

src/Coduo/ToString/String.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Coduo\ToString;
4+
5+
class String
6+
{
7+
/**
8+
* @var mixed
9+
*/
10+
private $value;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $locale;
16+
17+
/**
18+
* @param mixed $value
19+
* @param string $locale
20+
*/
21+
public function __construct($value, $locale = 'en')
22+
{
23+
$this->value = $value;
24+
$this->locale = $locale;
25+
}
26+
27+
public function __toString()
28+
{
29+
$type = gettype($this->value);
30+
switch ($type) {
31+
case 'float':
32+
case 'double':
33+
return $this->castDoubleToString();
34+
case 'boolean':
35+
return $this->castBooleanToString();
36+
case 'object':
37+
return $this->castObjectToString();
38+
case 'array':
39+
return sprintf('Array(%d)', count($this->value));
40+
case 'resource':
41+
return sprintf("Resource(%s)", get_resource_type($this->value));
42+
default:
43+
return (string) $this->value;
44+
}
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
private function castObjectToString()
51+
{
52+
return (method_exists($this->value, '__toString'))
53+
? (string)$this->value
54+
: '\\' . get_class($this->value);
55+
}
56+
57+
/**
58+
* @return string
59+
*/
60+
private function castBooleanToString()
61+
{
62+
return ($this->value) ? 'true' : 'false';
63+
}
64+
65+
/**
66+
* @return string
67+
*/
68+
private function castDoubleToString()
69+
{
70+
$formatter = new \NumberFormatter($this->locale, \NumberFormatter::PATTERN_DECIMAL);
71+
return $formatter->format($this->value);
72+
}
73+
}

0 commit comments

Comments
 (0)