code_junkie

Best practices for naming conventions

Nov 12th, 2011
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.65 KB | None | 0 0
  1. $instance = new Class()
  2. $var_name = 5;
  3. $x = get_x();
  4.  
  5. class TestThing
  6. {
  7. public $objectProperty;
  8.  
  9. public function methodName() {
  10. }
  11. }
  12.  
  13. function function_name( $argument = null ) {
  14. }
  15.  
  16. $variable_name = 'hi'; //Non-object
  17. $TestThing = new TestThing; //Object
  18.  
  19. <?php
  20. /*
  21. * This file is part of the psYiiExtensions package.
  22. *
  23. * @copyright Copyright &copy; 2009 Pogostick, LLC
  24. * @link http://www.pogostick.com Pogostick, LLC.
  25. * @license http://www.pogostick.com/licensing
  26. */
  27.  
  28. /**
  29. * Provides a base class for code lookup tables in your database
  30. *
  31. * @package psYiiExtensions
  32. * @subpackage models
  33. *
  34. * @author Jerry Ablan <[email protected]>
  35. * @version SVN: $Id: CPSCodeTableModel.php 402 2010-09-11 23:00:16Z [email protected] $
  36. * @since v1.0.6
  37. *
  38. * @filesource
  39. */
  40. class CPSCodeTableModel extends CPSModel
  41. {
  42. //********************************************************************************
  43. //* Constants
  44. //********************************************************************************
  45.  
  46. /***
  47. * The name of our code table DDL
  48. */
  49. const DDL_NAME = 'code_t.sql';
  50. const DDL_TABLE_NAME = 'code_t';
  51. const DDL_DATA_NAME = 'code_install_data.sql';
  52.  
  53. //********************************************************************************
  54. //* Private Members
  55. //********************************************************************************
  56.  
  57. /**
  58. * @var integer A value filter
  59. */
  60. protected $_valueFilter = null;
  61. public function getValueFilter() { return $this->_valueFilter; }
  62. public function setValueFilter( $value ) { $this->_valueFilter = $value; }
  63.  
  64. /**
  65. * @var string The column name for our "active" attribute.
  66. */
  67. protected $_activeAttributeName = 'active_ind';
  68.  
  69. /**
  70. * @var string The class name of our "code" model
  71. */
  72. protected $_modelClass = 'Codes';
  73.  
  74. //********************************************************************************
  75. //* Public Methods
  76. //********************************************************************************
  77.  
  78. public function init()
  79. {
  80. parent::init();
  81.  
  82. // Get our model class
  83. $this->_modelClass = self::_findModelClass();
  84. }
  85.  
  86. /**
  87. * Installs a standard code table into the database.
  88. *
  89. * @param CDbConnection $dbConnection Defaults to PS::_a()->db
  90. * @param string $name The code table name. Defaults to 'code_t'
  91. * @returns boolean
  92. */
  93. public static function install( $dbConnection = null, $name = self::DDL_TABLE_NAME )
  94. {
  95. $_db = PS::nvl( $dbConnection, PS::_db() );
  96.  
  97. if ( $name && $_db )
  98. {
  99. $_path = Yii::getPathOfAlias( 'pogostick.templates.ddl' );
  100.  
  101. $_sql = file_get_contents( $_path . self::DDL_NAME );
  102. if ( strlen( $_sql ) )
  103. {
  104. $_sql = str_ireplace( '%%TABLE_NAME', $name, $_sql );
  105. $_command = $_db->createCommand( $_sql );
  106. if ( $_command->execute() )
  107. {
  108. $_sql = file_get_contents( $_path . self::DDL_DATA_NAME );
  109.  
  110. // Load some codes...
  111. if ( strlen( $_sql ) )
  112. {
  113. $_sql = str_ireplace( '%%TABLE_NAME', $name, $_sql );
  114. $_command = $_db->createCommand( $_sql );
  115. $_command->execute();
  116. }
  117. }
  118.  
  119. return true;
  120. }
  121. }
  122.  
  123. return false;
  124. }
  125.  
  126. /**
  127. * Get code rows
  128. * @param int $codeId Specific code ID to retrieve
  129. * @param string $codeType Retrieves all codes that are of this type
  130. * @param string $codeAbbreviation Retrieves all codes that are of this abbreviation. If type specicifed, further filters return set
  131. * @param string $sortOrder Optional sort order of result set. Defaults to PK (i.e. id)
  132. * @return array|string Depending on the parameters supplied, either returns a code row, an array of code rows, or a string.
  133. */
  134. protected static function getCodes( $codeId = null, $codeType = null, $codeAbbreviation = null, $sortOrder = null, $activeCodesOnly = true, $valueFilter = null )
  135. {
  136. $_criteria = null;
  137. $_modelClass = self::_findModelClass();
  138. $_model = call_user_func( array( $_modelClass, 'model' ) );
  139.  
  140. if ( null === $sortOrder ) $sortOrder = $_model->primaryKey;
  141.  
  142. // Get a single code...
  143. if ( null !== $codeId ) return $_model->valueFilter( $valueFilter )->active( $activeCodesOnly )->findByPk( $codeId );
  144.  
  145. $_condition = array();
  146. $_params = array();
  147.  
  148. // Get a specific code by type/abbr
  149. if ( null !== $codeType )
  150. {
  151. $_condition[] = 'code_type_text = :code_type_text';
  152. $_params[':code_type_text'] = $codeType;
  153. }
  154.  
  155. if ( null !== $codeAbbreviation )
  156. {
  157. $_condition[] = 'code_abbr_text = :code_abbr_text';
  158. $_params[':code_abbr_text'] = $codeAbbreviation;
  159. }
  160.  
  161. // No conditions? Bail...
  162. if ( empty( $_condition ) )
  163. return null;
  164.  
  165. $_criteria = array(
  166. 'condition' => implode( ' AND ', $_condition ),
  167. 'params' => $_params,
  168. 'order' => PS::nvl( $sortOrder ),
  169. );
  170.  
  171. return $_model->valueFilter( $valueFilter )->active( $activeCodesOnly )->findAll( $_criteria );
  172. }
  173.  
  174. /**
  175. * @return array validation rules for model attributes.
  176. */
  177. public function rules()
  178. {
  179. return array(
  180. array( 'code_type_text', 'length', 'max' => 60 ),
  181. array( 'code_abbr_text', 'length', 'max' => 60 ),
  182. array( 'code_desc_text', 'length', 'max' => 255 ),
  183. array( 'assoc_text', 'length', 'max' => 255 ),
  184. array( 'id, active_ind, code_type_text, code_abbr_text, code_desc_text, create_date, lmod_date', 'required' ),
  185. array( 'id, active_ind, parnt_code_id', 'numerical', 'integerOnly' => true ),
  186. array( 'assoc_value_nbr', 'numerical' ),
  187. );
  188. }
  189.  
  190. /**
  191. * @return array customized attribute labels (name=>label)
  192. */
  193. public function attributeLabels()
  194. {
  195. return array(
  196. 'id' => 'Id',
  197. 'code_type_text' => 'Type',
  198. 'code_abbr_text' => 'Abbreviation',
  199. 'code_desc_text' => 'Description',
  200. 'parnt_code_id' => 'Parent Code Id',
  201. 'assoc_value_nbr' => 'Associated Number',
  202. 'assoc_text' => 'Associated Text',
  203. 'active_ind' => 'Active',
  204. 'create_date' => 'Created On',
  205. 'lmod_date' => 'Modified On',
  206. );
  207. }
  208.  
  209. //********************************************************************************
  210. //* Statics
  211. //********************************************************************************
  212.  
  213. /**
  214. * Returns the static model of the specified AR class.
  215. * @return CActiveRecord the static model class
  216. */
  217. public static function model( $sClassName = __CLASS__ )
  218. {
  219. return parent::model( $sClassName );
  220. }
  221.  
  222. /**
  223. * Find a code by type
  224. *
  225. * @param string $codeType
  226. * @return array
  227. * @static
  228. */
  229. public static function findAllByType( $codeType, $sortOrder = 'code_desc_text', $valueFilter = null )
  230. {
  231. return self::getCodes( null, $codeType, null, $sortOrder, true, $valueFilter );
  232. }
  233.  
  234. /**
  235. * Find a code by type
  236. *
  237. * @param string $codeType
  238. * @return array
  239. * @static
  240. */
  241. public static function findAllByAbbreviation( $codeAbbreviation, $codeType = null, $sortOrder = 'code_desc_text' )
  242. {
  243. return self::getCodes( null, $codeType, $codeAbbreviation, $sortOrder );
  244. }
  245.  
  246. /**
  247. * Finds a single code by code_id
  248. *
  249. * Duplicates findByPk, but wanted to be consistent.
  250. *
  251. * @param integer $iCodeId
  252. * @return CActiveRecord
  253. * @static
  254. */
  255. public static function findById( $iCodeId )
  256. {
  257. return self::getCodes( $iCodeId );
  258. }
  259.  
  260. /**
  261. * Retrieves the associated value for a code
  262. *
  263. * @param int $codeId
  264. * @return double
  265. */
  266. public function valueFilter( $value = null )
  267. {
  268. // Nothing? Move along...
  269. if ( null === $value )
  270. return $this;
  271.  
  272. // Not an array? Make one...
  273. if ( ! is_array( $value ) ) $value = array( $value );
  274.  
  275. // No items?
  276. if ( empty( $value ) )
  277. return $this;
  278.  
  279. // Make string...
  280. if ( count( $value ) == 1 )
  281. $_condition = '( assoc_value_nbr is null or assoc_value_nbr = ' . $value[0] . ')';
  282. else
  283. $_condition = '( assoc_value_nbr is null or assoc_value_nbr in (' . implode( ',', $value ) . '))';
  284.  
  285. $this->getDbCriteria()->mergeWith( array( 'condition' => $_condition ) );
  286. return $this;
  287. }
  288.  
  289. /**
  290. * Returns a code's description
  291. *
  292. * @param int $codeId
  293. * @return string
  294. */
  295. public static function getCodeDescription( $codeId )
  296. {
  297. $_code = self::getCodes( $codeId );
  298. return $_code ? $_code->code_desc_text : null;
  299. }
  300.  
  301. /**
  302. * Returns a code's abbreviation
  303. *
  304. * @param int $codeId
  305. * @return string
  306. */
  307. public static function getCodeAbbreviation( $codeId )
  308. {
  309. $_code = self::getCodes( $codeId );
  310. return $_code ? $_code->code_abbr_text : null;
  311. }
  312.  
  313. /**
  314. * Returns a code's id given a type and abbreviation
  315. *
  316. * @param string $codeType
  317. * @param string $codeAbbreviation
  318. * @return integer
  319. */
  320. public static function getCodeFromAbbreviation( $codeType, $codeAbbreviation )
  321. {
  322. $_codeList = self::getCodes( null, $codeType, $codeAbbreviation );
  323. return ! empty( $_codeList ) ? $_codeList[0]->id : null;
  324. }
  325.  
  326. /**
  327. * Retrieves the associated text for a code
  328. *
  329. * @param int $codeId
  330. * @return string
  331. */
  332. public static function getAssociatedText( $codeId )
  333. {
  334. $_code = self::getCodes( $codeId );
  335. return $_code ? $_code->assoc_text : null;
  336. }
  337.  
  338. /**
  339. * Retrieves the associated value for a code
  340. *
  341. * @param int $codeId
  342. * @return double
  343. */
  344. public static function getAssociatedValue( $codeId )
  345. {
  346. $_code = self::getCodes( $codeId );
  347. return $_code ? $_code->assoc_value_nbr : null;
  348. }
  349.  
  350. /**
  351. * Returns all rows based on type/abbr given
  352. * If it's a specific request, (i.e. type & abbr given) a single row is returned.
  353. *
  354. * @param string $codeType
  355. * @param string $codeAbbreviation
  356. * @return int|CPSCodeTableModel|array
  357. */
  358. public static function lookup( $codeType, $codeAbbreviation = null, $returnIdOnly = true )
  359. {
  360. $_code = self::getCodes( null, $codeType, $codeAbbreviation );
  361. return $_code ? ( $returnIdOnly ? $_code->id : $_code ) : null;
  362. }
  363.  
  364. /**
  365. * Only return active code records as designated by the active_ind
  366. * @returns CPSCodeTableModel
  367. */
  368. public function active( $activeOnly = true )
  369. {
  370. if ( $activeOnly && $this->hasAttribute( $this->_activeAttributeName ) ) $this->getDbCriteria()->mergeWith( array( 'condition' => 'active_ind = :active_ind', 'params' => array( ':active_ind' => 1 ) ) );
  371. return $this;
  372. }
  373.  
  374. /**
  375. * Finds and sets the model class for this object.
  376. * If your model class is not "Codes" and you're not running PHP 5.3.x+, override
  377. * this method to provide your own model name.
  378. *
  379. * @return string The name of the model class
  380. */
  381. protected static function _findModelClass()
  382. {
  383. $_modelClass = 'Codes';
  384.  
  385. // Can't really use get_called_class with 5.2...
  386. if ( version_compare( PHP_VERSION, '5.3.0' ) >= 0 )
  387. $_modelClass = get_called_class();
  388.  
  389. return $_modelClass;
  390. }
  391. }
Add Comment
Please, Sign In to add comment