Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/10quality/php-class-to-javascript
PHP trait that casts (transforms, converts) classes into javascript objects (string version).
https://github.com/10quality/php-class-to-javascript
Last synced: 5 days ago
JSON representation
PHP trait that casts (transforms, converts) classes into javascript objects (string version).
- Host: GitHub
- URL: https://github.com/10quality/php-class-to-javascript
- Owner: 10quality
- License: mit
- Created: 2017-02-23T19:05:09.000Z (over 7 years ago)
- Default Branch: v1.0
- Last Pushed: 2017-02-28T19:04:10.000Z (over 7 years ago)
- Last Synced: 2024-04-29T03:02:54.731Z (7 months ago)
- Language: PHP
- Size: 5.86 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Class to Javascript (PHP trait)
[![Latest Stable Version](https://poser.pugx.org/10quality/php-class-to-javascript/v/stable)](https://packagist.org/packages/10quality/php-class-to-javascript)
[![Total Downloads](https://poser.pugx.org/10quality/php-class-to-javascript/downloads)](https://packagist.org/packages/10quality/php-class-to-javascript)
[![License](https://poser.pugx.org/10quality/php-class-to-javascript/license)](https://packagist.org/packages/10quality/php-class-to-javascript)PHP trait that casts (transforms, converts) classes into javascript objects (string version).
## Installation
With composer, make the dependecy required in your project:
```bash
composer require 10quality/php-class-to-javascript
```## Usage
Add trait to the wanted class:
```php
use TenQuality\Traits\CastJavascriptTrait;class MyClass
{
use CastJavascriptTrait;
}
```The user the casting methods:
```php
$class = new MyClass;
$class->toJS();
$class->to_js(); // Alias
```### Hide properties
To hide properties on casting, add the `hidden` property to the class:
```php
class MyClass
{
use CastJavascriptTrait;protected $hidden = [
'propertyA',
'property_2',
];
}
```### Properties selection
To select a specific selection of properties to cast, add the `castingProperties` property to the class:
```php
class MyClass
{
use CastJavascriptTrait;// (1) As array
protected $castingProperties = [
'propertyA',
'property_2',
];
}
``````php
class MyClass
{
use CastJavascriptTrait;protected $attributes = [
'id',
'name',
];// (1) As property mapping
protected $castingProperties = 'attributes';
}
```## Coding guidelines
PSR-4.
## LICENSE
The MIT License (MIT)
Copyright (c) 2017 [10Quality](http://www.10quality.com).