Skip to content

Commit

Permalink
start to implement trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiriac committed Dec 19, 2016
1 parent 27bdc5d commit 9349baa
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/trait.js
@@ -0,0 +1,36 @@
/*!
* Copyright (C) 2016 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-writer/graphs/contributors
* @url http://glayzzle.com/php-writer
*/

var filter = require('./helpers/filter');

/**
* @constructor
*/
var Trait = function(ast) {
this.ast = ast;
};

/**
* Change the trait name
* @return {Trait}
*/
Trait.prototype.setName = function(name) {
this.ast[1] = name;
return this;
};

/**
* @return {Interface|Null}
*/
Trait.locate = function(ast, name) {
return filter(ast, 'trait', function(node) {
if (node[1] === name) {
return new Trait(node);
}
});
};

module.exports = Trait;
30 changes: 30 additions & 0 deletions test/trait.js
@@ -0,0 +1,30 @@
/*!
* Copyright (C) 2016 Glayzzle (BSD3 License)
* @authors https://github.com/glayzzle/php-writer/graphs/contributors
* @url http://glayzzle.com/php-writer
*/

var should = require('./assert');
var writer = require('../src/index');

describe('Trait', function() {

var test = new writer([
'<?php',
'trait foo {',
'\tconst BAR = 1;',
'}'
].join('\n'));
var traitObj;

it('findTrait', function() {
traitObj = test.findTrait('foo');
traitObj.should.be.Object();
});

it('setName', function() {
traitObj.setName('baz');
traitObj.ast[1].should.be.equal('baz');
});

});

0 comments on commit 9349baa

Please sign in to comment.