Commit f2837835 authored by Constantin Druc's avatar Constantin Druc 🤦🏻

Merge branch 'base-changes' into 'master'

Static method should not be abstract

waiting for test.

See merge request !5
parents 571a51c0 303a1749
<?php
namespace WordpressTheme\Model;
abstract class Base {
/**
*
* @var array
*/
private $cachedValues = array();
/**
*
* @var \WP_Post
*/
private $post;
/**
*
* @param int|\WP_Post $postIdOrPostObj
*/
public function __construct($postIdOrPostObj) {
if(is_numeric($postIdOrPostObj)) {
$this->post = get_post($postIdOrPostObj);
} elseif(is_object($postIdOrPostObj)) {
$this->post = $postIdOrPostObj;
}
}
/**
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments = null) {
if(empty($this->post)) {
return null;
}
$fieldName = lcfirst(self::decamelize(substr($name, 3)));
$postProperty = 'post_' . $fieldName;
if(isset($this->post->$postProperty)) {
return $this->post->$postProperty;
}
if(!isset($this->cachedValues[$fieldName])) {
$this->cachedValues[$fieldName] = get_field($fieldName, $this->post->ID);
}
return $this->cachedValues[$fieldName];
}
/**
*
* @param array $arguments
* @return \static
*/
static public function getPosts($arguments = array()) {
$arguments = array_merge(array(
'post_type' => static::getPostType()
), $arguments);
$posts = array();
foreach(get_posts($arguments) as $post) {
$posts []= new static($post);
}
return $posts;
}
/**
*
* @param array $arguments
* @return \static
*/
static public function getPost($arguments = array()) {
if(is_array($arguments)) {
$arguments = array_merge(array(
'post_type' => static::getPostType()
), $arguments);
}
$post = get_post($arguments);
return $post ? new static($post) : false;
}
/**
* @param $string
* @return string
*/
static public function decamelize($string) {
return strtolower(preg_replace('/(?|([a-z\d])([A-Z])|([^\^])([A-Z][a-z]))/', '$1_$2', $string));
namespace WordpressTheme\Model;
abstract class Base
{
/**
*
* @var array
*/
private $cachedValues = array();
/**
*
* @var \WP_Post
*/
private $post;
/**
*
* @param int|\WP_Post $postIdOrPostObj
*/
public function __construct($postIdOrPostObj)
{
if (is_numeric($postIdOrPostObj)) {
$this->post = get_post($postIdOrPostObj);
} elseif (is_object($postIdOrPostObj)) {
$this->post = $postIdOrPostObj;
}
}
/**
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments = null)
{
if (empty($this->post)) {
return null;
}
$fieldName = lcfirst(self::decamelize(substr($name, 3)));
$postProperty = 'post_' . $fieldName;
if (isset($this->post->$postProperty)) {
return $this->post->$postProperty;
}
if (!isset($this->cachedValues[$fieldName])) {
$this->cachedValues[$fieldName] = get_field($fieldName, $this->post->ID);
}
/**
* @return \WP_Post
*/
public function getQueryObject(){
return $this->post;
return $this->cachedValues[$fieldName];
}
/**
*
* @param array $arguments
* @return \static
*/
static public function getPosts($arguments = array())
{
$arguments = array_merge(array(
'post_type' => static::getPostType()
), $arguments);
$posts = array();
foreach (get_posts($arguments) as $post) {
$posts [] = new static($post);
}
static abstract protected function getPostType();
}
\ No newline at end of file
return $posts;
}
/**
*
* @param array $arguments
* @return \static
*/
static public function getPost($arguments = array())
{
if (is_array($arguments)) {
$arguments = array_merge(array(
'post_type' => static::getPostType()
), $arguments);
}
$post = get_post($arguments);
return $post ? new static($post) : false;
}
/**
* @param $string
* @return string
*/
static public function decamelize($string)
{
return strtolower(preg_replace('/(?|([a-z\d])([A-Z])|([^\^])([A-Z][a-z]))/', '$1_$2', $string));
}
/**
* @return \WP_Post
*/
public function getQueryObject()
{
return $this->post;
}
/**
* @return string
*/
static protected function getPostType()
{
$className = get_called_class();
$segments = explode("\\", $className);
return ucfirst(array_pop($segments));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment