{"id":18750637,"url":"https://github.com/webiny/stdlib","last_synced_at":"2026-06-07T09:04:23.088Z","repository":{"id":20219006,"uuid":"23490693","full_name":"webiny/StdLib","owner":"webiny","description":"[READ-ONLY] A PHP library that puts objective wrappers around string, array, url and datetime. (master at Webiny/Framework)","archived":false,"fork":false,"pushed_at":"2017-11-26T21:25:28.000Z","size":90,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-26T18:06:14.648Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.webiny.com/","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webiny.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-30T11:49:14.000Z","updated_at":"2023-05-21T17:38:01.000Z","dependencies_parsed_at":"2022-07-25T07:02:03.704Z","dependency_job_id":null,"html_url":"https://github.com/webiny/StdLib","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FStdLib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FStdLib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FStdLib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webiny%2FStdLib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webiny","download_url":"https://codeload.github.com/webiny/StdLib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647257,"owners_count":21139081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-07T17:12:40.042Z","updated_at":"2026-06-07T09:04:22.999Z","avatar_url":"https://github.com/webiny.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"StdLib\n======\nThe standard library is a set of core Webiny framework components that make coding a bit more pleasurable when working with some low level objects like strings, array, date time and url.\n\nInstall the component\n---------------------\nThe best way to install the component is using Composer.\n\n```bash\ncomposer require webiny/std-lib\n```\nFor additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/std-lib).\n\n## About\n\nThe standard library consists of following libraries:\n- [Exception](Exception)\n- [StdObject] (StdObject)\n\n.. and following helper traits:\n- ComponentTrait\n- FactoryLoaderTrait\n- SingletonTrait\n- ValidatorTrait\n- StdLibTrait\n- StdObjectTrait\n\n## ComponentTrait\nThis trait is used when creating a new Webiny Framework component. By implementing this trait you automatically get the possibility to register the required class loader libraries, services and additional parameters for your new component. The trait is based on two main methods `setConfig` and `getConfig`, both methods are defined as `public static`.\n\nHere is an example Webiny Framework component that uses the `ComponentTrait`.\n\n```php\n$crypy = \\Webiny\\Component\\Crypt::setConfig('ExampleConfig.yaml');\n```\nThe `ExampleConfig.yaml` file is defined like this:\n\n```yaml\nCrypt:\n    Services:\n        Password:\n            Class: \\Webiny\\Component\\Crypt\\Crypt\n            Arguments: [Blowfish, CCM, rijndael-128, _FOO_VECTOR]\n        Cookie:\n            Class: \\Webiny\\Component\\Crypt\\Crypt\n            Arguments: [Blowfish, CCM, rijndael-128, _FOO_VECTOR]\n    Bridge: \\Webiny\\Component\\Crypt\\Bridge\\CryptLib\\CryptLib\n    ClassLoader:\n        CryptLib: /var/www/Vendors/PHP-CryptLib/lib/CryptLib\n```\n\nThere are couple of things you should notice:\n- the root of yaml file matches the class name (`Crypt`) and the class name matches the root component namespace `\\Webiny\\Component\\Crypt`\n- the `Services` definition is based on rules defined by [ServiceManager)(../ServiceManager) component and all services defined in the yaml file are automatically registered with the `ServiceManager` under the name `ComponentName.ServiceName` in this example that would be `Crypt.Password` and `Crypt.Cookie`\n- the `Bridge` is an optional parameter. You can define as many optional parameters as you want\n- the `ClassLoader` parameter is an array that contains the namespace as key and path as a value. All the definitions under the `ClassLoader` parameter are automatically assigned to Webiny Framework ClassLoader component\n\nSo basically as you see, the `ComponentTrait` does a lot of handy stuff and makes Webiny Framework components very neatly organised.\n\n### Callback\nIf you want to know when the configuration file on your class has been parsed, and all the services and class loader paths have been assigned, you can just define a `protected static function _postSetConfig` inside your component class, and the `ComponentTrait` will automatically call it each time you define a configuration for that class.\n\n## FactoryLoaderTrait\nA handy trait when you want to load some classes where the class name is defined as a string or inside a variable and you want to pass along some parameters to the constructor. You can also pass a interface or a class name that the factory class must implement/extend. This trait will first construct the object, with the given parameters, and then it will verify its instance against the given interface or a class. If everything matches, the class instance is returned, otherwise, and Exception is thrown.\n\n```php\nclass MyClass\n{\n    use FactoryTrait;\n\n    function myMethod()\n    {\n        // let's say we have a class we want to construct\n        // the class has a constructor defined like this __construct($param1, $param2)\n        // the class should implement \\Webiny\\Crypt\\Bridge\\CryptInterface\n        $class = '\\SomeVendor\\SomeLib\\SomeClass';\n\n        try {\n            $classInstance = $this-\u003efactory($class, '\\Webiny\\Crypt\\Bridge\\CryptInterface', ['foo1', 'foo2']);\n        } catch (\\Webiny\\StdLib\\Exception\\Exception $e) {\n            throw $e; // the class probably doesn't implement the required interface\n        }\n    }\n}\n```\n\n## SingletonTrait\nThe `SingletonTrait` is used on classes that must implement [singleton pattern](http://en.wikipedia.org/wiki/Singleton_pattern). You just `use` the trait on the class you want to be singleton, and that's it.\n\nThere are two methods that you can optionally implement in your class, `public function init()` and `protected function _init()`, they are called only once and at the moment when you request a singleton instance.\n\n```php\nclass MyClass\n{\n    use SingletonTrait;\n\n    protected function __init()\n    {\n        // this will be called only once\n        $this-\u003e__somePrivateMethod();\n    }\n}\n```\n\nTo use your class you just call the static `getInstance` method that is implemented by the trait.\n\n```php\n$instance = MyClass::getInstance(); // this calls the internal __init method, but only the first time, when it creates the instance\n```\n\n\n## StdLibTrait\nAlso a helper trait with some commonly used functions. The trait itself only supports a limited number of methods, but we plan to expand it with more, so feel free to give suggestions.\n\nThis trait, not only that it defines helper methods, but it also `uses` `ValidatorTrait` and `StdObjectTrait` traits.\n\n## StdObjectTrait\nThis trait provides helper functions when working with `StdObject` library.\n\n```php\nclass MyClass\n{\n    use StdObjectTrait;\n\n    public function test()\n    {\n        // create StringObject\n        $this-\u003estr('This is a string');\n\n        // create ArrayObject\n        $this-\u003earr(['one', 'two']);\n\n        // create UrlObject\n        $this-\u003eurl('http://www.webiny.com/');\n\n        // create DateTimeObject\n        $this-\u003edateTime('now');\n    }\n}\n```\n\n## ValidatorTrait\nThis is a helper trait with some common validation methods like `isNull`, `isEmpty`, `isInstanceOf` and many more.\nJust view the class, all the methods are documented and, mostly self-explanatory.\n\nResources\n---------\n\nTo run unit tests, you need to use the following command:\n\n    $ cd path/to/Webiny/Component/StdLib/\n    $ composer.phar install\n    $ phpunit","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fstdlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebiny%2Fstdlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebiny%2Fstdlib/lists"}