PHP Classes

File: src/App/Databag/Databag.php

Recommend this page to a friend!
  Packages of Thierry Feuzeu   Jaxon   src/App/Databag/Databag.php   Download  
File: src/App/Databag/Databag.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Jaxon
Call PHP classes from JavaScript using AJAX
Author: By
Last change:
Date: 6 months ago
Size: 2,418 bytes
 

Contents

Class file image Download
<?php

namespace Jaxon\App\Databag;

use
Jaxon\Plugin\Response\Databag\DatabagPlugin;
use
JsonSerializable;

use function
array_map;
use function
is_array;
use function
key_exists;

class
Databag implements JsonSerializable
{
   
/**
     * @var DatabagPlugin
     */
   
protected $xPlugin;

   
/**
     * @var array
     */
   
protected $aData = [];

   
/**
     * @var bool
     */
   
protected $bTouched = false;

   
/**
     * The constructor
     *
     * @param array $aData
     */
   
public function __construct(DatabagPlugin $xPlugin, array $aData)
    {
       
$this->xPlugin = $xPlugin;
       
// Ensure all contents are arrays.
       
$this->aData = array_map(function($aValue) {
            return
is_array($aValue) ? $aValue : [];
        },
$aData);
    }

   
/**
     * @return bool
     */
   
public function touched(): bool
   
{
        return
$this->bTouched;
    }

   
/**
     * @return array
     */
   
public function getAll(): array
    {
        return
$this->aData;
    }

   
/**
     * @param string $sBag
     *
     * @return void
     */
   
public function clear(string $sBag): void
   
{
       
$this->aData[$sBag] = [];
       
$this->xPlugin->addCommand('databag.clear', ['bag' => $sBag]);
    }

   
/**
     * @param string $sBag
     * @param string $sKey
     * @param mixed $xValue
     *
     * @return void
     */
   
public function set(string $sBag, string $sKey, $xValue): void
   
{
       
$this->bTouched = true;
       
$this->aData[$sBag][$sKey] = $xValue;
    }

   
/**
     * @param string $sBag
     * @param string $sKey
     * @param mixed $xValue
     *
     * @return void
     */
   
public function new(string $sBag, string $sKey, $xValue): void
   
{
       
// Set the value only if it doesn't already exist.
       
if(!isset($this->aData[$sBag]) || !key_exists($sKey, $this->aData[$sBag]))
        {
           
$this->set($sBag, $sKey, $xValue);
        }
    }

   
/**
     * @param string $sBag
     * @param string $sKey
     * @param mixed $xValue
     *
     * @return mixed
     */
   
public function get(string $sBag, string $sKey, $xValue = null): mixed
   
{
        return
$this->aData[$sBag][$sKey] ?? $xValue;
    }

   
/**
     * Convert this call to array, when converting the response into json.
     *
     * @return array
     */
   
public function jsonSerialize(): array
    {
        return
$this->aData;
    }
}