Skip to content

Commit 0c7b406

Browse files
committed
Source code
1 parent cc944b5 commit 0c7b406

File tree

7 files changed

+172
-0
lines changed

7 files changed

+172
-0
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# top-most EditorConfig file
2+
root = true
3+
4+
# Unix-style newlines with a newline ending every file
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = false
8+
charset = utf-8
9+
indent_style = tab
10+
trim_trailing_whitespace = true
11+
tab_width = 4
12+
13+
[{package.json,.travis.yml}]
14+
indent_style = space
15+
indent_size = 2

.gitattributes

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Set the default behavior, in case people don't have core.autocrlf set.
2+
* text=auto
3+
4+
# Declare files that will always have LF line endings on checkout.
5+
*.php text eol=lf

.gitignore

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

.scrutinizer.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
checks:
2+
php:
3+
duplication: true
4+
unused_methods: false
5+
unused_parameters: true
6+
argument_type_checks: false
7+
verify_property_names: false
8+
method_calls_on_non_object: true
9+
fix_doc_comments: false
10+
instanceof_class_exists: false
11+
catch_class_exists: false
12+
assignment_of_null_return: false
13+
use_statement_alias_conflict: false
14+
tools:
15+
php_sim:
16+
enabled: true
17+
min_mass: 50 # Defaults to 16
18+
coding_style:
19+
php:
20+
indentation:
21+
general:
22+
use_tabs: true
23+
size: 4
24+
switch:
25+
indent_case: true
26+
spaces:
27+
around_operators:
28+
concatenation: true
29+
ternary_operator:
30+
before_condition: false
31+
after_condition: false
32+
before_alternative: false
33+
after_alternative: false
34+
braces:
35+
classes_functions:
36+
class: end-of-line
37+
function: end-of-line
38+
closure: end-of-line
39+
if:
40+
opening: end-of-line
41+
for:
42+
opening: end-of-line
43+
while:
44+
opening: end-of-line
45+
do_while:
46+
opening: end-of-line
47+
switch:
48+
opening: end-of-line
49+
try:
50+
opening: end-of-line
51+
upper_lower_casing:
52+
keywords:
53+
general: lower
54+
constants:
55+
true_false_null: lower
56+
build:
57+
nodes:
58+
analysis:
59+
tests:
60+
override:
61+
- php-scrutinizer-run

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
[![License](https://img.shields.io/github/license/imponeer/toarray-interface.svg?maxAge=2592000)](LICENSE)
2+
[![GitHub release](https://img.shields.io/github/release/imponeer/toarray-interface.svg?maxAge=2592000)](https://github.com/imponeer/toarray-interface/releases) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/imponeer/toarray-interface/badges/quality-score.png)](https://scrutinizer-ci.com/g/imponeer/toarray-interface/)
3+
4+
# ToArray Interface
5+
6+
Some long time ago there were [RFC about adding __toArray method into PHP](https://wiki.php.net/rfc/object_cast_to_types). Sadly, this was rejected. [PHP-FIG](https://www.php-fig.org/psr/) doesn't have yet any project about that. And that's why today we have many classes in different frameworks that has toArray method. There are some composer packages that has toArray interface like [illuminate/contracts](https://packagist.org/packages/illuminate/contracts). However these packages are not very good choice if you need only one file with interface from there. In that case they have many things that you do not need. So, that's why we have created small composer library that could be used for such cases.
7+
8+
So, basically this library is only for one thing - it gives interface that could be used when you need to know if object has possibility to be converted to array with `toArray` method.
9+
10+
## Installation
11+
12+
To install and use this package, we recommend to use [Composer](https://getcomposer.org):
13+
14+
```bash
15+
composer require imponeer/toarray-interface
16+
```
17+
18+
Otherwise you need to include manualy files from `src/` directory.
19+
20+
## Example
21+
22+
```php5
23+
24+
use Imponeer/ToArrayInterface;
25+
26+
class DummyObject implements ToArrayInterface {
27+
28+
/**
29+
* Converts object to array
30+
*
31+
* @return array
32+
*/
33+
public function toArray() {
34+
return array(
35+
'hash' => sha1(time())
36+
);
37+
}
38+
39+
}
40+
41+
$instance = new DummyObject();
42+
if ($instance instanceof ToArrayInterface) {
43+
var_dump($instance->toArray());
44+
}
45+
46+
```

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "imponeer/toarray-interface",
3+
"description": "PHP library that provides toArray interface, that could be used everywhere where you need to convert object into array and make sure that to that toArray function is because of same interface",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": [
7+
"toArray",
8+
"interface"
9+
],
10+
"authors": [
11+
{
12+
"name": "Raimondas Rimkevičius",
13+
"email": "github@mekdrop.name"
14+
}
15+
],
16+
"autoload": {
17+
"psr-4": {
18+
"Imponeer\\": "src/"
19+
}
20+
},
21+
"require": {
22+
"php": ">=5.0"
23+
}
24+
}

src/ToArrayInterface.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Imponeer;
4+
5+
/**
6+
* Interface to define how object should be converted to array
7+
*/
8+
interface ToArrayInterface
9+
{
10+
11+
/**
12+
* Converts object to array
13+
*
14+
* @return array
15+
*/
16+
public function toArray();
17+
18+
}

0 commit comments

Comments
 (0)