Giter VIP home page Giter VIP logo

namer's Introduction

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.

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.

Scrutinizer Code Quality Code Coverage Build Status SensioLabsInsight

Install

Composer

Add the following configuration to your composer.json file.

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

then run php ./composer.phar update emarref/namer.

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:

$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.

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.

HashStrategy

Returns a hashed representation of the name concatenated with the index. sha1 and md5 are supported.

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.

e.g. Name, Name copy, Name copy 2, Name copy 3 etc

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

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.

ArrayDetector

Detects the existence of a value in an array.

FilesystemDetector

Detects the existence of a file on a filesystem.

GaufretteDetector

Detects the existence of a file using a Gaufrette Adapter, e.g. Gaufrette\Adapter\Local

namer's People

Contributors

emarref avatar samfleming avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

samfleming

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.