Emre.xyz

@delirehberi thoughts

I don't know it is possible to enable vim mode, in bash before. I learned today and this feature is awesome.

You can enable vim mode in the bash with only change a parameter in your .bashrc file, like that.

set -o vi

That's it. You can use bash with vim strokes.

#tips #vim #en #bash

You can get file path in vim with % (percent) symbol. Sometimes you need to run a command with the current file path, for example git add filepath or sh filepath.

You can add the current file to git with this command in vim:

:!git add %

or you can run any file with this command:

:!%:p

For example, you write a bash script and saved it. But you don't want to close vim or you don't want to go to another terminal screen. Just you need to write :!%:p and vim run the file for you.

#tips #en #vim #git

When you develop an API client with PHP, you would need data mapper for mapping data from API to an object.

For example, you have requested an API endpoint and you get a response like that:

{
  "user": {
    "username": "delirehberi",
    "bio": "carpenter"
  }
}

And you have a User class

<?php
class User{ 
    protected $username; 
    protected $fullname; 
    protected $bio;
      /**
     * @return mixed
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @param mixed $username
     *
     * @return User
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getFullname()
    {
        return $this->fullname;
    }

    /**
     * @param mixed $fullname
     *
     * @return User
     */
    public function setFullname($fullname)
    {
        $this->fullname = $fullname;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getBio()
    {
        return $this->bio;
    }

    /**
     * @param mixed $bio
     *
     * @return User
     */
    public function setBio($bio)
    {
        $this->bio = $bio;

        return $this;
    }
}

You need to map JSON response to the PHP object. But how?

You will need to know how to use ReflectionClass, PropertyAccess, AnnotationReader.

How do you map class properties to API response fields and returns an object?

At first, you need to decode JSON string to PHP array or object.

$result = json_decode($response, true);

After that, create a Reflection class;

$reflectionObj = new \ReflectionClass(User::class);

Now you can access all properties

$properties = $reflectionObj->getProperties();

You don't need to know your User Class has which properties anymore.

Now, we need PropertyAccessor for manipulating our User object.

$propertyAccessor = PropertyAccess::createPropertyAccessor();

How can manipulate an object property with PropertyAccess object?

$propertyAccessor->setValue(
                    $userObject,
                    $property->getName(), //eg: username
                    $value //eg: emre
                );

OK, we know how to access the property of an object and how to manipulate another object properties.

Now we can start writing our data mapper.

Create an annotation class to configure properties to json values.

use Doctrine\Common\Annotations\Annotation;

/**
 * Class DataMapper.
 *
 * @Annotation
 * @Annotation\Target({"PROPERTY"})
 */
class DataMapper
{
    public $json_field;
}

We will use this object to define a JSON field equal to an object property. We can update our User class property annotations like that:

 /**
     * @var
     * @DataMapper(json_field="username")
     */
    protected $username;
    /**
     * @var
     * @DataMapper(json_field="full_name")
     */
    protected $fullname;
    /**
     * @var
     * @DataMapper(json_field="bio")
     */
    protected $bio;

note: don't forget to add DataMapper class to the header.

We are ready for writing data mapping logic.

  • Create an annotation reader object
  • Create property accessor
  • Create a new empty object (eg: User)
  • Create a reflection object for reach properties
  • Get object properties from reflection object
  • Foreach to properties
    • read property annotation for DataMapper annotation
    • get json_field value from annotation
    • access data from JSON with json_field value
    • set property of a new object with data
  • return new filled object :)
$reader = new AnnotationReader();
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$user = new User();
$reflectionObj = new \ReflectionClass(User::class);
$properties = $reflectionObj->getProperties();
foreach ($properties as $key => $property) {
  $propertyAnnotation = $reader->getPropertyAnnotation($property, DataMapper::class);
  $json_field = $propertyAnnotation->json_field;
  if(!$json_field) continue;
  $value = $result[$json_field];
  $propertyAccessor->setValue(
                    $user,
                    $property->getName(),
                    $value
                );
}

var_dump($user);

Congrats, you can write as a function and use in your project.

#php #reflection #datamapper #annotation #annotationreader #doctrine #symfony

You can run the same command in all active Tmux panes at the same time.

You need to activate command mode in Tmux with Ctrl+B keys and update synchronize-panes setting to on.

:setw synchronize-panes on

To disable it, set as off.

:setw synchronize-panes off

You can toggle synchronize-panes setting without use on-off parameters.

:setw synchronize-panes

#tmux #tips

Sometimes you get an error like that:

container extension liip_imagine not registered

You double-checked all configurations and you are absolutely sure nothing is wrong.

But code not works, why?

Because Php can't convert i to I if os language is not en. Your code searches Liipİmagine and can't find that. Change os's lang code to en_US.UTF-8 for fix that.

Or add this line to .bashrc or .profile file.

export LANG=en_US.UTF-8

bb.

#errors #php #debug #en

Today, we adopt a 3 legged cat for the office. Its name is Rosalind. We are working on some science projects. We gave its name from Rosalind Franklin.

Rosalind Franklin is the owner of the Photo 51 of DNA if you don't hear before. Photo 51 is a milestone for biology. You can read about that in Wikipedia if you are curious.

Rosalind :)

View this post on Instagram

Ofisimizin gülü #rosalind :) ziyarete bekleriz.

A post shared by emre ッ (@delirehberi) on

#cat #office #en

Hi, kats;

I want to talk about my morning routine. I try to keep this routine for 5 weeks and I want to stuck with this routine.

I wake up at approximately 06:00 am every morning. I try to be as consistent as possible with this because it helps to I stay motivated.

Once I'm up, the first thing I do is wash my face and I feed my cats (Tedi and Turgut). I clean the cat litter if my partner hasn't.

Then I check my emails and take notes if any further subject for looking after.

Sometimes I do a quick breakfast, sometimes we do Turkish style breakfast with my partner if not exists an urgent problem in the office.

Then I'm going to the door out and go to the office. I filter a morning coffee when I arrive in the office.

And the new day, let's start to work out.

-emre

#en #me

This is my blog's new face. I'll write my posts here after this.

I don't think to move my old posts to this blog. You can view my posts from github archive if you need.

Sometimes I may write posts in Turkish, sometimes in English. It would be practice for me I think.

I plan to write a post once in three days.

Anyway, hello world posts should be short.

-emre

#en