     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    <?php
    /**
     * @type Id
     *
     * Represent a Catapult
     * type id. Where id
     * must be an integer and string seperated
     * by a dash. Thus:
     * c-5o5kfc5hzmyshrpkpjnxzjy
     *
     * prefix representing the entity
     * the other being the unique.
     */

    namespace Catapult;
    final class Id extends Types {
         /**
          *
          * A list of known prefixes
          * TODO: Try to make the type
          * switch whenever match with prefix below
          */
        public static $prefixes = array(
          "c",
          "conf",
          "b",
          "m",
          "g",
          "rec",
          "transc",
          "u"
        );

        /**
         * construct an id object
         * throws on error. If not needed for
         * exception handling use: valid/1
         *
         * @param id: valid Catapult Id
         */
        public function __construct($id)
        {
          if (!(self::valid($id))) {
            throw new \CatapultApiException("Invalid id, used: $id");
          }

          $this->id = $id;
        }

        /**
         * check if the supplied
         * id is valid
         *
         * @param id: valid Catapult Id
         */
        public function valid($id)
        {
          $valid = FALSE;

          foreach (self::$prefixes as $prefix) {
            $m = array();

            preg_match("/$prefix-.*/", $id, $m);

            if (sizeof($m)) {
              $valid = TRUE;
            }
          }

          return $valid;
        }

        public function __toString()
        {
          return (string) $this->id;
        }
    }