Skip to content

Commit f2377ed

Browse files
committed
Initial commit.
0 parents  commit f2377ed

18 files changed

+554
-0
lines changed

.gitignore

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

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
language: php
2+
php:
3+
- '5.4'
4+
- '5.5'
5+
- '5.6'
6+
- '7.0'
7+
- hhvm
8+
- nightly
9+
10+
install:
11+
- composer self-update
12+
- composer install
13+
14+
script:
15+
- ./bin/phing unit

LICENSE

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

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Exceptions with format string
2+
3+
This package provides exceptions with messages according a format string like
4+
[sprintf](http://php.net/manual/en/function.sprintf.php).

build.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project name="php-affirm" default="build" basedir=".">
3+
<target name="build">
4+
</target>
5+
6+
<!-- Runs all unit tests -->
7+
<target name="unit">
8+
<exec command="bin/phpunit --bootstrap=test/bootstrap.php test" passthru="true" checkreturn="true"/>
9+
</target>
10+
</project>

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "setbased/php-affirm",
3+
"description": "Exceptions with format string",
4+
"keywords": [
5+
"Exception"
6+
],
7+
"type": "library",
8+
"license": "MIT",
9+
"require": {
10+
"php": ">=5.4.0"
11+
},
12+
"require-dev": {
13+
"phing/phing": "2.*",
14+
"phpunit/phpunit": ">=4.0 <6.0"
15+
},
16+
"autoload": {
17+
"psr-4": {
18+
"SetBased\\Exception\\": "src/"
19+
}
20+
},
21+
"config": {
22+
"bin-dir": "bin"
23+
}
24+
}

src/ErrorException.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
//----------------------------------------------------------------------------------------------------------------------
3+
namespace SetBased\Exception;
4+
5+
//----------------------------------------------------------------------------------------------------------------------
6+
/**
7+
* Class for PHP errors.
8+
*/
9+
class ErrorException extends \ErrorException implements NamedException
10+
{
11+
protected static $ourNames = [E_COMPILE_ERROR => 'PHP Compile Error',
12+
E_COMPILE_WARNING => 'PHP Compile Warning',
13+
E_CORE_ERROR => 'PHP Core Error',
14+
E_CORE_WARNING => 'PHP Core Warning',
15+
E_DEPRECATED => 'PHP Deprecated Warning',
16+
E_ERROR => 'PHP Fatal Error',
17+
E_NOTICE => 'PHP Notice',
18+
E_PARSE => 'PHP Parse Error',
19+
E_RECOVERABLE_ERROR => 'PHP Recoverable Error',
20+
E_STRICT => 'PHP Strict Warning',
21+
E_USER_DEPRECATED => 'PHP User Deprecated Warning',
22+
E_USER_ERROR => 'PHP User Error',
23+
E_USER_NOTICE => 'PHP User Notice',
24+
E_USER_WARNING => 'PHP User Warning',
25+
E_WARNING => 'PHP Warning'];
26+
27+
//--------------------------------------------------------------------------------------------------------------------
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function getName()
32+
{
33+
return isset(self::$ourNames[$this->getCode()]) ? self::$ourNames[$this->getCode()] : 'Error';
34+
}
35+
36+
//--------------------------------------------------------------------------------------------------------------------
37+
}
38+
39+
//----------------------------------------------------------------------------------------------------------------------

src/FallenException.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
//----------------------------------------------------------------------------------------------------------------------
3+
namespace SetBased\Exception;
4+
5+
//----------------------------------------------------------------------------------------------------------------------
6+
/**
7+
* Class for situations where PHP code has fallen through a switch statement or a combination of if-elseif statements.
8+
*/
9+
class FallenException extends RuntimeException
10+
{
11+
//--------------------------------------------------------------------------------------------------------------------
12+
/**
13+
* Object constructor.
14+
*
15+
* @param string $theName The name or description of the variable of expression.
16+
* @param string $theValue The actual value that.
17+
*
18+
* Example:
19+
* ```
20+
* $size = 'xxl';
21+
* switch ($size)
22+
* {
23+
* case 'S':
24+
* echo 'small';
25+
* break;
26+
*
27+
* case 'M':
28+
* echo 'medium';
29+
* break;
30+
*
31+
* case 'L':
32+
* echo 'small';
33+
* break;
34+
*
35+
* default:
36+
* throw new FallenException('size', $size);
37+
* }
38+
* ```
39+
*/
40+
public function __construct($theName, $theValue)
41+
{
42+
parent::__construct("Unknown or unexpected value '%s' for '%s'.", $theValue, $theName);
43+
}
44+
45+
//--------------------------------------------------------------------------------------------------------------------
46+
}
47+
48+
//----------------------------------------------------------------------------------------------------------------------

src/FormattedException.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
//----------------------------------------------------------------------------------------------------------------------
3+
namespace SetBased\Exception;
4+
5+
//----------------------------------------------------------------------------------------------------------------------
6+
trait FormattedException
7+
{
8+
//--------------------------------------------------------------------------------------------------------------------
9+
/**
10+
* Object constructor.
11+
*
12+
* @param string|array $format The format string of the error message, see
13+
* [sprintf](http://php.net/manual/function.sprintf.php).
14+
* @param mixed $param,... The arguments for the format string. However, if the first argument is an exception
15+
* it will be used as the [previous](http://php.net/manual/exception.construct.php)
16+
* exception for the exception chaining.
17+
*/
18+
public function __construct($format)
19+
{
20+
$code = 0;
21+
$previous = null;
22+
23+
$args = func_get_args();
24+
array_shift($args);
25+
26+
if (is_array($format))
27+
{
28+
$special = $format;
29+
$format = array_shift($args);
30+
31+
if (isset($special[0]))
32+
{
33+
if ($special[0] instanceof \Exception) $previous = $special[0];
34+
elseif (is_int($special[0])) $code = $special[0];
35+
}
36+
37+
if (isset($special[1]))
38+
{
39+
if ($special[1] instanceof \Exception) $previous = $special[1];
40+
elseif (is_int($special[1])) $code = $special[1];
41+
}
42+
}
43+
44+
parent::__construct(vsprintf($format, $args), $code, $previous);
45+
}
46+
47+
//--------------------------------------------------------------------------------------------------------------------
48+
}
49+
50+
//----------------------------------------------------------------------------------------------------------------------

src/LogicException.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
//----------------------------------------------------------------------------------------------------------------------
3+
namespace SetBased\Exception;
4+
5+
//----------------------------------------------------------------------------------------------------------------------
6+
/**
7+
* Parent class for errors in the program logic.
8+
*/
9+
class LogicException extends \LogicException implements NamedException
10+
{
11+
//--------------------------------------------------------------------------------------------------------------------
12+
use FormattedException;
13+
14+
//--------------------------------------------------------------------------------------------------------------------
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function getName()
19+
{
20+
return 'Programming Error';
21+
}
22+
23+
//--------------------------------------------------------------------------------------------------------------------
24+
}
25+
26+
//----------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)