PHP code example of thefredfox / cakephp-ip-type

1. Go to this page and download the library: Download thefredfox/cakephp-ip-type library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thefredfox / cakephp-ip-type example snippets

 php
composer 
 php
// in bootstrap file

Plugin::load('IpType');
Type::map('ip', 'IpType\Database\Type\IpType');
 php
// 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;
}
 php
// 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)
 php
// 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;
};
 php
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';
 php
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);
};