]> dev.renevier.net Git - syj.git/blob - library/phptojs.php
version 0.1
[syj.git] / library / phptojs.php
1 <?php
2 /*  This file is part of Syj, Copyright (c) 2010 Arnaud Renevier,
3     and is published under the AGPL license. */
4
5 namespace phptojs;
6
7 class JsObject {
8     protected $_name;
9     protected $_data = array();
10     public function __construct($name = null, $data = array()) {
11         $this->_name = $name;
12         if (is_array($data)) {
13             $this->_data = $data;
14         }
15     }
16
17     public function getName() {
18         return $this->_name;
19     }
20
21     public function setName($name) {
22         $this->_name = (string)$name;
23         return $this;
24     }
25
26     public function __set($property, $value) {
27         $this->_data[$property] = $value;
28     }
29     public function __get($property) {
30         if (array_key_exists($property, $this->_data)) {
31             return $this->_data[$property];
32         }
33         return null;
34     }
35
36     public function __unset($property) {
37         unset($this->_data[$property]);
38     }
39     public function __isset($property) {
40         return __isset($this->_data[$property]);
41     }
42
43     public function __toString() {
44         if ($this->_name) {
45             $prefix = "var " . $this->_name . " = ";
46         } else {
47             $prefix = "";
48         }
49         return $prefix . " " . json_encode($this->_data, JSON_FORCE_OBJECT) . ";" . PHP_EOL;
50     }
51
52 }