Skip to content

Commit bec94dc

Browse files
committed
Null Objects - Basic usage.
0 parents  commit bec94dc

File tree

9 files changed

+249
-0
lines changed

9 files changed

+249
-0
lines changed

.editorconfig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
# Matches multiple files with brace expansion notation
13+
# Set default charset
14+
[*]
15+
charset = utf-8
16+
17+
# 4 space indentation
18+
[*]
19+
indent_style = space
20+
indent_size = 4
21+
22+
# Matches the exact files either package.json or .travis.yml
23+
[{package.json,.travis.yml}]
24+
indent_style = space
25+
indent_size = 2

.gitignore

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

LICENSE.md

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 Rajdeep Tarat <[email protected]>
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Null Object for PHP
2+
3+
Allows the user to call the null object which handles all calls silently and returns sensible defaults.
4+
The returned object can be type cast to return the following values:
5+
6+
- String - ""
7+
- Integer - 0
8+
- Float or Real - 0.0
9+
- Array - []
10+
- Boolean - False
11+
12+
## Installation
13+
14+
Run the following to install:
15+
16+
```composer require conceptbyte/null-object```
17+
18+
## Usage
19+
The null object can be created in one of the following methods:
20+
- Named constructor: ```NullObject::create()```
21+
- Helper function: ```no()```
22+
23+
Both will return an instance of the class.
24+
25+
## Quirks
26+
PHP does not allow modifying the behaviour of casting a class to boolean.
27+
A simple hack is to use the SimpleXMLElement class which can return false when passed
28+
an empty XML tag.

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "conceptbyte/null-object",
3+
"description": "A Null Object for PHP.",
4+
"keywords": ["null-object", "null-object-pattern", "null"],
5+
"type": "package",
6+
"license": "MIT",
7+
"authors": [
8+
{
9+
"name": "Rajdeep Tarat",
10+
"email": "[email protected]",
11+
"homepage": "http://rajdeeptarat.com"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.5.9"
16+
},
17+
"require-dev": {
18+
"phpunit/phpunit": "^5.4"
19+
},
20+
"autoload": {
21+
"psr-4": {
22+
"ConceptByte\\NullObject\\": "src"
23+
},
24+
"files": [
25+
"src/helpers.php"
26+
]
27+
},
28+
"minimum-stability": "stable"
29+
}

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Null Object Test Suite">
14+
<directory suffix=".php">./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>

src/NullObject.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace ConceptByte\NullObject;
4+
5+
class NullObject extends \SimpleXMLElement
6+
{
7+
8+
/**
9+
* Named constructor to create the class
10+
*
11+
* @return static
12+
*/
13+
public static function create()
14+
{
15+
return new static('<stupid></stupid>');
16+
}
17+
18+
/**
19+
* Handles casting to a string
20+
*
21+
* @return string
22+
*/
23+
public function __toString()
24+
{
25+
return '';
26+
}
27+
28+
/**
29+
* Suppress function calls
30+
*
31+
* @param $name
32+
* @param $arguments
33+
*/
34+
public function __call($name, $arguments)
35+
{
36+
// Silence is golden
37+
}
38+
39+
/**
40+
* Suppress static method calls
41+
*
42+
* @param $name
43+
* @param $arguments
44+
*/
45+
public static function __callStatic($name, $arguments)
46+
{
47+
// Silence is golden
48+
}
49+
50+
/**
51+
* Suppress method calls
52+
*
53+
* @param \child $name
54+
* @return \SimpleXMLElement[]|void
55+
*/
56+
public function __get($name)
57+
{
58+
// Silence is golden
59+
}
60+
}

src/helpers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
if (!function_exists('no')) {
4+
/**
5+
* Create a null object
6+
*
7+
* @return static
8+
*/
9+
function no() {
10+
return \ConceptByte\NullObject\NullObject::create();
11+
}
12+
}

tests/NullObjectTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
use ConceptByte\NullObject\NullObject;
4+
5+
class NullObjectTest extends PHPUnit_Framework_TestCase
6+
{
7+
8+
/** @test */
9+
public function it_should_return_an_empty_string()
10+
{
11+
$null = NullObject::create();
12+
$this->assertEquals("", (string)$null->xyz);
13+
}
14+
15+
/** @test */
16+
public function it_should_return_zero_for_an_integer()
17+
{
18+
$null = NullObject::create();
19+
$this->assertEquals(0, (int)$null->xyz);
20+
$this->assertEquals(0, (integer)$null->xyz);
21+
}
22+
23+
/** @test */
24+
public function it_should_return_zero_for_a_float()
25+
{
26+
$null = NullObject::create();
27+
$this->assertEquals(0.0, (float)$null->xyz);
28+
$this->assertEquals(0.0, (real)$null->xyz);
29+
}
30+
31+
/** @test */
32+
public function it_should_return_false_for_a_boolean()
33+
{
34+
$null = NullObject::create();
35+
$this->assertEquals(false, (bool)$null->xyz);
36+
$this->assertEquals(false, (boolean)$null->xyz);
37+
}
38+
39+
/** @test */
40+
public function it_should_return_an_empty_array()
41+
{
42+
$null = NullObject::create();
43+
$this->assertEquals([], (array)$null->xyz);
44+
}
45+
46+
/** @test */
47+
public function it_handles_both_methods_and_properties()
48+
{
49+
$null = NullObject::create();
50+
$this->assertEquals([], (array)$null->xyz);
51+
$this->assertEquals([], (array)$null->xyz());
52+
}
53+
}

0 commit comments

Comments
 (0)