bem / bh-php

PHP port of https://github.com/bem/bh. It's cool thing but better use this:
https://github.com/zxqfox/php-fpm-bem-demo-heroku
MIT License
34 stars 9 forks source link

Rework incoming data normalizing #3

Closed qfox closed 9 years ago

qfox commented 9 years ago

We should create a special classes for mods, bemjson nodes, etc.

Mods:

class Mods implements Iterator {
  protected $content = [];
  // etc.
}

JsonNode (or Json, or Node):

class JsonNode {
  protected $block = 'page';
  protected $elem = 'content';
  protected $mods = null; // Mods
  protected $blockMods = null; // Mods
  protected $elemMods = null; // Mods
  protected $attrs = 'page';
  protected $content = [];

  // etc.
  function __construct (array $bemjson) {
    $this->block = $bemjson['block'];
    $this->elem = $bemjson['elem'];
    $this->content = $bemjson['content'];
  }

  function iterate (callable $cb) {
    // call cb for each node
  }

  public static buildTree ($bemjson) {
    if (is_array($bemjson)) {
      $res = [];
      foreach ($bemjson as $node) {
        $res[] = static::buildTree($node);
      }
      return $res;
    } elseif ($bemjson instanceof self) {
      return $bemjson;
    } else {
      // etc.
      return new self($bemjson);
    }
  }
}
qfox commented 9 years ago

Done in 54cf16218