PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon Sep 28, 2020 1:13 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: Mon Aug 28, 2006 10:52 am 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK
I'm posting this code because I think its perfect. Want to proove me wrong? I'm sure you do. Can you find fault?

What it is designed to do is aid the user of OsisForms to specify a character encoding to use for their applications from a list of approved encodings and then allow me to write code based on different encodings by testing against the object. PHP's doesn't really have any good facilities for restricting input to a series of approved values so there is quite a lot of code but is very straight forward.

How it is used
Syntax: [ Download ] [ Hide ]
class OF

{

    << snip >>

    /**

     * Initialize OF without creating an instance

     *

     * @param OF_Encoding $encoding

     */


    static public function init(OF_Encoding $encoding)

    {

        self::$_encoding = $encoding

        if ($encoding->isUtf8()) {

            require_once 'utf8/utf8.php';

        }

   

        self::$registry = new OF_Registry();

        self::$_idRegistry = new OF_Registry(null);

    }

    << snip >>

}



OF::init(OF_Encoding::getInstance('UTF-8'));


Unit Test Case
Syntax: [ Download ] [ Hide ]
<?php

class TestOfEncoding extends OF_UnitTest

{

    private $_instance;



    public function testConstruction()

    {

        // smoketest this because when it fails (which it is supposed to) its

        // fatal

        //$a = new OF_Encoding();

        //$this->assertError(true);

    }

    public function testGetInstance()

    {

        try {

            $this->_instance = OF_Encoding::getInstance('no exist');

        } catch (OF_Exception $e) {}

        $this->assertNotNull($e);

        $this->_instance = OF_Encoding::getInstance('UTF-8');

    }

    public function testIsUtf8()

    {

        $this->assertTrue($this->_instance->isUtf8());

        // deliberately inexact case:

        $this->_instance = OF_Encoding::getInstance('Iso-8859-1');

        $this->assertFalse($this->_instance->isUtf8());

    }

    public function testToStringAndGet()

    {

        $this->assertEqual($this->_instance->get(), 'ISO-8859-1');

        $this->assertEqual($this->_instance->__tostring(), 'OF_Encoding(ISO-8859-1)');

    }

}


Class
Syntax: [ Download ] [ Hide ]
<?php

/**

 * @package OF

 */


/**

 * OF_Encoding

 *

 * --- Class Details -----------------

 *

 * Ensures only a supported encoding is used.

 * This will be replaced by declare(encoding = 'x'); in PHP 6

 *

 * --- Maintenance Details -----------

 *

 * If you need support for more encodeding add to the static $_supported

 * variable these must be all uppercase. If you need to add any more is* tests

 * remember you can cache them on construction if you think it will aid

 * performance.

 *

 * ------------------------------------

 *

 * @package     OF

 * @author      Oliver Saunders

 * @copyright   Copyright (c) 2006 OS Internet Services (http://www.osinternetservices.com)

 * @license             See LICENSE.txt New BSD License

 */


class OF_Encoding

{

    static protected $_supported = array('UTF-8', 'ISO-8859-1', 'CP1252');

    /**

     * Key of self::$_support, the encoding to use

     *

     * @var int

     */


    protected $_encoding;

    /**

     * @var bool;

     */


    protected $_isUtf8;

    /**

     * Privatised to ensure construction is only done internally with getInstance()

     */


    private function __construct() {}

    /**

     * Creates a controller instance of Encoding

     *

     * @param string $encoding

     * @return Encoding

     */


    static public function getInstance($encoding)

    {

        $encoding = strtoupper($encoding); // ensures case insensitivity

        $supportedKey = array_search($encoding, self::$_supported);



        if ($supportedKey === false) {

            throw new OF_Exception('Unsupported encoding specified (' . $encoding . ')');

        }



        $instance = new self();

        $instance->_encoding = $supportedKey;

        // a very common test, result is cached here at construction

        $instance->_isUtf8 = $encoding == 'UTF-8';

        return $instance;

    }

    /**

     * Access to $_isUtf8

     *

     * @return bool

     */


    public function isUtf8()

    {

        return $this->_isUtf8;

    }

    /**

     * Return encoding

     *

     * @return string

     */


    public function get()

    {

        return self::$_supported[$this->_encoding];

    }

    /**

     * @return string

     */


    public function __tostring()

    {

        return __CLASS__ . '(' . $this->get() . ')';

    }

}


Last edited by Ollie Saunders on Mon Aug 28, 2006 3:34 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 12:06 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
I am obviously missing a big something because that looks like an immense about of code for what should be just a setEncoding() method. And I never understand the attraction of statics, such as that "without creating an instance" which creates two objects internally. What's wrong with __construct()? I prefer simpler.

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 12:16 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK
Is there was something wrong with this code it would be that. But I was thinking about performance you see. I didn't want to have to do string comparisons where I would cache booleans.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 1:05 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 2:30 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 2:44 pm 
Offline
Site Administrator
User avatar

Joined: Sun May 19, 2002 10:24 pm
Posts: 6887

_________________
Image


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 2:49 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 2:52 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 3:24 pm 
Offline
Neighborhood Spidermoddy
User avatar

Joined: Mon Mar 29, 2004 4:24 pm
Posts: 31559
Location: Bothell, Washington, USA
This thread needs a better (more descriptive) name.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 3:36 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK
And now its got one.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 5:08 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Last edited by Ollie Saunders on Mon Aug 28, 2006 5:50 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 5:35 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
So what is a use case?

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 5:55 pm 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 6:36 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 29, 2006 6:09 am 
Offline
DevNet Master
User avatar

Joined: Tue May 24, 2005 6:01 pm
Posts: 3179
Location: UK


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group