Php generate class code

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Code generator for making data classes on PHP

License

ProgMiner/php-datagen

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

DEVELOP OF THIS PROJECT IS CURRENTLY STOPPED

The PHP-DataGen project is a utility — code generator of PHP classes with certain properties and is aimed at simplifying the work of PHP programmers.

The article about this project is available on Russian language at https://habr.com/post/415861/.

$ composer require progminer/php-datagen

The tool has both the ability to control the generation using PHP scripts and the CLI to work with the built-in parser of its own language (hereinafter — PDGL).

# General form ./php-datagen command> # File compiling ./php-datagen compile files>. # Project building (all files compiling) ./php-datagen build [project dir>]

All language-supported statements are provided in the file schema.md at the root of the project, but without a description of what a particular operator does. The namespace and use statements works the same as in normal PHP, but the class, fields, and their modifiers are not so simple.

Of the modifiers of the class can be identified only one non-standard for PHP the final modifier which also has the final! variation. The fact is that the result of the work of PHP-DataGen — class, which must be extended by another class to work.

The final modifier turns the class into a ready-to-use class by removing the prefix (the default, so far, without the possibility of modification, Data_ ) and the abstract modifier of the resulting PHP class. The final! modifier which «under the hood» is referred to as «final final» is an addition to the final modifier (and can not be used without it) and adds to the resulting PHP class the final modifier.

The syntax of class field very little resemblance to the syntax of PHP classes properties and even more, in my opinion, resembles the syntax of class properties Kotlin.

Let’s start with what is written in the file schema.md:

And now in order (operators are bold, substitutions are italic):

  • direct — modifier. If exists, allows the extending class to access properties directly (sets the protected access modifier instead of private );
  • val or var is a field declaration operator. If val is used, the property is not editable after setting in the constructor, if var is editable;
  • Field name — the name of the field, specified without the PHP-specific dollar sign ( $ );
  • : — optional colon operator to specify the field type. If not specified, the field type is considered mixed ;
  • Type name — type name. It can be one of the standard PHP types (case-insensitive) or a class name. If it ends with a question sign (for example, string? ) then the field as well can store null value;
  • , — optional comma operator allows you to specify the validator name after the type (or validator) name;
  • Validator name — name of validator (see next section);
  • , := or = — default value assignment operator. In variation assigns a value when declaring a property. In variation := assigns a value when the constructor is called without checking the type and calling validators. In variation = assigns a value when calling a constructor with type checking and calling validators;
  • ` or «` — see Default value;
  • Default value — the default value of the field. It can be surrounded by ` or «` operators when contains a semicolon ( ; ) (except when a variation of the default value assignment operator is used). There is no difference in the use of ` or «` if the default value is not contains a reverse apostrophe (`), in which case you must use the «` operator.

If you discover any security related issues, please email eridan200@mail.ru instead of using the issue tracker.

The MIT License (MIT). Please see License File for more information.

About

Code generator for making data classes on PHP

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Script that generates simple PHP class code for working with database objects

License

noko3/php_class_generator

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Simple tool to generate PHP code for working with database objects

./generate_class.php ClassName sql_table_name id_field_name +getter_and_setter [+. ] -only_getter [-. ] [meta meta_table_name rel_field key_field value_field] > output.php 
  • Arg 1 is a desired class name
  • Arg 2 is a SQL table name
  • Arg 3 is an ID SQL table field name
  • The following args are SQL table field names:
    • +field_name will add getFieldName() and setFieldName($value)
    • -field_name will only add getFieldName()
    • table_name is a name of metadata MySQL table
    • rel_field is a name of a field that stores object ID
    • key_field is a name of a field that stores meta key
    • value_field is a name of a field that stores meta value

    Output PHP class will have the following methods:

    • getFieldName() and optionally setFieldName($value) for a field called field_name
    • __construct($id, $row=null) : you can initialize new class instance by specifying an $id attribute or row data
    • save() method to save changes
    • getMeta($key) / setMeta($key, $value) to work with metadata

    Example table structure and output PHP code

    SQL create table statements

    /* Users */ create table `users` ( `id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT, `flags` BIGINT NOT NULL DEFAULT 0, `ctime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `mtime` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `user_name` VARCHAR(128), `email` VARCHAR(80) UNIQUE NULL, `password` VARCHAR(40) ) Engine=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci; /* Users metadata */ create table `user_meta` ( `user_id` BIGINT UNSIGNED NOT NULL, `key` VARCHAR(16), `value` VARCHAR(128), PRIMARY KEY (`user_id`,`key`), FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ) Engine=InnoDB CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
    generate_class.php User users id +flags -ctime -mtime +user_name +email +password meta user_meta user_id key value > user.php
     class User < private $id; private $ctime; private $mtime; private $flags; private $user_name; private $email; private $password; private $_loaded = false; private $_modified = false; private $_m_loaded = false; private $_m_modified = []; private $_META = Array(); /** id field getter */ public function getId() return $this->id;> /** ctime field getter */ public function getCtime() return $this->ctime;> /** mtime field getter */ public function getMtime() return $this->mtime;> /** flags field getter */ public function getFlags() return $this->flags;> /** flags field setter */ public function setFlags($value) < if ($this->flags == $value) return; $this->flags = $value; $this->_modified = true; > /** user_name field getter */ public function getUserName() return $this->user_name;> /** user_name field setter */ public function setUserName($value) < if ($this->user_name == $value) return; $this->user_name = $value; $this->_modified = true; > /** email field getter */ public function getEmail() return $this->email;> /** email field setter */ public function setEmail($value) < if ($this->email == $value) return; $this->email = $value; $this->_modified = true; > /** password field getter */ public function getPassword() return $this->password;> /** password field setter */ public function setPassword($value) < if ($this->password == $value) return; $this->password = $value; $this->_modified = true; > public function __construct($id=null, $row=null) < if ($id !== null) < $this->id = $id; $this->load($row); > else < $this->_modified = true; > > private function create() < $sql pl-s">INSERT INTO `users` (`flags`,`user_name`,`email`,`password`) VALUES (:flags,:user_name,:email,:password)"; $params = Array ( "flags" => $this->flags, "user_name" => $this->user_name, "email" => $this->email, "password" => $this->password, ); $this->id = exec_query($sql, $params, false, true); $this->_loaded = true; $this->_modified = false; > private function update() < $sql pl-s">UPDATE `users` SET `flags` = :flags, `user_name` = :user_name, `email` = :email, `password` = :password WHERE `id` = :id"; $params = Array ( "flags" => $this->flags, "user_name" => $this->user_name, "email" => $this->email, "password" => $this->password, "id" => $this->id, ); exec_query($sql, $params, false); $this->_modified = false; > private function loadMeta() < if ($this->_m_loaded) return; $this->_META = Array(); $sql pl-s">SELECT `key` k,`value` v FROM `user_meta` WHERE `user_id` = ?"; $params = [$this->id]; $res = exec_query($sql, $params); foreach ($res as $row) < $this->_META[$row["k"]] = $row["v"]; > $this->_m_loaded = true; > private function saveMeta() < if (!count($this->_m_modified)) return; $values = ""; $params = []; foreach ($this->_m_modified as $key=>$true) < $values . pl-s">,(. )"; $params []= $this->id; $params []= $key; $params []= $this->_META[$key]; > $values = substr($values,1); $sql pl-s">REPLACE INTO `user_meta` (`user_id`,`key`,`value`) VALUES $values"; $res = exec_query($sql, $params, false); $this->_m_modified = Array(); > public function getMeta($key) < if (!$this->_m_loaded) $this->loadMeta(); if (!array_key_exists($key, $this->_META)) return null; return $this->_META[$key]; > public function setMeta($key, $value) < if (array_key_exists($key, $this->_META) && $this->_META[$key] == $value) return; $this->_META[$key] = $value; $this->_m_modified[$key] = true; > public function load($row=null) < if ($this->_loaded && !$this->_modified) return; if ($row === null) < $sql pl-s">SELECT `ctime`,`mtime`,`flags`,`user_name`,`email`,`password` FROM `users` WHERE `id` = :id"; $params = Array("id"=>$this->id); $result = exec_query($sql,$params); if (!count($result)) < throw new Exception("Entry not found"); > $row = $result[0]; >; list( $this->ctime, $this->mtime, $this->flags, $this->user_name, $this->email, $this->password ) = Array ( $row["ctime"], $row["mtime"], $row["flags"], $row["user_name"], $row["email"], $row["password"]); $this->_loaded = true; $this->loadMeta(); > public function save() < if (!$this->_modified) return; if ($this->_loaded) $this->update(); else $this->create(); $this->saveMeta(); > > ?>

    About

    Script that generates simple PHP class code for working with database objects

    Источник

    Читайте также:  Как поставить на фон картинку в HTML
Оцените статью