Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/php-strict/struct
Implementation of composite type for PHP
https://github.com/php-strict/struct
php php-library php7 struct structure
Last synced: about 1 month ago
JSON representation
Implementation of composite type for PHP
- Host: GitHub
- URL: https://github.com/php-strict/struct
- Owner: php-strict
- License: gpl-3.0
- Created: 2019-03-02T11:54:50.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2019-12-16T05:40:15.000Z (about 5 years ago)
- Last Synced: 2024-05-03T13:59:47.314Z (9 months ago)
- Topics: php, php-library, php7, struct, structure
- Language: PHP
- Size: 29.3 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Struct
[![Software License][ico-license]](LICENSE.txt)
[![Build Status][ico-travis]][link-travis]
[![codecov][ico-codecov]][link-codecov]
[![Codacy Badge][ico-codacy]][link-codacy]Implementation of composite type for PHP.
Contains methods to create from associated array, JSON string or another Struct, with/without type cast.
## Requirements
* PHP >= 7.1
## Install
Install with [Composer](http://getcomposer.org):
```bash
composer require php-strict/struct
```## Usage
Define your own composite type by extending Struct class:
```php
use PhpStrict\Struct\Structclass Book extends Struct
{
/**
* @var string
*/
public $author = '';
/**
* @var string
*/
public $title = '';
/**
* @var string
*/
public $isbn = '';
/**
* @var int
*/
public $pages = 0;
/**
* @var bool
*/
public $publicated = false;
/**
* @var array
*/
public $tags = [];
}
```Now you can fill your strcuture with data:
```php
//book with classic assign data to class fields
$book1 = new Book();
$book1->author = 'Author Name';
$book1->title = 'Book title 1';
$book1->isbn = '000-0-000-00000-0';
$book1->pages = 240;
$book1->publicated = true;
$book1->tags = ['fantastic', 'space', 'detective'];//another book with data through array
$book2 = new Book([
'author' => 'Author Name',
'title' => 'Book title 2',
'isbn' => '000-0-000-00000-0',
'pages' => 156,
'publicated' => true,
'tags' => ['drama', 'nature'],
]);//another book with data through JSON
$json = '{"author":"Author Name","title":"Book title 3","isbn":"000-0-000-00000-0","pages":156,"publicated":true,"tags":["comedy","city"]}';
$book3 = new Book($json);//book, based on existing book
$book4 = new Book($book1);
$book4->title = 'Book title 4';
```See examples dir.
## Tests
To execute the test suite, you'll need [Codeception](https://codeception.com/).
```bash
vendor\bin\codecept run
```[ico-license]: https://img.shields.io/badge/license-GPL-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/php-strict/struct/master.svg?style=flat-square
[link-travis]: https://travis-ci.org/php-strict/struct
[ico-codacy]: https://api.codacy.com/project/badge/Grade/92ff05432c1c4c1b8599e1f53ee5c15a
[link-codacy]: https://www.codacy.com/app/php-strict/struct?utm_source=github.com&utm_medium=referral&utm_content=php-strict/struct&utm_campaign=Badge_Grade
[ico-codecov]: https://codecov.io/gh/php-strict/struct/branch/master/graph/badge.svg
[link-codecov]: https://codecov.io/gh/php-strict/struct