Skip to content

10quality/php-class-to-javascript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Class to Javascript (PHP trait)

Latest Stable Version Total Downloads License

PHP trait that casts (transforms, converts) classes into javascript objects (string version).

Installation

With composer, make the dependecy required in your project:

composer require 10quality/php-class-to-javascript

Usage

Add trait to the wanted class:

use TenQuality\Traits\CastJavascriptTrait;

class MyClass
{
    use CastJavascriptTrait;
}

The user the casting methods:

$class = new MyClass;
$class->toJS();
$class->to_js(); // Alias

Hide properties

To hide properties on casting, add the hidden property to the class:

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:

class MyClass
{
    use CastJavascriptTrait;

    // (1) As array
    protected $castingProperties = [
        'propertyA',
        'property_2',
    ];
}
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.

About

PHP trait that casts (transforms, converts) classes into javascript objects (string version).

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages