格式化json代码

@param $code

@return string
161121
private static function formatCode($code)
    {
        $code = array_map(function ($val) {
            return trim(ltrim(trim($val), '*'));
        }, explode("\n", trim($code)));
        $dep = 0;

        foreach ($code as $lineNum => &$line) {
            $pos = strpos($line, '//');
            $pos || $pos = strpos($line, '#');
            $wordLine = $pos === false ? $line : trim(substr($line, 0, $pos));
            $firstWord = substr($wordLine, 0, 1);
            $lastWord = substr($wordLine, -1);
            $lineNum === 0 && $line !== '{' && $line = "{\n    " . substr($line, 1);

            //本行就要减空格
            if (
                $firstWord === '}'//行首
                || $firstWord === ']' //行首
                || ($lastWord === '}' && false === strpos($line, '{'))
                || ($lastWord === ']' && false === strpos($line, '['))
            ) {
                --$dep;
            }

            $line = str_pad("", $dep * 4, " ", STR_PAD_LEFT) . $line;

            //下一行加空格
            if (
                $lastWord === '{'
                || $lastWord === '['
                || ($firstWord === '{' && false === strpos($line, '}'))
                || ($firstWord === '[' && false === strpos($line, ']'))
            ) {
                ++$dep;
            }
        }

        return implode("\n", $code);
    }
1080823
public function repositories(string $code=''){
    $all = $this->options->full_soptions($this->options->from_code('PATHS', 'ide', 'appui'));      
    $cats = [];
    $r = [];
    foreach ( $all as $a ){
      if ( \defined($a['bbn_path']) ){
        $k = $a['bbn_path'] . '/' . ($a['code'] === '/' ? '' : $a['code']);
        if ( !isset($cats[$a['id_alias']]) ){
          unset($a['alias']['cfg']);
          $cats[$a['id_alias']] = $a['alias'];
        }
        unset($a['cfg']);
        unset($a['alias']);
        $r[$k] = $a;
        $r[$k]['title'] = $r[$k]['text'];
        $r[$k]['alias_code'] = $cats[$a['id_alias']]['code'];
        if ( !empty($cats[$a['id_alias']]['tabs']) ){
          $r[$k]['tabs'] = $cats[$a['id_alias']]['tabs'];
        }
        else if( !empty($cats[$a['id_alias']]['extensions']) ){
          $r[$k]['extensions'] = $cats[$a['id_alias']]['extensions'];
        }
        unset($r[$k]['alias']);
      }
    }
    if ( $code ){
      return isset($r[$code]) ? $r[$code] : false;
    }
    return $r;
  }
147219
public static function getLogStringByCode($code)
    {
        $code = is_numeric($code) && !is_float($code)
            ? abs($code)
            : $code;
        if (!is_int($code)) {
            return null;
        }

        switch (self::getConvertAliasLogLevel($code)) {
            case Logger::NOTICE:
                return 'notice';
            case Logger::ERROR:
                return 'error';
            case Logger::WARNING:
                return 'warning';
            case Logger::INFO:
                return 'info';
            case Logger::DEBUG:
                return 'debug';
            case Logger::EMERGENCY:
                return 'emergency';
            case Logger::CRITICAL:
                return 'critical';
            case Logger::ALERT:
                return 'alert';
        }

        return null;
    }
543616
static public function getRequestConfig($code = Service::BP)
    {
        Service::isValid($code);

        $config = [
            static::T540  => ['Date expédition format AAAAMMJJ', true, 8],
            static::T530  => ['Poids format xx.xx', true, 5],
            static::T860  => ['Raison sociale destinataire', true, 35],
            static::T863  => ['Rue destinataire', true, 35],
            static::T861  => ['Complément 1 adresse destinataire', false, 35],
            static::T862  => ['Complément 2 adresse destinataire', false, 35],
            static::T330  => ['Code postal destinataire', true, 10],
            static::T864  => ['Ville Destinataire', true, 35],
            static::T100  => ['Pays Destinataire', true, 2],
            static::T8906 => ['Commentaires', false, 35],
            static::T871  => ['Téléphone Destinataire', false, 20],
            static::T859  => ['Votre référence destinataire', false, 20],
            static::T854  => ['Référence supplémentaire 1', false, 20],
            static::T8908 => ['Référence supplémentaire 2', false, 20],
            static::T1229 => ['Email destinataire', false, 100],
            static::T1230 => ['Portable', false, 20],
            static::T810  => ['Raison sociale Expéditeur', true, 35],
            static::T820  => ['Adresse expéditeur', true, 35],
            static::T821  => ['Pays Expéditeur', true, 2],
            static::T822  => ['Code postal Expéditeur', true, 10],
            static::T823  => ['Ville Expéditeur', true, 35],
            static::T8700 => ['Dépôt GLS Expéditeur', true, 6],
            static::T8915 => ['Code client GLS', true, 10],
            static::T8914 => ['Contact Id (compte sur lequel va être affecté le colis)', true, 10],
            static::T8904 => ['Quantième colis dans Expédition (présent sur l’étiquette physiquement)', true, 3],
            static::T8973 => ['Quantième colis dans Expédition (présent dans le code barre datamatrix)', true, 3],
            static::T8905 => ['Nombre total colis Expédition (présent sur l’étiquette physiquement)', true, 3],
            static::T8702 => ['Nombre total colis Expédition (présent dans le code barre datamatrix)', true, 3],
            static::T8975 => ['Référence Origine GLS', true, 1024],
            static::T082  => ['Constante : UNIQUENO Transmettre cette valeur que si le destinataire est en France', true, 1042],
            static::T090  => ['Constante : NOSAVE ', true, 1042],
        ];

        if ($code === Service::EBP) {
            // Spécificités livraisons Shop Delivery Service
            $config = array_replace($config, [
                static::T200  => ['Service Shod Delivery ("SHD")', true, 3],
                static::T750  => ['Libellé service ("ShopDelivery-Service")', true, 1024],
                static::T1229 => ['Adresse Email Destinataire', true, 100],
                static::T1230 => ['Tél mobile Destinataire', true, 20],
                static::T8237 => ['Identifiant Point relais', true, 10],
                static::T8904 => ['Quantième colis dans Expédition ("1")', true, 3],
                static::T8973 => ['Quantième colis dans Expédition ("1")', true, 3],
                static::T8905 => ['Nombre total colis Expédition ("1")', true, 3],
                static::T8702 => ['Nombre total colis Expédition ("1")', true, 3],
            ]);
        } else if ($code === Service::EP) {
            // Spécificités livraison Express Parcel Guaranteed
            $config = array_replace($config, [
                static::T200  => ['13:00 service ("T13")', true, 3],
                static::T206  => ['Produit Express Parcel Guaranteed ("EP")', true, 1024],
                static::T8904 => ['Quantième colis dans Expédition ("1")', true, 3],
                static::T8973 => ['Quantième colis dans Expédition ("1")', true, 3],
                static::T8905 => ['Nombre total colis Expédition ("1")', true, 3],
                static::T8702 => ['Nombre total colis Expédition ("1")', true, 3],
            ]);
        } else if ($code === Service::FDF) {
            // Spécificités livraison Flex Delivery Service
            $config = array_replace($config, [
                static::T200  => ['Service Flex Delivery Service ("FDF")', true, 3],
                static::T750  => ['Libellé service  ("Flex Delivery Service")', true, 1024],
                static::T1229 => ['Adresse Email Destinataire', true, 100],
                static::T1230 => ['Tél mobile Destinataire', true, 20],
            ]);
        }

        return $config;
    }
1273288
public function getOpenIdByMini($code) {
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.self::$config['appId'].'&secret='.self::$config['appSecret'].'&js_code='.$code.'&grant_type=authorization_code';
        $result = $this->curl($url);
        $data = $this->get($result);
        return $data ? $data['openid'] : false;
    }
373111
public function getAuth($code)
    {
        $result = $this->_curl(
            'https://api.weixin.qq.com/sns/oauth2/access_token',
            [
                'appid' => $this->getConfig('appid'),
                'secret' => $this->getConfig('secret'),
                'code' => $code,
                'grant_type' => 'authorization_code',
            ]
        );

        if ($result) {
            return [
                'access_token' => $result['access_token'],
                'openid' => $result['openid'],
            ];
        }

        return false;
    }
9752
public function dirs($code=false){
    $all = $this->options->full_soptions(self::_dev_path());
    $cats = [];
    $r = [];
    foreach ( $all as $a ){
      if ( \defined($a['bbn_path']) ){
        $k = $a['bbn_path'] . '/' . ($a['code'] === '/' ? '' : $a['code']);
        if ( !isset($cats[$a['id_alias']]) ){
          unset($a['alias']['cfg']);
          $cats[$a['id_alias']] = $a['alias'];
        }
        unset($a['cfg']);
        unset($a['alias']);
        $r[$k] = $a;
        $r[$k]['title'] = $r[$k]['text'];
        $r[$k]['alias_code'] = $cats[$a['id_alias']]['code'];
        if ( !empty($cats[$a['id_alias']]['tabs']) ){
          $r[$k]['tabs'] = $cats[$a['id_alias']]['tabs'];
        }
        else{
          $r[$k]['extensions'] = $cats[$a['id_alias']]['extensions'];
        }
        unset($r[$k]['alias']);
      }
    }
    if ( $code ){
      return isset($r[$code]) ? $r[$code] : false;
    }
    return $r;
  }
101053
public static function json( $src, $encode = 0 ) {
		$options = JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT;
		$out     = $encode ? json_encode( $src, $options ) : json_decode( $src, true );

		return $out;
	}
737749
public function getAccessTokenResponse($code)
    {
        $postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? 'form_params' : 'body';

        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
            'headers' => ['Accept' => 'application/json'],
            $postKey => $this->getTokenFields($code),
        ]);

        return json_decode($response->getBody(), true);
    }
366722
public function get_string_from_code($code) {
		$errors = [
			self::ERR_GENERAL => 'General Error',
			self::ERR_BAD_REQUEST => 'Bad Request',
			self::ERR_UNAUTHORIZED => 'Unauthorized',
			self::ERR_FORBIDDEN => 'Forbidden',
			self::ERR_INTERNAL_ERROR => 'Internal Error',
			self::ERR_PRECONDITION_FAILED => 'Precondition Failed',
			self::ERR_RESOURCE_NOT_FOUND => 'Resource Not Found',
			self::ERR_INVALID_RESOURCE => 'Invalid Resource',
			self::ERR_NOT_IMPLEMENTED => 'Feature not implemented',
			self::ERR_DISABLED => 'Feature Disabled',
			self::ERR_NETWORKING_DISABLED => 'Networking is Disabled',
		];

		return (isset($errors[$code])) ? $errors[$code] : '';
	}
1155702
public function getUserId($code)
    {
        $apiUrl = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo';

        $result = $this->_curl($apiUrl, [
            'access_token' => $this->getAccessToken(),
            'code' => $code,
        ]);

        return ($result and isset($result['UserId'])) ? $result['UserId'] : null;
    }
65946
public static function getMessageByCode($code)
    {
        return isset(self::$_httpCodes[$code]) ? self::$_httpCodes[$code] : null;
    }
304090
public function getAccessTokenResponse(string $code)
    {
        $response = $this->getHttpClient()->post($this->getTokenUrl(), [
            'headers' => ['Accept' => 'application/json'],
            'form_params' => $this->getTokenFields($code),
        ]);
        return json_decode($response->getBody(), true);
    }
615123
protected function getAuthCode($code)
    {
        if (null === $code = $this->app['repo.api-oauth-codes']->find($code)) {
            return null;
        }

        return [
            'redirect_uri' => $code->getRedirectUri(),
            'client_id'    => $code->getAccount()->getApplication()->getClientId(),
            'expires'      => $code->getExpires(),
            'account_id'   => $code->getAccount()->getId(),
        ];
    }
115687
public function getByCode($code)
    {
        if (\is_scalar($code) && ! empty($code = trim($this->uppercaseAndSafeTransliterate($code)))) {
            foreach ($this->items as $auto_category) {
                if ($auto_category->getCode() === $code) {
                    return $auto_category;
                }
            }
        }
    }
800357
protected function json($message = '操作成功', $code = 1, array $append = [])
    {
        return $this->response->json(['message' => $message, 'code' => $code] + $append);
    }
111487
public function getAddress($code): string
    {
        return $this->getProvince($code) . $this->getCity($code) . $this->getCounty($code);
    }