2017 © Pedro Peláez
 

library tokenlist

The StringTokenList class represents a set of distinct space-separated tokens.

image

mcaskill/tokenlist

The StringTokenList class represents a set of distinct space-separated tokens.

  • Wednesday, February 24, 2016
  • by mcaskill
  • Repository
  • 1 Watchers
  • 0 Stars
  • 11 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

TokenList

The StringTokenList class represents a set of distinct space-separated tokens. It provides the main functionalities of a PHP Array. The main differences are that the StringTokenList:, (*1)

  • allows only integers as indexes,
  • is always case-sensitive,
  • and excludes duplicate values.

The StringTokenList class is essentially a PHP implementation of the DOMTokenList JavaScript interface., (*2)

The class is extended by:, (*3)

  • DOMTokenList
    • DOMClassList
    • DOMRelList

Installation

With Composer

$ composer require mcaskill/tokenlist
{
    "require": {
        "mcaskill/tokenlist": "dev-master"
    }
}
<?php

require 'vendor/autoload.php';

use McAskill\TokenList\StringTokenList;

printf( (string) ( new StringTokenList([ 'foo', 'baz', 'qux' ]) ) );

Without Composer

Why are you not using composer? Download the repository and save the files into your project path somewhere., (*4)

<?php

require 'path/to/StringTokenList.php';
require 'path/to/DOMTokenList.php';

use McAskill\TokenList\DOMTokenList;

printf( ( new DOMTokenList([ 'foo', 'baz', 'qux' ]) )->attr() );

Examples

Consult source code for each class for additional usage examples., (*5)

Example #1 Basic Usage, (*6)

<?php

$obj = new StringTokenList;

$obj->add('foo baz qux');
var_dump( $obj->value );

$obj->add([ 'foo', 'not', 'qux', 'xor' ]);
var_dump( $obj->value );

$obj->remove([ 'foo', 'qux' ]);
var_dump( $obj->value );

$obj->replace( 'not', 'and' );
var_dump( $obj->value );

$obj->toggle( 'foo' );
var_dump( $obj->value );

$obj->toggle( 'foo' );
var_dump( $obj->value );

var_dump( $obj->contains('and') );

var_dump( $obj->item(1) );

var_dump( (string) $obj );

var_dump( count( $obj ) ); // Equivalent to $obj->count();

The above example will output something similar to:, (*7)

array (
  0 => 'foo',
  1 => 'baz',
  2 => 'qux',
)
array (
  0 => 'foo',
  1 => 'baz',
  2 => 'qux',
  3 => 'not',
  4 => 'xor',
)
array (
  0 => 'baz',
  1 => 'not',
  2 => 'xor',
)
array (
  0 => 'baz',
  1 => 'and',
  2 => 'xor',
)
array (
  0 => 'baz',
  1 => 'and',
  2 => 'xor',
  3 => 'foo',
)
array (
  0 => 'baz',
  1 => 'and',
  2 => 'xor',
)
bool(true)
string(3) "xor"
string(11) "baz xor and"
int(3)

Example #2 Syntactic Sugar, (*8)

<?php

$obj = new StringTokenList;

$obj[] = 'foo';          // Equivalent to $obj->add('foo');
$obj['baz'] = true;      // Equivalent to $obj->add('baz');
$obj['baz'] = false;     // Equivalent to $obj->remove('baz');
unset( $obj['foo'] );    // Equivalent to $obj->remove('foo');
unset( $obj[0] );        // Equivalent to $obj->remove( $obj->item(0) );
$obj['foo'] = 'qux';     // Equivalent to $obj->replace('foo', 'qux');
isset( $obj['foo'] );    // Equivalent to $obj->contains('foo');
$obj['foo'];             // Equivalent to $obj->contains('foo');
$obj[0];                 // Equivalent to $obj->item(0);

The Versions

24/02 2016

dev-master

9999999-dev http://github.com/mcaskill/tokenlist

The StringTokenList class represents a set of distinct space-separated tokens.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires