2017 © Pedro Peláez
 

library namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

image

emarref/namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

  • Thursday, May 28, 2015
  • by emarref
  • Repository
  • 1 Watchers
  • 3 Stars
  • 95,618 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Namer

This is a simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not., (*1)

For example, when saving a file to a filesystem, use the Namer to determine an available filename to use that will not clash with other files on the filesystem., (*2)

Scrutinizer Code Quality Code Coverage Build Status SensioLabsInsight, (*3)

Install

Composer

Add the following configuration to your composer.json file., (*4)

[...]
    "require": {
       "emarref/namer": "dev-master"
   }
[...]

then run php ./composer.phar update emarref/namer., (*5)

Usage

$strategy = new Emarref\Namer\Strategy\SuffixStrategy();
$detector = new Emarref\Namer\Detector\ArrayDetector(['Taken', 'Taken copy']);
$namer    = new Emarref\Namer\Namer($strategy, $detector);
echo $namer->getName('Taken'); // Will return "Taken copy 2"

The detector is optional, but must be passed as the second argument to the Namer#getName() method if it is omitted when instantiating the namer. This is useful when your namer is configured in your DI container, but you need to use a different detector. For example:, (*6)

$namer            = $this->get('default_namer');
$unavailableNames = $repository->getUnavailableNames();
$detector         = new Emarref\Namer\Detector\ArrayDetector($unavailableNames);
echo $namer->getName('Taken', $detector);

Using the Symfony2 Dependency Injection Container

parameters:
    namer.default.limit:                    100

    namer.strategy.suffix.suffix:           copy
    namer.strategy.suffix.incremental:      true
    namer.strategy.suffix.ignore_extension: true

    namer.detector.filesystem.path:         /var/www/website/web/uploads

services:
    namer.strategy.suffix:
        class:  Emarref\Namer\Strategy\SuffixStrategy
        public: false
        arguments:
            - %namer.strategy.suffix.suffix%
            - %namer.strategy.suffix.incremental%
            - %namer.strategy.suffix.ignore_extension%

    namer.detector.filesystem:
        class:  Emarref\Namer\Detector\FilesystemDetector
        public: false
        arguments:
            - %namer.detector.filesystem.path%

    namer.default:
        class:  Emarref\Namer\Namer
        public: false
        arguments:
            - @namer.strategy.suffix
            - @namer.detector.filesystem
            - %namer.default.limit%

    namer:
        alias: namer.default

You can then use the namer by accessing the namer service in the container., (*7)

Strategies

The strategy is the class that generates a new name, based on an iteration index. It must implement the interface Emarref\Namer\Strategy\StrategyInterface. There are currently two strategies available for use. HashStrategy and SuffixStrategy., (*8)

HashStrategy

Returns a hashed representation of the name concatenated with the index. sha1 and md5 are supported., (*9)

SuffixStrategy

A simple strategy that returns the name untouched on iteration 0, returns the name with suffix appended on iteration 1, and returns the name with suffix and iteration appended thereafter., (*10)

e.g. Name, Name copy, Name copy 2, Name copy 3 etc, (*11)

The Suffix can also be inserted before the file extension using SuffixStrategy::setIgnoreExtension(false)., (*12)

Detectors

The detector is the class that determines whether or not a name is available to use. A detector must implement the interface Emarref\Namer\Detector\DetectorInterface. There are currently two simple detectors available for use. ArrayDetector and FilesystemDetector., (*13)

ArrayDetector

Detects the existence of a value in an array., (*14)

FilesystemDetector

Detects the existence of a file on a filesystem., (*15)

GaufretteDetector

Detects the existence of a file using a Gaufrette Adapter, e.g. Gaufrette\Adapter\Local, (*16)

The Versions

28/05 2015

dev-master

9999999-dev https://github.com/emarref/namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

naming namer

11/08 2014

0.6

0.6.0.0 https://github.com/emarref/namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

  Sources   Download

MIT

The Development Requires

naming namer

07/05 2014

0.5.1

0.5.1.0 https://github.com/emarref/namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

  Sources   Download

MIT

The Development Requires

naming namer

07/05 2014

0.5

0.5.0.0 https://github.com/emarref/namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

  Sources   Download

MIT

The Development Requires

naming namer

07/05 2014

0.4

0.4.0.0 https://github.com/emarref/namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

  Sources   Download

MIT

The Development Requires

naming namer

07/05 2014

0.3

0.3.0.0 https://github.com/emarref/namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

  Sources   Download

MIT

The Development Requires

naming namer

07/05 2014

0.2

0.2.0.0 https://github.com/emarref/namer

A simple library for determining whether or not a given name is available for use, and generating a new name based on a strategy if it is not.

  Sources   Download

MIT

The Development Requires

naming namer