Codeigniter write chainable functions - codeigniter

Can I write chainable functions in CodeIgniter?
So if I have functions like these :
function generate_error(){
return $data['result'] = array('code'=> '0',
'message'=> 'error brother');
}
function display_error(){
$a= '<pre>';
$a.= print_r($data);
$a.= '</pre>';
return $a;
}
I want to call those by chaining them :
echo $this->generate_error()->display_error();
The reason why I want to seperate these functions are because display_error() is only useful for development, so when it comes to production, I can just remove the display_error() or something like that.
Thanks!

To write chainable functions they musy be part of a class, from the function you then return a reference to the current class (usually $this).
If you return anything other than a reference to a class it will fail.
It is also possible to return a reference to another class (e.g. when you use the code igniter active records class get() function it returns a reference to the DBresult class)
class example {
private $first = 0;
private $second = 0;
public function first($first = null){
$this->first = $first;
return $this;
}
public function second($second = null){
$this->second = $second;
return $this;
}
public function add(){
return $this->first + $this->second;
}
}
$example = new example();
//echo's 15
echo $example->first(5)->second(10)->add();
//will FAIL
echo $example->first(5)->add()->second(10);

you should return $this in your function to make chain-able functions in php oop
public function example()
{
// your function content
return $this;
}

Related

Laravel collection map use private function in same class instead closure

I use the map function to loop each object of collections.
The closure method is too long and I tried to make it a private function.
But I cannot successfully call the function.
Here is my sample code. (note: the function test may be very long in real case)
<?php
class cls
{
private function test($a) {
return ($a + 1);
}
public function run1() {
return ($this->test(5));
}
public function run2() {
$col = Collect([1,2,3]);
return ($col->map($this->test()));
}
public function run3() {
$col = Collect([1,2,3]);
$mycls = $this;
return ($col->map(function ($c) use ($mycls) {
return($mycls->test($c));
}));
}
}
$c = new cls;
$c->run1(); # 6
$c->run2(); # Error: Too Few Argements
$c->run3(); # [2,3,4]
I use the function: run1 to test if the private function is callable and I failed at function: run2.
Although function: run3 makes code shorten. It seems a little bit too superfluous.
How can I make run2 works?
Update
My version of Laravel is 6.2
Update
I tried the #xenooooo answer.
It works with the public function, but I get a different error code with the private method.
Simple answer: $this->test() required a parameter and you are simply not passing anything to it.
You can also modify your run2/run3 methods to do the following:
public function run2() {
$col = collect([1,2,3])->map(function ($value) {
return $this->test($value);
});
return $col; //returns a collection of items
//return $col->toArray(); //(extra option) returns an array of collected items
}
Result:
Illuminate\Support\Collection {#872 ▼
#items: array:3 [▼
0 => 2
1 => 3
2 => 4
]
#escapeWhenCastingToString: false
}
You may read more on collections here: https://laravel.com/docs/9.x/collections#introduction
The problem in run2 is that you are calling the method directly and you need to pass the an argument since test is expecting argument $a. To use the method as a callback function you need the pass it as an array which is the first value is $this and the second is the method name which is test :
public function test($a) {
return ($a + 1);
}
public function run2()
{
$collection = collect([1,2,3]);
$newCollection = $collection->map([$this,'test']);
return $newCollection;
}
UPDATE
It only work if the method that you will call is public and it will not work if you use a private or protected method. If you use a private or protected if will throw a BadMethodCallException

Post with xml file in laravel

I am using method post to create new data in xml file but The function c_element cannot be used in the function store
$DeTai = c_element('DeTai', $root);
This is my current code:
public function c_element($e_name, $parent)
{
global $xml;
$node = $xml->createElement($e_name);
$parent->appendChild($node);
return $node;
}
public function c_value($value, $parent)
{
global $xml;
$value = $xml->createTextNode($value);
$parent->appendChild($value);
return $value;
}
public function store(Request $request)
{
$xml = new DOMDocument("1.0","UTF-8");
$xml->load('../xml/QuanLyDoAnTotNghiep.xml');
if ($request->isMethod('post'))
{
$madt= $request->madt;
$noidungdetai = $request->noidungdetai;
$root=$xml->getElementsByTagName("QuanLyDoAnTotNghiep")->item(0);
$DeTai = c_element("DeTai", $root); //error in here
$s_madt = c_element('MaDT', $DeTai);
c_value("$madt", $s_madt);
$s_noidungdetai = c_element('NoiDungDeTai', $DeTai);
c_value("$noidungdetai", $s_noidungdetai);
$xml->formatOutput=true;
$xml->save('../xml/QuanLyDoAnTotNghiep.xml');
echo "Thêm mới thành công!!!";
}
}
use this keyword to call one method in different method of same class
$DeTai = $this->c_element('DeTai', $root);
to know more about it please visit this
Thanks..

Codeigniter 3: Is there any 'where' condition added to the Query Builder so far?

I'm using CodeIgniter 3. I just need to know if there is a 'where' condition added to the query builder until now.
I'm calling a 'delete' function that deleted rows from database And it's possible to add a where condition before calling that function. Something like this:
public function delete()
{
// Here I need to know if where condition added to the db class
$this->db
->where('field', 1)
->delete('my_table');
}
public function main()
{
$this->db->where('field2', 2);
$this->delete();
}
In controller
function delete_row()
{
$this->load->model('model_name');
// Pass the id or something else to the row_delete() method
$this->model_name->row_delete($id);
}
In Model
function row_delete($id)
{
$this->db->where('id', $id);
$this->db->delete('table_name');
}
According to your example:
public function delete_method($condition,$table_name )
{
$this->db->where($condition)
$this->db->delete($table_name);
}
public function main()
{
$condition = [
'field1'=>1,
'field2'=>2
];
$table_name = 'my_table';
$this->delete_method($condition,$table_name );
}
I found a solution.
The only thing I need to do is getting a select query and search for 'where' clause inside it:
public function delete()
{
// Here I need to know if where condition added to the db class
$sql = $this->db->get_compiled_select(NULL, FALSE);
$has_where = stripos($sql, 'where') !== FALSE;
// $has_where is TRUE if there is a where condition defined until now
$this->db
->where('field', 1)
->delete('my_table');
}
public function main()
{
$this->db->where('field2', 2);
$this->delete();
}

Laravel Mutators and Accessors

I have created mutator date function in model to convert the created_at date into human readable time using diffForHumans().
I have done the following
public function setDateAttribute($value){
return \Carbon\Carbon::parse($value)->diffForHumans();
}
It works fine but it affects in all. it is possible to apply this mutator only on the specified function of the controller rather than all function
A small logic in the mutator would do the job:-
public function setDateAttribute($value){
if ( request()->path() === "/yourURL/To/IndexMethod"){
return $this->attributes['date'] = \Carbon\Carbon::parse($value)->diffForHumans();
} else {
return $this->attributes['date'] = $value;
}
}
the idea is to check by the url.
getting request path helper
EDIT
Comparison using route name is better, as exact path can contain id/slugs.
public function setDateAttribute($value){
if ( \Route::currentRouteName() === "/yourRouteName/To/IndexMethod"){
return $this->attributes['date'] = \Carbon\Carbon::parse($value)->diffForHumans();
} else {
return $this->attributes['date'] = $value;
}
}
getting route name

How to cache model attributes in Laravel

In my current configuration, a user's email is stored on a remote server that I need to hit with a curl quest.
Luckily, I only need the email once a day when a certain process runs. However, when that process does run it will need to reference the email multiple times.
This is the current accessor I have set up for email. The problem is the curl request is being called every time I use $user->email. What's the best way to avoid this?
in UserModel:
public function getEmailAttribute(){
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
return $response->email;
}
private $cached_email = false;
public function getEmailAttribute(){
if ($this->cached_email){
// if set return cached value
return $this->cached_email;
}
// get the email
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
// cache the value
$this->cached_email = $response->email;
// and return
return $this->cached_email;
}
Depending on your use case make adjustments (ie. session, cache , static property...).
Extend a the Eloquent Model class
namespace App\Models\Utils;
use Illuminate\Database\Eloquent\Model as OldModel;
class MyModel extends OldModel
{
private $cachedAttributes = [];
public function getCachedAttribute(string $key, Callable $callable)
{
if (!array_key_exists($key, $this->cachedAttributes)) {
$this->setCachedAttribute($key, call_user_func($callable));
}
return $this->cachedAttributes[$key];
}
public function setCachedAttribute(string $key, $value)
{
return $this->cachedAttributes[$key] = $value;
}
public function refresh()
{
unset($this->cachedAttributes);
return parent::refresh();
}
}
make your class
class ElementWithEmail extends MyModel
{
const ATTRIBUTE_KEY_FOR_EMAIL = 'Email';
public function getEmailAttribute(){
$key = self::ATTRIBUTE_KEY_FOR_EMAIL;
$callable = [$this, 'getEmail'];
return $this->getCachedAttribute($key, $callable);
}
protected function getEmail()
{
$curl = new Curl;
$responseJson = $curl->post('https://www.dailycred.com/admin/api/user.json',array(
'client_id'=>getenv('dailycredId')
,'client_secret'=>getenv('dailycredSecret')
,'user_id'=>$this->id
));
$response = json_decode($responseJson);
return $response->email;
}
}
Call it from your code
$element = new ElementWithEmail();
echo $element->email;

Resources