Code Monkey home page Code Monkey logo

cakephp-ip-type's Introduction

IpType plugin for CakePHP

Description

An Ip Type for the Database Framework of CakePHP, which converts raw ip addresses represented as human readable strings (127.0.0.1, ::1) to byte strings with inet_pton to store them into a database.

The final converted value for the database is meant to be stored as a LOB value. I used a VARBINARY with 16 bytes. It is long enough for also IPv6 addresses.

The class was written for an application for InnoGames GmbH. The permission was given to make this class public.

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require thefredfox/cakephp-ip-type

After that you have to load the plugin in your application's bootstrap file and map the type for the database as follows:

// in bootstrap file

Plugin::load('IpType');
Type::map('ip', 'IpType\Database\Type\IpType');

In the Table class itself you have to tell the column to be this type:

// in your Entity Table class (eg. UsersTable)

use Cake\Database\Schema\Table as Schema;

protected function _initializeSchema(Schema $schema) {
    $schema->columnType('ip', 'ip');
    return $schema;
}

Configuration

The default function for encoding and decoding is 'inet_pton' resp. 'inet_ntop'. However it is possible to set the encode (toDatabase) and decode (toPHP) methods as callables.

Is the variable not a callable an \UnexpectedValueException will be thrown with the message: 'Could not decode the value, IpType::_decode has to be a callable.' resp. '... encode ... IpType::_encode ...'.

Method Strings

// in bootstrap file

/** @var IpType $ipType */
$ipType = Type::build('ip');
$ipType->_encode = 'ip2long'; // using global ip2long method for encoding (just IPv4 support)
$ipType->_decode = 'long2ip'; // using global long2ip method for decoding (just IPv4 support)

Custom Functions

// in bootstrap file

/** @var IpType $ipType */
$ipType = Type::build('ip');
$ipType->_encode = function ($value) {
    return $value . '1'; // just concatenate a '1' at the end, for what reason ever
};
$ipType->_decode = function ($value) {
    return $value;
};

Static Functions of a Class

class TestClass {
    public static function staticEncode($value) { /*...*/ }
    public static function staticDecode($value) { /*...*/ }
}

/** @var IpType $ipType */
$ipType = Type::build('ip');
$ipType->_encode = 'TestClass::staticEncode';
$ipType->_decode = 'TestClass::staticDecode';

Non Static Functions of a Class via Wrapper Function

class TestClass {
    public function encode($value) { /*...*/ }
    public function decode($value) { /*...*/ }
}

/** @var IpType $ipType */
$ipType = Type::build('ip');
$ipType->_encode = function ($value) {
   $object = new TestClass();
   return $object->encode($value);
};
$ipType->_decode = function ($value) {
   $object = new TestClass();
   return $object->decode($value);
};

Database

The final converted value for the database is meant to be stored as a LOB value. I used a VARBINARY with 16 bytes. It is long enough for also IPv6 addresses.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.