Skip to content

Instantly share code, notes, and snippets.

@flangofas
Last active August 26, 2016 21:39
Show Gist options
  • Save flangofas/fc130fca139d4957d658 to your computer and use it in GitHub Desktop.
Save flangofas/fc130fca139d4957d658 to your computer and use it in GitHub Desktop.
CakePHP 2 API ArticleList using bitwise operators
<?php
App::uses('ArticleListsAppModel', 'ArticleLists.Model');
/**
* ArticleList Model
*
* @property ArticleListPost $ArticleListPost
*/
class ArticleList extends ArticleListsAppModel {
const MASK_ID = 1;
const MASK_ID_PROPERTIES = 2;
const MASK_ID_PROPERTIES_SLUG = 4;
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'ArticleListPost' => array(
'className' => 'ArticleLists.ArticleListPost',
'foreignKey' => 'article_list_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty')
)
),
);
/**
* Model Behaviours
*
* @var array
*/
public $actsAs = array('Containable');
public function beforeValidate($options = array()) {
if (!empty($this->data[$this->alias]['title']) && empty($this->data[$this->alias]['slug'])) {
$this->data[$this->alias]['slug'] = strtolower(Inflector::slug($this->data[$this->alias]['title'], '-'));
}
return true;
}
public function removeRequiredParams($query) {
$filtered_params = array();
/**
* @todo: [$required_params culture omitted for now]
*/
$required_params = array('site', 'language', 'culture');
foreach ($required_params as $param) {
if (!empty($query[$param])) {
$filtered_params[$param] = $query[$param];
}
else {
$filtered_params = array();
break;
}
}
return !empty($filtered_params) ? array_diff_assoc($query, $filtered_params) : $filtered_params;
}
public function findParamsMask(array $query) {
$params_map = array(
static::MASK_ID => array('id'),
static::MASK_ID_PROPERTIES => array('id', 'properties'),
static::MASK_ID_PROPERTIES_SLUG => array('id', 'properties', 'slug')
);
$query_keys = array_keys($query);
foreach ($params_map as $key => $params) {
if (empty(array_diff($query_keys, $params))) {
$result = $key;
break;
}
}
return $result;
}
public function fetchData($mask, array $query) {
if ($mask & static::MASK_ID) {
return $this->find('first',
array('conditions' => array('ArticleList.id' => $query['id']),'recursive' => 2)
);
}
if ($mask & static::MASK_ID_PROPERTIES) {
$result = array();
$result = $this->find('first', array(
'conditions' => $this->alias . '.' . 'id = ' . $query['id'],
'recursive' => -1
)
);
return $this->find('first', array(
'conditions' => 'ArticleList.id = 10',
'fields' => array('ArticleList.id'),
'contain' => array(
'ArticleListPost' => array(
'fields' => array('ArticleListPost.id'),
'ArticleListPostProperty' => array(
'conditions' => array(
'ArticleListPostProperty.name' => 'subcategory',
'ArticleListPostProperty.value' => 'F',
),
),
'Content' => array(
'condition' => 'Content.slug = "Forex"'
)
)
)
)
);
}
if ($mask & static::MASK_ID_PROPERTIES_SLUG) {
}
return;
}
}
<?php
App::uses('ArticleListsAppModel', 'ArticleLists.Model');
/**
* ArticleListPost Model
*
* @property ArticleList $ArticleList
* @property Content $Content
*/
class ArticleListPost extends ArticleListsAppModel {
public $validate = array(
'article_list_id' => array(
'rule' => 'decimal',
'required' => true,
'allowEmpty' => false,
),
'content_id' => array(
'rule' => 'decimal',
'required' => true,
'allowEmpty' => false
)
);
/**
* hasMany associations
*
* @var array
*/
public $hasMany = array(
'ArticleListPostProperty' => array(
'className' => 'ArticleLists.ArticleListPostProperty',
'foreignKey' => 'article_list_post_id',
'dependent' => false
)
);
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'ArticleList' => array(
'className' => 'ArticleLists.ArticleList',
'foreignKey' => 'article_list_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Content' => array(
'className' => 'Contents.Content',
'foreignKey' => 'content_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* Model Behaviours
*
* @var array
*/
public $actsAs = array('Containable');
/**
* Prepare lists to be displayed by Form input
* @todo generic
*
* @param array $lists
* @return array
*/
public function prepareLists(array $lists = array()) {
$result = array();
foreach($lists as $list) {
$result[$list['ArticleList']['id']] = $list['ArticleList']['title'];
}
return $result;
}
/**
* Prepare contents to be displayed by Form input
* @todo generic
*
* @param array $contents
* @return array
*/
public function prepareContents(array $contents = array()) {
$result = array();
foreach($contents as $content) {
$result[$content['Content']['id']] = $content['Content']['title'];
}
return $result;
}
/**
* Process Properties to be printed in the view
*
* @param array $properties
* @return array
*/
public function processProperties(array $properties = array()) {
$result = array();
foreach ($properties as $property) {
$result[ucwords($property['name'])][] = $property['value'];
}
return $result;
}
public function beforeSave($options = array()) {
$conditions = array(
'ArticleListPost.article_list_id' => $this->data[$this->alias]['article_list_id'],
'ArticleListPost.content_id' => $this->data[$this->alias]['content_id']
);
if ($this->hasAny($conditions)) {
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment